C++ Lambda Captures

The difference is how the values are captured

  • & captures by reference
  • = captures by value
  • Example:

    int x = 1;
    auto valueLambda = [=]() { cout << x << endl; };
    auto refLambda = [&]() { cout << x << endl; };
    x = 13;
    valueLambda();
    refLambda();

    This code will print:

    1
    13

    https://stackoverflow.com/questions/21105169/is-there-any-difference-betwen-and-in-lambda-functions
    https://en.cppreference.com/w/cpp/language/lambda

    Comments are closed.