How can I run a lambda immediately instead of storing it and then run it?
Instead of storing the lambda like this:
auto lambda = [&](){ std::cout << ++x << '\n'; }
I am trying to run it immediately like this:
[&](){ std::cout << ++x << '\n'; }
But that gives me this error message:
Warning: expression result unused
You can invoke the lambda immediately by placing parenthesis at the end like shown below:
int x = 0;
[&]{ std::cout << ++x << '\n'; }();
// ^^
Now this will print out 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With