正如论文P0409R2中提出的那样
我期望 x 的定义从 C++20 中被弃用并且无法编译,但它似乎在 g++ (GCC) 8.1.0 中工作有谁知道我是否做错了什么?
在 Visual Studio 2017 中,由于 y 的定义出现错误而无法编译。错误 C3791:当默认捕获模式为复制 (=) 时,无法显式捕获“this”
#include <iostream>
struct X {
void f()
{
int value = 3;
auto x = [=] { // Deprecated from C++20:
return value + g();
};
auto y = [=, this] { // Recommended method from C++20:
return value + g(); // [=] The this pointer will not be captured, so capture with specifying this
};
}
int g() const
{
return 2;
}
};
int main()
{
X().f();
}