词条 | Concepts (C++) |
释义 |
}} Concepts are an extension to C++'s templates, published as an ISO Technical Specification ISO/IEC TS 19217:2015.[1] They are named boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, function template, or member function of a class template), in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters. The following is a declaration of the concept "EqualityComparable" from the concept-enabled C++ standard library (which is a separate ISO Technical Specification, ISO/IEC DTS 21425). This concept is satisfied by any type A function template constrained on this concept may be declared as follows: And may be called as usual: The main uses of concepts are:
Compiler diagnosticsIf a programmer attempts to use a template argument that does not satisfy the requirements of the template, the compiler will generate an error. When concepts are not used, such errors are often difficult to understand because the error is not reported in the context of the call, but rather in an internal, often deeply nested, implementation context where the type was used. For example, {{Cpp|std::sort}} requires that its first two arguments be random-access iterators. If an argument is not an iterator, or is an iterator of a different category, an error will occur when std::sort attempts to use its parameters as bidirectional iterators: Typical compiler diagnostic without concepts is over 50 lines of output, beginning with a failure to compile an expression that attempts to subtract two iterators: In instantiation of '{{cpp|1=void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator If concepts are used, the error can be detected and reported in the context of the call: Overload resolutionConcepts can be used to choose function template overloads and class template specializations based on properties of their template arguments, as an alternative to SFINAE and tag dispatching. If an argument satisfies more than one concept, the overload associated with the more constrained concept is chosen. Type deductionConcepts may be used instead of the unconstrained type deduction placeholder {{Cpp|auto}} in variable declarations and function return types: Implementation statusConcepts, as specified in ISO/IEC TS 19217:2015, are implemented in GCC 6.[2] During the C++ standards committee meeting in March 2016, the evolution working group moved to merge Concepts into the mainline C++17 standard, but the motion was defeated in full committee.[3] Concepts v1 was merged into the C++20 draft.[4] HistoryA different form of Concepts, popularly known as "C++0x Concepts," was temporarily accepted into the working paper for C++11 but was removed in 2009.[5] In addition to concepts themselves, "C++0x Concepts" included concept maps (a feature that could make it possible, for example, for the concept "Stack" to accept {{Cpp|std::vector}}, automatically mapping "Stack" operations such as `push()` to differently named operations on `std::vector`, such as `push_back()`) and axioms (a facility to specify semantic properties such as associativity or commutativity, allowing the compiler to take advantage of these properties without proof). In contrast to this abandoned proposal, the C++20 version of Concepts is sometimes referred to as "Concepts Lite."[6] See also
Notes1. ^{{cite web|url=http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=64031|title=ISO/IEC TS 19217:2015|publisher=ISO|date=15 November 2015}} 2. ^{{cite web | url=https://gcc.gnu.org/gcc-6/changes.html | title=GCC 6 Release Series - Changes, New Features, and Fixes}} 3. ^{{cite web|url=http://honermann.net/blog/2016/03/06/why-concepts-didnt-make-cxx17/|title=Why Concepts didn’t make C++17|last=Honermann|first=Tom|date=6 March 2016|publisher=honermann.net}} 4. ^{{cite web | url=https://www.reddit.com/r/cpp/comments/6ngkgc/2017_toronto_iso_c_committee_discussion_thread/ | title=2017 Toronto ISO C++ Committee Discussion Thread (Concepts in C++20; Coroutines, Ranges and Networking TSes published) : cpp}} 5. ^{{cite web |url=http://www.drdobbs.com/cpp/the-c0x-remove-concepts-decision/218600111 |title=The C++0x "Remove Concepts" Decision |publisher=Dr. Dobbs |author=Bjarne Stroustrup |date=2009-07-22}} 6. ^{{cite web |url=https://isocpp.org/blog/2013/02/concepts-lite-constraining-templates-with-predicates-andrew-sutton-bjarne-s |title=Concepts Lite: Constraining Templates with Predicates |date=2013-02-24 |publisher=isocpp.org |author=Andrew Sutton}} References
External links
2 : C++|Articles with example C++ code |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。