-
c++11 lambda question
I would like some clarification on lambda.
In the code below, I thought that using "mutable" will let me modify the captured variables. But the lambda function f2() doesn't seem to work that way.
Is the code below wrong? If it does, shouldn't there be a compile error?
Code:
int x = 1;
int y = 50;
int z = 3;
auto f2 = [=] () mutable {++x ;return x+y+z;};
std::cout << "f2() = " << f2() << std::endl; // print 55
std::cout << "x = " << x << std::endl; // print 1
-
O_o
The `mutable' allows you to mutate variables, but you aren't passing those variables by reference.
Soma
-
Thanks for clarifying. I misunderstood the purpose of mutable. I thought that using mutable would allow me to modify the variable outside of the lambda scope without having to pass the references.