警告C4626阻止我们从std:c++ 17迁移到std:c++ latest。从19.22开始(不是在19.21或更早版本中),几乎每个lambda都会发出它。
#pragma warning(default : 4626)
int main()
{
int foo;
auto bar = [&]() {
foo;
};
return 0;
}
<source>(8): warning C4626: 'main::<lambda_1>': assignment operator was implicitly defined as deleted
用/ std:c++ latest / W4编译
这个警告根本不是错误的,但是我认为这很冗长。当我们从显式/隐式删除的赋值运算符派生时,我们希望C4626保持启用状态,以获取警告。因为我们将每个警告都映射为一个错误,所以目前无法实现。
是否需要这种详细程度?还是可以将其描述为错误?
最佳答案
这是一个已知的错误,已经报告给MSVC开发人员团队。 current status是“固定的未决版本”(在版本16.5中)。
同时,可以使用#pragma warning(suppress:4626)
指令which disables the warning解决此问题,但仅适用于和下一个代码行:
suppress - Pushes the current state of the pragma on the stack, disables the specified warning for the next line, and then pops the warning stack so that the pragma state is reset.
另外,也可以使用MSVC
__pragma()
语法,有条件地定义一个ENDLAMBDA
(或任何您想调用的宏),如下所示(在顶级“.pch-forming” header 中只需一次):#if (__cplusplus > 201703L) // Using the "/std:c++latest" option...
#define ENDLAMBDA __pragma(warning(suppress:4626))
#else // Don't need it for earlier standards:
#define ENDLAMBDA
#endif
然后,在任何lambda表达式中,只需在结束
}
之前添加此宏即可;因此,在您的示例中,您可以这样做:int main()
{
int foo;
auto bar = [&]() {
foo;
ENDLAMBDA };
return 0;
}
我承认这有点笨拙-但是它将一直用作临时的权宜之计,直到16.5发布为止,然后,搜索并删除所有出现的宏将相当容易。
关于c++ - MSVC 19.22在Lambda上发出C4626警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58117581/