22

hello i have found this code on windows documentation

but i don't get what means

[&]

just please can you clear me what it should do ??

it is not c++ standard true??

This is the code:

void parallel_matrix_multiply(double** m1, double** m2, double** result, size_t size)
{
   parallel_for (size_t(0), size, [&](size_t i)
   {
      for (size_t j = 0; j < size; j++)
      {
         double temp = 0;
         for (int k = 0; k < size; k++)
         {
            temp += m1[i][k] * m2[k][j];
         }
         result[i][j] = temp;
      }
   });
}
KillianDS
  • 16,936
  • 4
  • 61
  • 70
FrankTan
  • 1,626
  • 6
  • 28
  • 63

6 Answers6

37

It introduces a lambda expression. The contents of the square brackets indicate what is to be captured inside the lambda. Having only a & in there means that everything that is mentioned inside the lambda and can be found outside of its scope is captured by reference.

Example:

int a = 0;
auto l = [&]() { 
  ++a; // a refers to the a outside of this scope through a reference
}
l(); // execute the lambda
pmr
  • 58,701
  • 10
  • 113
  • 156
  • this is a nice example... i like the Comment inside the function it makes more clear the & meaning – FrankTan Sep 04 '12 at 11:14
  • So if i write [=] and i edit the a variable then ouside the lambda function the value is changed ? or it is not because was not passed by reference ? – FrankTan Sep 04 '12 at 11:15
  • @frank: if you just change & to = it won't compile because you cannot mutate copied variables unless you explicitly say that you intend to do so with the mutable keyword. – sellibitze Sep 04 '12 at 13:14
  • thx for the answer but in the end after the lambda execution when a is mutable the value will not change right? – FrankTan Sep 04 '12 at 13:38
22

It is a C++11 feature and is called the lambda capture clause. In this case, the [&] makes available to the lambda function all of the arguments to the parallel_matrix_multiply() function by reference.

See lambda functions for more information.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • "makes all of the arguments of the `parallel_matrix_multiply() function` available by reference to the lambda function"? – Steve Jessop Sep 04 '12 at 11:22
  • 1
    @SteveJessop, the `[&]` makes all variables in scope available in the body of the lambda function by reference, including the arguments to the function `parallel_matrix_multiply()`. Is this incorrect or just confusingly phrased? – hmjd Sep 04 '12 at 11:23
  • It's correct (as far as I understand it: I don't know whether there are any confusing edge cases). My comment was just trying to make the sentence a bit less unwieldy, sorry if I didn't convey that. – Steve Jessop Sep 04 '12 at 11:26
10

It is feature of C++11 standard. lambda

ForEveR
  • 55,233
  • 2
  • 119
  • 133
10

This is the capture clause used for lambda expressions. A lambda expression can have access to all variables of it's enclosing scope (i.e. if the lambda is within a function it can have access to all variables inside that function). The [&] means that the lambda will get all the variables by reference. They can also be taken by value, in that case you would use [=]. you can also specify concrete variables that need to be taken in a specific way, for example this code:

[&X, =]

means that lambda will take the X variable by reference, and all others by value.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • So if i write [=] and i edit a variable .... then ouside the lambda function the value is changed ? i suppose not .. right? – FrankTan Sep 04 '12 at 11:20
  • @frank, yes, if you write [=], the value outside the lambda will stay the same, no matter what you do. – SingerOfTheFall Sep 04 '12 at 11:24
  • It's equivalent to passing a variable by value to a regular function: the caller's copy doesn't change. You can easily confirm this yourself with a toy example. – Useless Sep 04 '12 at 11:25
  • Correct me if I'm wrong, but according to [here](https://stackoverflow.com/questions/30217956/error-variable-cannot-be-implicitly-captured-because-no-default-capture-mode-h), lambda functions have *no* access to variables of their enclosing scope by default. – E. Kaufman May 15 '21 at 09:05
6

[&] means that you will have access to variables from surrounding code inside lambda expression, and it will be access by referenc (i.e. you can modify them). In the example code, you can see that m1, m2, result and size are used in lambda experssion even though they are not passed as parameters.

Alex1985
  • 658
  • 3
  • 7
  • 3
    Technically you can't modify the variables either way, since lambdas which are not explicitely specified as `mutable` have a `const` `operator()`, so captured variables can't be changed. For `mutable` lambdas the variables can be changed in both cases, however the change is visible outside the lambda only for capture by reference – Grizzly Sep 04 '12 at 12:56
4

This is a Lambda function, C++ 11 feature

[&](size_t i){...}

will act like a callback or functor. So you don't need to write another function somewhere else to pass to parallel_for.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146