Depending on what you want to do with this lambda, you might consider the following alternative:
auto exec = [i = 0]() mutable { cout << ++i << ' '; }; exec(); // 1 exec(); // 2 auto exec2 = exec; // copy state of lambda exec2(); // 3 exec(); // 3
Using []() { static int i = 0; cout << ++i << ' '; }; []() { static int i = 0; cout << ++i << ' '; }; instead, prints the sequence 1 2 3 4 .
Living example
Hiura source share