7
假设我有以下几点。Lambda表达式偷偷`捕获``
struct A
{
int x;
std::function<int()> f1() { return [=](){ return x; }; }
std::function<int()> f2() { return [=](){ return this->x; }; }
std::function<int()> f3() { return [this](){ return x; }; }
std::function<int()> f4() { return [this](){ return this->x; }; }
std::function<int()> f5()
{
int temp = x;
return [=](){ return temp; };
}
}
现在我有以下代码。
auto a = std::make_shared<A>();
a->x = 5;
std::function<int()> f = a.f#();
a.reset();
int x = f();
其中f#
指的是任何的f1, f2, f3, f4, f5
。
这些功能在两套一个展示行为:
- 回报5称为(
f5
)时,或 - 坠毁试图取消引用
nullptr
(f1, f2, f3, f4
)。
据我所知,这是因为有些人正在隐式或显式地在成员函数A
中捕获“this
”。
确定行为1或2的正式规则是什么?
我花了一段时间处理,是由类似于f1
东西造成的错误,以为它会捕捉x
从不认为它会捕捉this
,所以我想这将是获得这个文件非常有用。
噢 - 我把这两个组标记为'f1,f2,f3,f4'和'f5'!是的,我的意思是说'f5'能够正常工作,而其他人则不能。编辑。 – 2013-03-25 06:36:45