【发布时间】:2016-02-05 18:50:37
【问题描述】:
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();
这段代码将在 gcc 4.6.3 中编译,它会正确地复制成员变量z,并且两个打印都会得到2。在 gcc 4.8.2 中,这不再编译了..
error: 'this' was not captured for this lambda function
我想知道为什么这个功能被删除了,因为它非常有用。
【问题讨论】: