c++syntaxrequirements

Meaning of the { } -> "operator" in C++


My activities in C++ have been dormant for a while, so maybe my question isn't appropriate.

I have seen in the definition of concepts a 'strange' syntax like e.g.: {x + 1} -> std::same_as<int>;

Is there a name for the { } -> "operator", resp. when has it been introduced into the standard and for what purpose?
I understand its meaning in the context of defining requirements but would appreciate some further information.


Solution

  • Meaning of the { } -> "operator" in C++

    It is not a single operator but what is called a compound requirement and has the form:

    { expression } noexcept(optional) return-type-requirement (optional) ;    
    
    return-type-requirement   -   -> type-constraint
    
    

    and asserts properties of the named expression. Substitution and semantic constraint checking proceeds in the following order:

    1. Template arguments (if any) are substituted into expression;
    2. If noexcept is used, expression must not be potentially throwing;
    3. If return-type-requirement is present, then: a) Template arguments are substituted into the return-type-requirement; b) decltype((expression)) must satisfy the constraint imposed by the type-constraint. Otherwise, the enclosing requires-expression is false.

    (emphasis mine)

    This means that decltype((x+1)) must satisfy the constraint imposed by std::same_as<int> which essentially means that the type deduced by/for the expression x+1 should evaluate to an int.