【发布时间】:2019-09-13 04:23:01
【问题描述】:
class A{
public:
void do_something(std::function<void()> const& f) {
}
};
class B: public A{
public:
int x = 0;
void do_another_thing(){
do_something([x]{});
}
};
它说 x 不是一个变量:
16:20: error: capture of non-variable 'B::x'
14:13: note: 'int B::x' declared here
为什么它不能与类成员一起使用,但它可以与定义在 do_another_thing() 中的变量一起使用?
【问题讨论】:
-
添加到上面的链接,这里是 iso std 参考:- §5.1.2 Lambda 表达式 #13 “...如果 lambda 表达式捕获一个实体并且该实体未在立即包含 lambda 表达式或函数,程序格式错误。..."
标签: c++