wai*_*r92 1 c++ lambda this c++11
如果lambda函数ic C++是由functor实现的,为什么这不可能?
#include <iostream>
class A
{
public:
int a;
void f1(){ [](){std::cout << this << std::endl ;}();};
};
int main()
{
A a;
a.f1();
}
Run Code Online (Sandbox Code Playgroud)
我收到错误9:34: error: 'this' was not captured for this lambda function.如果我的理解是正确的,如果拉姆达为函数子类实现的,为什么它是不可能得到它单曲内部这个?
编辑:该函子类,而不是这个类A的实例
来自lambda:
出于名称查找,确定this指针的类型和值以及访问非静态类成员的目的,闭包类型的函数调用操作符的主体在lambda表达式的上下文中被考虑.
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*
};
}
};
Run Code Online (Sandbox Code Playgroud)
所以,你无法this按照自己的意愿参考.
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |