Home > Article > Backend Development > Can Lambda Functions Be Used in `constexpr` Contexts?
constexpr Support for Lambda Functions
constexpr support for lambda functions has been a widely discussed topic in the C community. Although lambdas are currently not allowed in constant expressions according to [expr.const]/(2.6), N4487, which is included in the working draft N4582, proposes to remove this restriction.
Proposed lambda-related changes:
Example:
The following example will be valid once N4487 is accepted:
<code class="c++">struct Test { static const int value = []() constexpr { return 0; } (); };</code>
Workaround:
As a workaround before constexpr support is officially added, you can use a function template instead of a lambda:
<code class="c++">struct Test { template <typename> static const int value = 0; };</code>
The above is the detailed content of Can Lambda Functions Be Used in `constexpr` Contexts?. For more information, please follow other related articles on the PHP Chinese website!