The default member functions of the Lambda call operator are const . If you want to work with a mutable call, say mutable :
auto fn = [=]() mutable { // ^^^^^^^ v.push_back("world"); };
With the default const value, you should be explicit about the fact that you want to grab a copy of the vector and change that copy, not the original vector v .
In contrast, variables that are written by reference can be changed using constant member functions:
auto fn = [&]() { // ^^^ v.push_back("world"); // modifies original "V"! };
(This is essentially because const T same as T , when T = U & , there are no "constant references" in C ++.)
source share