The following code does not compile:
struct object
{
static constexpr auto f;
};
constexpr auto object::f = [](auto&& x){x += 1;};
neither this one:
struct object
{
static constexpr auto f = [](auto&& x){x += 1;};
};
but this does (when f is not a member):
static constexpr auto f = [](auto&& x){x += 1;};
Is there a way to declare and define a static constexpr data member lambda in C++14?
The rule on static data members is in [class.static.data]:
Only
static constintegral/enumeration types orconstexprmembers can be defined within the class definition. C++14 disallowedconstexprlambdas period. The wording in [expr.const] used to read:So in C++14, you just can't have a static lambda data member - you can't define it inline and you can't define it out of line because you have no way of declaring it (variables declared with
autorequire an initializer). You're out of luck.In C++17, we can have
constexprlambdas thanks to p0170, at which point your second option is fine: