Why does a variable become 'const' when I use a [=] capture (using Lambda class)? Why does a variable become 'const' when I use a [=] capture (using Lambda class)? multithreading multithreading

Why does a variable become 'const' when I use a [=] capture (using Lambda class)?


If you do a capture by value in a lambda, you will get a 'member' which gets stored. As the default operator() is a const function, you cannot modify them.

Lambdas can be defined as []() mutable {} to allow you to modify the local variables.

By capturing the value by reference, you have something which behaves like a const pointer to a non-const object, so without the mutable, you can adapt those objects. (Unless they already where const)


Captured variables are indeed const in the scope of the lamba:

[foo](){   // foo, whatever that is, is const}

In a mutable lambda, the captured variables are not constant:

[foo]()mutable {   // Lambda can modify foo, but it's a copy of the original   // captured variable}