Monday, July 17, 2017

C++11: Lambda Expressions: Capture List

In a lambda expression, “[]” and it’s contents define the capture list.
The following are some variations of the capture list:
[] captures nothing
[&] captures all local variables by reference
[=] captures all local variables by value
[this] captures the current object by reference
[a] captures local variable a by copy
[&a] captures local variable a by reference
Here is an example:
#include
int main()
{
  int local1 = 3;
  int local2 = 4;
  [&](int param1, int param2) {std::cout << param1 << " " << param2 << " "
                                         << local1 << " " << local2 << std::endl;}(1, 2);
  return 0;
}
// Output: 1 2 3 4



No comments:

Post a Comment