For a lambda function, you can use the default capture if you have many variables to capture from the scope of the lambda definition.
Type | Capture Syntax | Captured Variable |
---|---|---|
default capture by value | [=] |
Access only inside the lambda. It cannot change even in the lambda. Think of this as a const . |
default capture by reference | [&] |
Access and change inside the lambda and the main program. |
This example shows the variations and the possible effects on the capture variables:
You can use the mutable
specifier to allow changes to a captured variable inside the lambda definition with the default capture by value, i.e., [=]
. See this in the example above.
From the design perspective, only use these if you pass a high percentage of the variables in scope (see Stamp Coupling). Otherwise, capture individual variables.