Static array of lambda functions (C++)

When you construct constexpr object, everything you pass into it needs to be a core constant expression, [decl.constexpr]/9:

A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. If it is initialized by a constructor call, that call shall be a constant expression (5.19).

and, from [expr.const] lambdas are not constant expressions1:

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

  • [...]
  • a lambda-expression (5.1.2);
  • [...]

However, that applies only to constexpr and not to const, so you could simply do that instead:

static const MyStruct ops[6] = {
    {'+', [] (double a, double b) { return a+b; } },
    {'-', [] (double a, double b) { return a-b; } },
};

Note: your lambdas don't need to capture anything, so you should just empty the capture list [].


1As dyp points out, there is a proposal to change this: N4487

capturing lambda cannot decay to function pointer.

and operator to return the function pointer from a (non-capturing) lambda is not constexpr.

Tags:

C++

Lambda

C++11