1

I saw an snippet CC source such as.

imagecb = [ &, xx, yy, zz] (uint32_t a1, int a2) { 
   ... 
}
  1. I did know what is symbol of & in the array
  2. this syntax declare an inline functions with 2 arguments a1, a2, right?
Robber Pen
  • 1,033
  • 2
  • 12
  • 24
  • See [Lambda expressions (since C++11)](https://en.cppreference.com/w/cpp/language/lambda) under **Lambda capture** – David C. Rankin Sep 28 '21 at 07:05
  • In this context it is a lambda capture, it says capture all local variables by reference and captur xx,yy,zz by value. These captured variables can then be used in the scope of the lambda function. – Pepijn Kramer Sep 28 '21 at 07:18

1 Answers1

1

it is not the array, it is a lambda expression. Between [] you pass elements from another scope that you need inside expression. & means to get all elements that you need by reference.

In this specific case, variables xx, yy, zz will be copied, but if let's say this lambda expression uses another variable like for example aa it will be passed by reference.

It is worth mentioning that even if the element is passed by reference to a lambda expression, it cannot be modified (in default). To allow modification you need to use keyword mutable

Little code sample:
 int xx=0, yy=0, zz=0;
    int aa=1;
    auto imagecb = [&, xx, yy, zz](uint32_t a1, int a2) mutable {
        cout << aa;
        aa++;
        return aa;
    };
    cout << endl<<imagecb(2, 3);
Michał Turek
  • 701
  • 3
  • 21
  • The link uses slightly different language to say essentially the same thing "`&` (implicitly capture the used automatic variables by reference)" The only qualification it adds is limiting to automatic variables. – David C. Rankin Sep 28 '21 at 07:10