Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

requires — constraints and requirement checking

The requires keyword introduces either a clause constraining a declaration or an expression checking requirements on types and operations. Distinguishing these uses helps you write templates whose availability depends on the capabilities of the supplied type.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

template<class T> requires Constraint<T> void f(T);
template<class T> void f(T) requires Constraint<T>;
requires (T x) { x + x; typename T::value_type; { x + x } noexcept -> ResultConcept; requires Constraint<T>; }

requires has two main uses. A requires-clause specifies a declaration's constraint: it can follow the template parameter list or appear as a trailing clause on a function. Unsatisfied constraints exclude a function from the viable candidates during overload resolution; they do not select a runtime branch.

A requires-expression, requires (T x) { ... }, produces a value of type bool; its parameter list is optional. Local parameters are only notation for requirements—they do not create objects. In requires requires (T x) { x + x; }, the first requires starts a clause and the second starts an expression.

There are four requirement kinds inside the braces: a simple requirement x + x; checks expression validity; a type requirement typename T::value_type; checks a type name; a compound requirement { x + x } noexcept -> ResultConcept; checks that the expression is valid, that it is not classified as potentially throwing, and that decltype((x + x)) satisfies the concept; a nested requirement requires Constraint<T>; requires satisfaction of a constraint. Both noexcept and -> ResultConcept are optional. The noexcept check uses the language's classification of the expression, not whether an exception can actually be thrown during execution. An overloaded operator declared noexcept can throw inside its body; if an exception escapes the operator, std::terminate is called. Simple and compound requirement operands are unevaluated; x == x; alone does not require a true result.

Substitution failure in the immediate context of a template's requirements can yield false instead of a compilation error. This is not a universal error-suppression mechanism: an invalid requirement outside a templated entity's declaration makes the program ill-formed. If substituting template arguments into a requirement would always result in a substitution failure, the program is ill-formed with no diagnostic required. An unsatisfied requirement is not by itself a substitution failure: a nested requirement requires false; can validly make a requires-expression false for every substitution.

In the example, the Addable concept contains a requires-expression, while add has a requires Addable<T> clause. The static_assert checks cover a supported type, an unsupported type, and the addition result. This entry targets C++23; the supplied document is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>

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

template<class T>
requires Addable<T>
constexpr auto add(T a, T b) {
    return a + b;
}

struct NoAdd {};

static_assert(Addable<int>);
static_assert(!Addable<NoAdd>);
static_assert(add(2, 3) == 5);

int main() {
    std::cout << add(2, 3) << '\n';
}

Result

5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources