Lambda Functions and Constexpr
Question: Is it possible to use constexpr with lambda functions in C ?
In the following code example, the compiler reports an error related to requiring a constexpr function:
<code class="cpp">struct Test { static const int value = []() -> int { return 0; } (); };</code>
Answer:
Update (C 17): As of C 17, lambda functions are allowed in constant expressions.
Pre-C 17: Lambdas are not currently permitted in constant expressions in C 14. However, a proposal (N4487) has been made to allow certain lambda expressions and closure objects to appear in constant expressions.
If this proposal is accepted, it would introduce the following changes:
This change would align with the behavior of implicitly defined constructors and assignment operator functions, which can be inferred as constexpr when appropriate.
The above is the detailed content of Can Lambda Functions Be Used with `constexpr` in C ?. For more information, please follow other related articles on the PHP Chinese website!