ⓘ Concepts, C++. Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template pa ..


ⓘ Concepts (C++)

Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template, in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters.

Originally dating back to suggestions for C++11, the original concepts specification has been revised multiple time before now formally being a required part of C++20.


1. Main uses

The main uses of concepts are:

  • Simplified compiler diagnostics for failed template instantiations
  • Introducing type-checking to template programming
  • Constraining automatic type deduction
  • Selecting function template overloads and class template specializations based on type properties

2. Example: EqualityComparable

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 T such that for lvalues a and b of type T, the expressions a==b and a!=b compile and their results are convertible to a type that satisfies the concept "Boolean":

A function template constrained on this concept may be declared as follows:

And may be called as usual:


3. Compiler diagnostics

If 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, 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 void std __sort: error: no match for operator- operand types are std _List_iterator < int > and std _List_iterator < int > std __lg __last - __first * 2,

If concepts are used, the error can be detected and reported in the context of the call:


4. Overload resolution

Concepts 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.


5. Implementation status

Concepts TS, as specified in ISO/IEC TS 19217:2015, are implemented as an experimental feature in GCC 6. C++20 concepts are fully implemented in GCC 10, and partially in MSVC 19.23 and Clang 10.


6. History

A 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. In addition to concepts themselves, "C++0x Concepts" included concept maps 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."

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.

Concepts v1 was merged into the C++20 draft.

"The One Range" version of Range feature that depend on concepts was also merged into C++20.

Free and no ads
no need to download or install

Pino - logical board game which is based on tactics and strategy. In general this is a remix of chess, checkers and corners. The game develops imagination, concentration, teaches how to solve tasks, plan their own actions and of course to think logically. It does not matter how much pieces you have, the main thing is how they are placement!

online intellectual game →