iammilind
iammilind

Reputation: 69988

Is constexpr supported with lambda functions / expressions?

struct Test
{
  static const int value = []() -> int { return 0; } ();
};

With gcc-4.6 I get something like, error: function needs to be constexpr. I have tried multiple combinations of putting constexpr at various places, but no luck.

Is constexpr supported for lambda functions as well (irrespective of return type specified or not) ? What is the correct syntax ?

Any work around possible ?

Upvotes: 72

Views: 37936

Answers (5)

Adrian
Adrian

Reputation: 10911

I know that this is an old question, but no one specified a workaround for this. The workaround is to use a constexpr functor like so:

struct functor
{
    constexpr int operator()() { return 0; }
};

struct Test
{
  static const int value = functor{}();
};

Not optimal as it moves the code away from where it's used, but when you don't have many options, this is doable. I had to use something similar for a project still compiling with c++14. This simple case has been tested on c++11 here: https://godbolt.org/z/Gvxq19jsd

However, if you have c++17 or higher, using the described:

struct Test
{
  static constexpr int value = []() constexpr { return 0; }();
};

is a nicer option.

Upvotes: 1

user5560811
user5560811

Reputation:

FFWD to year 2018 :)

auto my_const_expression_lambda = []()
  constexpr -> bool
{
   return true ;
}

Since c++17

Upvotes: 16

Columbo
Columbo

Reputation: 60979

Update: As of C++17, lambdas are permitted in constant expressions.


Lambdas are currently (C++14) not allowed in constant expressions as per [expr.const]/(2.6), but they will once N4487 is accepted (which can be found in the working draft N4582):

This proposal suggests allowing lambda-expressions in constant expressions, removing an existing restriction. The authors propose that certain lambda-expressions and operations on certain closure objects be allowed to appear within constant expressions. In doing so, we also propose that a closure type be considered a literal type if the type of each of its data-members is a literal type; and, that if the constexpr specifier is omitted within the lambda-declarator, that the generated function call operator be constexpr if it would satisfy the requirements of a constexpr function (similar to the constexpr inference that already occurs for implicitly defined constructors and the assignment operator functions).

Upvotes: 42

Ralph Tandetzky
Ralph Tandetzky

Reputation: 23610

Prior to C++17 lambdas are not compatible with constexpr. They cannot be used inside constant expressions.

Starting with C++17 lambdas are constexpr where it makes sense. The proposal N4487 will be put into the C++17 standard. On his website Herb Sutter, chair of the ISO C++ committee, stated the following:

Lambdas are now allowed inside constexpr functions.

Upvotes: 21

James McNellis
James McNellis

Reputation: 355039

From the C++0x FDIS §7.1.5[dcl.constexpr]/1:

The constexpr specifier shall be applied only to the definition of a variable, the declaration of a function or function template, or the declaration of a static data member of a literal type.

A lambda expression is none of those things and thus may not be declared constexpr.

Upvotes: 29

Related Questions