Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ [&] operator [duplicate]

Tags:

c++

c++11

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;
      }
   });
}
like image 326
T-student Avatar asked Sep 04 '12 10:09

T-student


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

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
like image 100
pmr Avatar answered Sep 22 '22 18:09

pmr


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.

like image 28
hmjd Avatar answered Sep 23 '22 18:09

hmjd