c++ - MSVC 19.22在Lambda上发出C4626警告

标签 c++ visual-studio-2019 c++20

警告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/

相关文章:

c++ - 如何在 C++ 中重载 "operator %"

c++ - 我正在编写一个 cpp 程序来打印两个数字之间的所有质数。程序运行成功但未打印任何内容

c++ - 使用 Qt 时删除 CMake 3.14.0 中的 CMP0020 错误

c++ - 是否可以将花括号初始化列表作为模板参数传递?

c++ - 不能在 C++20 中将 std::cin 与 char* 或 char[] 一起使用

c++ - 为什么range::ostream_iterator默认可构造?

在客户端终止连接进程后,C++ 服务器不会关闭 TCP 套接字连接

c++ - 更改 volatile const 的值 - G++ 与 Visual Studio 2019

visual-studio - SSRS "member not found (exception from HRESULT: 0x80020003 DISP_E_MEMBERNOTFOUND"Visual Studio2019 社区

c# - 如何在不编辑文件的情况下仅在生成的代码中禁用编译器警告