6

I saw a piece of code like this:

Foo(
  [&](int a){
    ...
  }
);

Seems like an inner function, but why there is a [&]?

Allan Jiang
  • 10,075
  • 22
  • 96
  • 153
  • Looks like passing a lambda function with a capture to foo...http://www.cprogramming.com/c++11/c++11-lambda-closures.html – IdeaHat Feb 21 '14 at 19:26
  • Read about [anonymous functions (also known as lambda functions/expressions)](http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B_.28since_C.2B.2B11.29). – Some programmer dude Feb 21 '14 at 19:26
  • It's for C++11 lambda expressions. More information at http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 – andand Feb 21 '14 at 19:27

1 Answers1

4

This is a lambda, the [&] means that:

captures all automatic variables mentioned in the body of the lambda by reference

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682