Is constexpr supported with lambda functions / expressions?

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).

Leave a Comment