如果lambda函数IC C 是由函数实现的,为什么这是不可能的?
#include <iostream>
class A
{
public:
int a;
void f1(){ [](){std::cout << this << std::endl ;}();};
};
int main()
{
A a;
a.f1();
}
我有错误 9:34: error: 'this' was not captured for this lambda function
。如果我理解正确的话,如果将lambda实现为函子类,为什么不可能获得它的内部 this ?
编辑: functor类的,而不是类别A的实例。
来自lambda:
出于名称查找的目的,确定 该指针和访问非静态类成员的身体 在上下文中考虑了关闭类型的函数调用操作员 lambda-expression。
struct X {
int x, y;
int operator()(int);
void f()
{
// the context of the following lambda is the member function X::f
[=]()->int
{
return operator()(this->x + y); // X::operator()(this->x + (*this).y)
// this has type X*
};
}
};
所以,不可能按照您的意愿引用 this
。