c++windowsconcurrencytaskppl

Explain the "=" sign parameter in this Concurrency::Task call


Can someone explain to me what the '=' parameter is used for in this code? What other parameters may I use instead of =? What difference would it make? MSDN isn't very clear on the subject.

//Declaration
auto prerequisite = task<void>([](){});

//Here is where I don't understand the '=' parameter
prerequisite.then([=](task<void> prerequisite){/*custom code goes here*/})

Solution

  • Inside the lambda introducer (brackets at the beginning of a lambda), you can specify a capture to access data of outer scope that is not passed as an argument:

    • [=] means that the outer scope is passed to the lambda by value. Thus, you can read but not modify all data that was readable where the lambda was defined.

    • [&] means that the outer scope is passed to the lambda by reference. Thus, you have write access to all data that was valid when the lambda was defined, provided that you had write access there.