Quick Q: What's the difference between lambda [x]()mutable{++x;} and [&x](){++x;}? -- StackOverflow

Quick A: [x] captures a copy, [&x] remembers a reference to the original variable.

C++ Lambdas: Difference between “mutable” and capture-by-reference

In C++ you can declare lambdas for example like this:

int x = 5;
auto a = [=]() mutable { ++x; std::cout << x << '\n'; };
auto b = [&]()         { ++x; std::cout << x << '\n'; };

Both let me modify x, so what is the difference?

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.