Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The concept and requires keywords

Reference for C++20 concept and requires: defining concepts, requires clauses, requires expressions, compound and nested requirements, and subsumption.

A concept is a named compile-time predicate on template arguments. Declared with the concept keyword, it lets you state what a template needs from its arguments and get a clear error when the requirement is not met, instead of a wall of errors from deep inside the template.

The requires keyword appears in two places. A requires clause attaches a constraint to a template or function. A requires expression is a boolean expression that checks whether a set of operations would compile. They are often combined: requires requires { ... }, though a named concept is usually cleaner.

C++
template <typename T>
concept Addable = requires(T a, T b) { a + b; };

template <Addable T>
T sum(T a, T b) { return a + b; }

Try it

C++ Defining and using concepts

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.

C++ Overloading by constraints and testing concepts

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

C++
template <template-parameters>
concept Name = constraint-expression;

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 function template

requires (parameters) { requirement; ... }     // requires expression
Requirement kind Form Satisfied when
Simple a + b; The expression is valid.
Type typename T::value_type; The type name exists.
Compound { expr } noexcept -> Concept; The expression is valid (and is noexcept if stated), and its type satisfies the concept.
Nested requires bool-constant; The constant expression evaluates to true.

Usage notes

  • A concept cannot be constrained itself, cannot refer to itself recursively and cannot be specialized.
  • In a compound requirement { e } -> C; the concept is checked against decltype((e)), so -> std::same_as<int> fails for an lvalue expression of type int (it is int&). Use std::convertible_to or std::same_as<int&>.
  • requires clauses accept only primary expressions joined by && and ||. Wrap anything else in parentheses: requires (sizeof(T) > 4).
  • Subsumption: when two constrained overloads match, the one whose constraint is a logical superset of the other is chosen, but only if both are written with named concepts or identical atomic expressions.
  • A requires expression with invalid types in its parameter list or body does not cause a hard error: it just yields false. This works only inside templates; outside a template an invalid expression is still an error.
  • The standard library provides many ready-made concepts in <concepts> (same_as, integral, invocable, regular...), <iterator> and <ranges>.
  • Concepts check syntax, not semantics: a type with a + that subtracts still satisfies Addable.
  • Prefer a short, named concept to an inline requires requires, because a name documents the intent and error messages mention it.