【问题标题】:Visual C++ Compiler OptionsVisual C++ 编译器选项
【发布时间】:2018-08-13 20:54:31
【问题描述】:

我需要确保当发生隐式转换时,编译器应该抛出警告/错误,如下面的代码:

int32_t y = 9;
uint32_t x = y;
y = -1;
x = y;

在 gcc 中,我可以同时使用 -Wconversion -Wsign-conversion 编译器标志来报告此类问题 - VC++ 构建是否有类似的选项?

【问题讨论】:

    标签: visual-studio visual-studio-2010 visual-c++ visual-studio-2012


    【解决方案1】:

    Compiler Warnings That Are Off by Default

    • C4287(3 级)“运算符”:无符号/负常数不匹配

    • C4365(级别 4)“操作”:从“type_1”转换为“type_2”,有符号/无符号不匹配

    • C4388(4 级)有符号/无符号不匹配

    • C4287(3 级)“运算符”:无符号/负常数不匹配

    还有其他与截断有关。

    通常,您可以通过使用 /Wall 打开所有警告来使用这些警告,然后根据您的代码建立一个抑制列表,因为其中很多都非常嘈杂。

    例如,在DirectX Tool Kit for DX11 中,我在pch.h 中使用/Wall 和以下抑制:

    // VS 2013 related Off by default warnings
    #pragma warning(disable : 4619 4616 4350 4351 4472 4640 5038)
    // C4619/4616 #pragma warning warnings
    // C4350 behavior change
    // C4351 behavior change; warning removed in later versions
    // C4472 'X' is a native enum: add an access specifier (private/public) to declare a WinRT enum
    // C4640 construction of local static object is not thread-safe
    // C5038 can't use strictly correct initialization order due to Dev12 initialization limitations
    
    // Off by default warnings
    #pragma warning(disable : 4061 4265 4365 4571 4623 4625 4626 4628 4668 4710 4711 4746 4774 4820 4987 5026 5027 5031 5032 5039)
    // C4061 enumerator 'X' in switch of enum 'X' is not explicitly handled by a case label
    // C4265 class has virtual functions, but destructor is not virtual
    // C4365 signed/unsigned mismatch
    // C4571 behavior change
    // C4623 default constructor was implicitly defined as deleted
    // C4625 copy constructor was implicitly defined as deleted
    // C4626 assignment operator was implicitly defined as deleted
    // C4628 digraphs not supported
    // C4668 not defined as a preprocessor macro
    // C4710 function not inlined
    // C4711 selected for automatic inline expansion
    // C4746 volatile access of '<expression>' is subject to /volatile:<iso|ms> setting
    // C4774 format string expected in argument 3 is not a string literal
    // C4820 padding added after data member
    // C4987 nonstandard extension used
    // C5026 move constructor was implicitly defined as deleted
    // C5027 move assignment operator was implicitly defined as deleted
    // C5031/5032 push/pop mismatches in windows headers
    // C5039 pointer or reference to potentially throwing function passed to extern C function under - EHc
    
    // Windows 8.1 SDK related Off by default warnings
    #pragma warning(disable : 4471 4917 4986 5029)
    // C4471 forward declaration of an unscoped enumeration must have an underlying type
    // C4917 a GUID can only be associated with a class, interface or namespace
    // C4986 exception specification does not match previous declaration
    // C5029 nonstandard extension used
    

    随着时间的推移跟上编译器的变化确实需要维护成本,这也是我让 cmet 提醒我每个 cmet 的用途的部分原因。

    【讨论】:

    • 谢谢 - 请告诉我如何启用所需的关闭标志?
    • /Wall 是我上面提到的命令行。在 VC++ 项目设置中,您还可以将警告级别设置为“全部”。您应该按照我的答案中的 MSDN 链接了解更多信息。
    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 2018-07-26
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多