Files
2025-10-25 03:02:53 +03:00

7.2 KiB
Raw Permalink Blame History

[expr.prim.req.general]

7 Expressions [expr]

7.5 Primary expressions [expr.prim]

7.5.8 Requires expressions [expr.prim.req]

7.5.8.1 General [expr.prim.req.general]

1

#

A requires-expression provides a concise way to express requirements on template arguments that can be checked by name lookup or by checking properties of types and expressions.

requires-expression:
requires requirement-parameter-listopt requirement-body

requirement-parameter-list:
( parameter-declaration-clause )

requirement-body:
{ requirement-seq }

requirement-seq:
requirement requirement-seqopt

requirement:
simple-requirement
type-requirement
compound-requirement
nested-requirement

2

#

A requires-expression is a prvalue of type bool whose value is described below.

3

#

[Example 1:

A common use of requires-expressions is to define requirements in concepts such as the one below:templateconcept R = requires (T i) {typename T::type; {*i} -> std::convertible_to<const typename T::type&>; };

A requires-expression can also be used in arequires-clause ([temp.pre]) as a way of writing ad hoc constraints on template arguments such as the one below:templaterequires requires (T x) { x + x; } T add(T a, T b) { return a + b; }

The first requires introduces therequires-clause, and the second introduces the requires-expression.

— end example]

4

#

A requires-expression may introduce local parameters using aparameter-declaration-clause.

A local parameter of a requires-expression shall not have a default argument.

The type of such a parameter is determined as specified for a function parameter in [dcl.fct].

These parameters have no linkage, storage, or lifetime; they are only used as notation for the purpose of defining requirements.

The parameter-declaration-clause of arequirement-parameter-list shall not terminate with an ellipsis.

[Example 2: templateconcept C = requires(T t, ...) { // error: terminates with an ellipsis t;};templateconcept C2 = requires(T p[2]) {(decltype(p))nullptr; // OK, p has type “pointer to T''}; — end example]

5

#

The substitution of template arguments into a requires-expression can result in the formation of invalid types or expressions in the immediate context of its requirements ([temp.deduct.general]) or the violation of the semantic constraints of those requirements.

In such cases, the requires-expression evaluates to false; it does not cause the program to be ill-formed.

The substitution and semantic constraint checking proceeds in lexical order and stops when a condition that determines the result of the requires-expression is encountered.

If substitution (if any) and semantic constraint checking succeed, the requires-expression evaluates to true.

[Note 1:

If a requires-expression contains invalid types or expressions in its requirements, and it does not appear within the declaration of a templated entity, then the program is ill-formed.

— end note]

If the substitution of template arguments into a requirement would always result in a substitution failure, the program is ill-formed; no diagnostic required.

[Example 3: template concept C =requires {new decltype((void)T{}); // ill-formed, no diagnostic required}; — end example]