C ++ 11 lambda and square brackets

Looking at this lambda pattern:

[](int factor)->int{return factor*factor;}

Can someone explain to me that the square brackets before the C ++ 11 lambda expression are good for?

+5
source share
1 answer

Square brackets indicate which variables are “captured” by the lambda, and how (by value or by reference).

Capture means you can reference a variable outside of lambda from inside lambda. When capturing by value, you get the value of the variable during the creation of the lambda - similar to passing a parameter to a function by value. If the capture is by reference, you will have a reference to the actual variable outside of the lambda (and you need to make sure that it stays in the scope).

, "this", , .

+6

All Articles