In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr.
Are there special rules that apply to constexpr for capturing?
int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); }
xwas markedNOK, and I didn't understand until I read the answer. Hopefully this makes it clearer. If you disagree, feel free to roll back.