struct myclass {
myclass(){}
myclass(int qx):z(qx){ }
std::function<void()> create() {
auto px = [z](){
std::cout << z << std::endl;
};
return px;
}
int z;
};
myclass my;
my.z = 2;
auto func = my.create();
func();
my.z = 3;
func();
This code will be compiled in gcc 4.6.3, which will correctly make a copy of the member variable z, and both print will receive 2. In gcc 4.8.2, this no longer compiles ..
error: 'this' was not captured for this lambda function
I wonder why this functionality was removed, as it was quite useful.
source
share