C++ Reference ยท Types and Language Concepts
Concepts and constraints in C++20
Reference for C++20 concepts: defining concepts, requires clauses and expressions, constrained template parameters, abbreviated templates, standard concepts.
A concept is a named compile-time predicate on template arguments. It states what a type must be able to do, for example "supports + and gives the same type back", or "is a range of integers". You use a concept to constrain a template, so a call with an unsuitable type is rejected at the call with a message that names the unmet requirement, instead of failing deep inside the template body.
Concepts were added in C++20, together with the requires keyword, which has two uses: a requires-clause (a condition attached to a template) and a requires-expression (a list of checks that evaluates to true or false). The header <concepts> provides ready-made concepts such as std::integral and std::copyable.
template <typename T> concept Addable = requires(T a, T b) { { a + b } -> std::convertible_to<T>; }; template <Addable T> T add(T a, T b) { return a + b; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <typename T> concept Name = constraint-expression; // define template <Name T> void f(T); // constrained parameter template <typename T> requires Name<T> void f(T); // requires clause template <typename T> void f(T) requires Name<T>; // trailing requires clause void f(Name auto x); // abbreviated template requires(Params) { expression; // simple requirement: must be valid typename T::type; // type requirement { expression } noexcept -> Concept<Args>; // compound: result must satisfy a concept requires bool-constant; // nested requirement }
Standard concept (<concepts>) |
Meaning |
|---|---|
same_as, derived_from, convertible_to, constructible_from |
Relations between types. |
integral, signed_integral, unsigned_integral, floating_point |
Arithmetic categories. |
copyable, movable, semiregular, regular, swappable |
Object properties. |
equality_comparable, totally_ordered |
Comparison support. |
invocable, predicate, relation |
Callable requirements. |
std::ranges::range, input_iterator, sortable (other headers) |
Iterator and range concepts. |
Usage notes
- Concepts are evaluated at compile time and produce
trueorfalse, so they work instatic_assert,if constexprand as plainboolexpressions. - Subsumption. If one concept is defined by
&&-ing another, it is more constrained, and overload resolution picks the more constrained function (assigned_integraloverintegralabove). Subsumption works only through concept names and&&/||, not through arbitraryrequires (A && B)expressions repeated textually. - An invalid requirement gives
false, not an error. In a requires-expression that depends on a template parameter, an expression that does not compile makes the whole expressionfalse. - Constraints do not check semantics.
std::equality_comparableverifies that==compiles, not that it is an equivalence relation. - Better errors, not zero errors. An unsatisfied constraint lists which atomic constraint failed. Use small, well-named concepts to get readable messages.
- Constrain the interface, not the body. Concepts on public function templates document the requirements; internal helpers can stay unconstrained.
- Replaces SFINAE for most uses (see SFINAE): no
enable_if, novoid_t. - Class templates and variable templates can be constrained, and partial specializations can use concepts:
template <std::integral T> struct S<T> {}. - A name that is not a type must be reached with
typenamein a type requirement (typename T::value_type;). - Concept-constrained
autoalso works for variables:std::integral auto n = 5;.