I’m trying to use a lambda as a static member, like this:
struct A
{
static constexpr auto F = [](){};
};
int main()
{
A::F();
return 0;
}
Is this even correct C++11 code? On clang, I get this error:
error: constexpr variable 'F' must be initialized by a constant
expression
static constexpr auto F = [](){};
^~~~~~
It seems in clang, lambdas aren’t considered a constant expression. Is this correct? Perhaps they haven’t fully implemented lambdas yet in clang because gcc 4.7 seems to allow it as a constexpr, but it give another error:
error: ‘constexpr const<lambda()> A::F’, declared using local type ‘const<lambda()>’, is used but never defined
I’m not sure, I understand what that means. It seems to correctly deduce the type of the lambda, but it only declares it and not define it. How would I go about defining it?
This code is ill-formed. A
constexprvariable is required to be initialized by a constant expression, and[expr.const]p2says:GCC is therefore incorrect to accept this code.
Here’s one way to give a class a static data member of lambda type: