c++lambdac++20requires-expression

Weird return value of C++20 requires expression


Consider the requires expression:

constexpr auto b = requires { []{}; };

GCC pass the following two static_assert:

 static_assert(b);
 static_assert(!b); 

This looks extremely weird. I expect that the value of b should be true. Is this just a GCC Bug?


Solution

  • This isn't Schrödinger's Requirement: b should clearly be either true or false.

    The requirement here:

    constexpr auto b = requires { []{}; };
    

    is a simple-requirement. We're just checking that if the expression []{} is a valid expression. And... it is! So b should just be true. This is a gcc bug (and doubly so with StoryTeller's example demonstrating that in a slighly different spelling b doesn't even count as a constant expression).