I was checking some old code that I tried with lambda and I noticed. If I specify the lambda capture as copy and modify the value inside it it will modify the variable as it was a reference.
void classV::setLambda() {
r.setLambda([=]() {
value = 100;
v = 10;
std::cout << &value << std::endl;
});
}
void classV::executeLambda() {
r.runLambda();
std::cout << "NVAL: " << value << std::endl;
std::cout << "NVAL: " << v << std::endl;
std::cout << &value << std::endl;
}
Basically, this code is storing a lambda on an external object (class instance) and being executed there, I imagined that, specifying the capture as copy, when I tried to get the value of those variables those should not change.... as it was copied.
But, this is not the case, both value and v are 100 and 10. Just to be specific, value is an static int of the class classV, and v is an int object attribute of the classV.
Why are those values being changed?, I checked the addresses inside the lambda and the executeLambda and they are the same, shouldn't it only happen if it was captured by reference? Probably I am missing something here.