c++c++20concept

C++ Concepts Compound Requirements without noexcept and return-type-requirement vs Simple Requirements


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

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

Would there be a difference between C1 vs C2 functionally?

Edit: grammar


Solution

  • They are equivalent. The standard even has an example of it:

    template<typename T> concept C1 = requires(T x) {
     {x++};
    };
    

    The compound-requirement in C1 requires that x++ is a valid expression. It is equivalent to the simple-requirement x++;.

    Compound requirements are able to test some aspect of the expression such as whether it is noexcept or if the resulting type fulfills some concept. But if neither of those tests are present, it just substitutes the template parameters into the expression, verifies its validity, and calls it a day.