[](){}--lambda function (anonymous function) in C++

1. Basic form

[capture](parameters)->return-type {body}
例如:
auto func = [](){ cout << "hello,world"; };

1. “[ ]” -- capture specifier, indicating the beginning of a lambda expression


  • [ ] -- Do not intercept any variables
  • [&] -- Intercept all variables in the external scope and use them as references in the function body
  • [=] --Intercept all variables in the external scope and make a copy for use in the function body
  • [=, &foo] -- Intercept all variables in the external scope and make a copy for use in the function body, but use references to the foo variable
  • [bar] -- Intercept the bar variable and copy a copy for use in the function body without intercepting other variables.
  • [x, &y] -- x is passed by value, y is passed by reference
  • [this] -- Intercept the this pointer in the current class. This option is added by default if & or = is already used.

2. "( )" -- parameter list, that is, the parameters of this anonymous lambda function

[](const DMatch &m1, const DMatch &m2) { return m1.distance < m2.distance; }

3. "return-type" -- return type, if there is no return type, omit

4. "{ }" -- function body part

Guess you like

Origin blog.csdn.net/weixin_44884561/article/details/128664564