Consider this:
class TestLambda {
public:
std::vector<char> data;
};
void test_lambda(TestLambda& obj) {
[=]() mutable {
obj.data.push_back(0x01);
}();
}
int main() {
TestLambda tst;
tst.data.push_back(0x99);
test_lambda(tst);
return 0;
}
After the call, test_lambdaI expected to see the change in tst.data, but it is not. To see the changes, I had to create a lambda, again passing the link obj, i.e. [&obj]().
Why do we need this? I mean, link again?
objis already a link. Then lambdacaptures obj, copying it. So, objinside is lambdanot a link? Why?
Can someone explain this to me? Thanks.
source
share