Syntax meaning: [&](std::size_t, std::size_t t)

What does the following syntax mean? [&](std::size_t, std::size_t t)
Last edited on
It's a lambda function. The [&] means it is capturing the current scope so that you can use local variables inside the function.

A quick example:

1
2
3
4
5
6
7
8
9
10
11
void print( std::function<int()> func )
{
  cout << func() << endl;  // print whatever the function returns
}

int main()
{
  int foo = 100;

  print(  [&] () { return foo; } );  // prints 100
}


A conceptually similar thing would be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
// NOTE:  NOT REAL C++, only here to illustrate the concept

int main()
{
  int foo = 100;

  int temp_function()
  {
    return foo;
  }

  print( temp_function );
}
It is part of the C++ construction named lambda expression. You can find examples of using lambda expressions at this reference http://cpp.forum24.ru/?1-3-0-00000033-000-0-0-1343493920
Though it is written in Russian but you can use the google translator
Topic archived. No new replies allowed.