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.

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 as well as the reverse b==a and b!=a compile, and their results are convertible to a type that satisfies the concept "boolean-testable": A function template constrained on this concept may be declared as follows: or And may be called as usual: If a programmer attempts to use a template argument that does not satisfy the requirements of the template, the compiler will generate an error.

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: If concepts are used, the error can be detected and reported in the context of the call: 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.

Concepts may be used instead of the unconstrained type deduction placeholder auto in variable declarations and function return types: Concepts TS, as specified in ISO/IEC TS 19217:2015, are implemented as an experimental feature in GCC 6.