3 ms·
Lambdas. They added it a while back. [things-to-capture](params) -> return-type { function-body; } The return type is optional [0] since it can be detected
by Jtsummers 6y ago
Lambdas. They added it a while back.
[things-to-capture](params) -> return-type { function-body; }
The return type is optional [0] since it can be detected automatically. The things-to-capture have a couple variants:
[&foo]...
Captures variable foo by reference.
[foo]...
Makes a copy of foo.
You can also capture "everything" using [&] or [=]. Handy if you don't want to make an explicit list of captured variables. You can also mix it:
[&foo, bar]...
Captures foo by reference and a copy of bar.
https://en.cppreference.com/w/cpp/language/lambda https://en.cppreference.com/w/cpp/language/lambda
[0] Additionally, lambdas may not have a return value. Like this one:
int n = 0;
auto counter = [&n] () { n++; }
counter();
// now n == 1