是否可以通过引用捕获上下文以及this使用 lambda 函数捕获指针?
看来下面的代码不起作用。我怎样才能做到这一点?
[&, this] () { }
Run Code Online (Sandbox Code Playgroud)
它“工作”得很好 m8:
#include <iostream>
struct T
{
int y;
T() : y(0)
{
int x = 0;
[&, this](){ x = 1; y = 2; }();
std::cout << x << ' ' << y << '\n'; // 1 2
}
};
int main()
{
T t;
}
Run Code Online (Sandbox Code Playgroud)
实际上指定 是多余的this,因为&已经捕获了它。