Lambda's operator() is implicitly constexpr according to https://en.cppreference.com/w/cpp/language/lambda
When this specifier (
constexpr) is not present, the function call operator or any given operator template specialization will beconstexpranyway, if it happens to satisfy allconstexprfunction requirements
And a requirement of a constexpr-function according to https://en.cppreference.com/w/cpp/language/constexpr
there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression (for constructors, use in a constant initializer is sufficient) (since C++14). No diagnostic is required for a violation of this bullet.
In the next example, the function t() always throws an exception by calling lambda l():
auto l = []()->bool { throw 42; };
constexpr bool t() { return l(); }
GCC rejects this function with the error:
call to non-'constexpr' function '<lambda()>'
but Clang accepts the program (until the function t() is used in a constant evaluation), meaning that it considers l() a constexpr-function, demo: https://gcc.godbolt.org/z/j1z7ee3Wv
Is it a bug in Clang, or such compiler behavior is also acceptable?