Specifies if and what we capture. There are several forms to do so. There are two lazy variants:
- If we write [=] () {...}, we capture every variable the closure references from outside by value, which means that the values are copied
- Writing [&] () {...} means that everything the closure references outside is only captured by reference, which does not lead to a copy.
Of course, we can set the capturing settings for every variable individually. Writing [a, &b] () {...} means, that we capture the variable a by value, and b by reference. This is more typing work, but it's generally safer to be that verbose because we cannot accidentally capture something we don't want to capture from outside.
In the recipe, we defined a lambda expression as such: [count=0] () {...}. In this special case, we did not capture any variable from outside, but we defined a new one called count. Its type is deduced from the value we initialized it with, namely 0, so it's an int.
It is also possible to capture some variables by value and some, by reference, as in:
- [a, &b] () {...}: This captures a by copy and b by reference.
- [&, a] () {...}: This captures a by copy and any other used variable by reference.
- [=, &b, i{22}, this] () {...}: This captures b by reference, this by copy, initializes a new variable i with value 22, and captures any other used variable by copy.