C++ 使用#pragma warning push/pop 是暂时改变警告级别的正确方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4193476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is using #pragma warning push/pop the right way to temporarily alter warning level?
提问by sharptooth
Once in a while it's difficult to write C++ code that wouldn't emit warnings at all. Having warnings enabled is however a good idea. So it is often necessary to disable warnings around some specific construct and have them enables in all other pieces of code.
有时很难编写完全不会发出警告的 C++ 代码。然而,启用警告是一个好主意。因此,通常需要禁用围绕某些特定构造的警告,并在所有其他代码段中启用它们。
I've seen two ways of doing that so far.
到目前为止,我已经看到了两种方法。
The first one is to use #pragma warning( push )
and #pragma warning( pop )
:
第一个是使用#pragma warning( push )
和#pragma warning( pop )
:
#pragma warning( push )
#pragma warning( disable: ThatWarning )
//code with ThatWarning here
#pragma warning( pop )
The second is to use #pragma warning( default )
:
第二种是使用#pragma warning( default )
:
#pragma warning( disable: ThatWarning )
//code with ThatWarning here
#pragma warning( default: ThatWarning )
The problem I see in the second variant is that it discards the original warning level - the warning might have been off before that or its warning level might have been altered. Using default
would discard those alterations.
我在第二个变体中看到的问题是它丢弃了原始警告级别 - 警告可能在此之前已关闭,或者其警告级别可能已更改。使用default
将丢弃这些更改。
The first approach looks clean. Are there any problems with it? Are there any better ways to achieve the same?
第一种方法看起来很干净。它有什么问题吗?有没有更好的方法来实现相同的目标?
采纳答案by Goz
The first method is the best way to do it, IMO. I know of no problems with it.
第一种方法是最好的方法,IMO。我知道它没有问题。
Simply bear in mind that a #pragma is compiler specific so don't expect it to work on every compiler out there :)
请记住,#pragma 是特定于编译器的,所以不要指望它适用于所有编译器:)
回答by sharptooth
This will work with multiple compilers (and different versions of compilers).
这将适用于多个编译器(以及不同版本的编译器)。
Header "push"
标题“推”
#if defined(__clang__)
# pragma clang diagnostic push
#endif
#if defined(_MSC_VER)
# pragma warning(push)
#endif
#if defined(YOUR_FAVORITE_COMPILER)
# pragma your compiler push warning
#endif
Header "pop"
标题“流行”
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
Some warning
一些警告
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-variable"
# if __has_warning("-Wnew-special-warning")
# pragma clang diagnostic ignored "-Wnew-special-warning"
# endif
#endif
#if defined(_MSC_VER)
# pragma warning(disable: 4100) // unreferenced formal parameter
# if _MSC_VER > _MSC_SOME_VERSION
# pragma warning(disable: xxxx) // disable one more for special version
# endif
#endif
Usage
用法
// This code reports warnings
// ...
#include <ignore_compiler_warning/push>
#include <ignore_compiler_warning/warning_type_1>
#include <ignore_compiler_warning/warning_type_2>
#include <ignore_compiler_warning/warning_type_3>
// This code ignores warnings type_{1,2,3}
// ...
#include <ignore_compiler_warning/pop>
// Back to reporting warnings
// ...
Additionally include guards can check that there is no double push/pop/disable-warning pragmas.
此外,包括守卫可以检查是否没有双重推送/弹出/禁用警告编译指示。
Update
更新
回答by m3tikn0b
Too late for sharptooth but for all the googlers out there:
对于尖牙来说为时已晚,但对于所有的谷歌人来说:
#pragma warning ( suppress: ThatWarning )
// one single line with ThatWarning
is short for (generally since VS 2008, but in VS 2005for Code Analyzer warnings only):
是缩写(通常自 VS 2008 起,但在 VS 2005中仅用于代码分析器警告):
#pragma warning ( push )
#pragma warning ( disable: ThatWarning )
// one single line with ThatWarning
#pragma warning ( pop )
回答by ronag
The correct approach (although a bit ugly)
正确的做法(虽然有点丑)
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( once: ThatWarning )
#endif
//code with ThatWarning here
#ifdef _MSC_VER
#pragma warning( pop )
#endif
回答by Steve Townsend
You can disable specific warnings in the project or file options and this setting applies as the 'default' per those #pragmas at the relevant scope. Some of the warnings in VS2005 are so useless/annoying that this cleans up the output quite a bit, if using /W4
.
您可以禁用项目或文件选项中的特定警告,并且此设置适用于相关范围内每个 #pragmas 的“默认”。VS2005 中的一些警告是如此无用/令人讨厌,以至于如果使用/W4
.
This is in Properties under Configuration Properties -> C/C++ -> Advanced.
这是在 Configuration Properties -> C/C++ -> Advanced 下的 Properties 中。
回答by Kirill V. Lyadvinsky
I have no problems with the first variant. May be the better way is to use the following:
我对第一个变体没有问题。可能更好的方法是使用以下内容:
#pragma warning( push )
#pragma warning( once: ThatWarning )
//code with ThatWarning here
#pragma warning( pop )
This will let you know that there're still warnings in the code, but the warning messages will not be so annoying. But that is the matter of taste.
这会让您知道代码中仍然存在警告,但警告消息不会那么烦人。但那是品味的问题。
回答by Charlie
The first approach allows you change the specific warning setting in a local scope. It first stores all the current warning state by push into stack, apply your warning modifications, then restore (pop) to last warning state.
第一种方法允许您在本地范围内更改特定的警告设置。它首先通过压入堆栈存储所有当前警告状态,应用您的警告修改,然后恢复(弹出)到最后一个警告状态。
#pragma warning( push ) #pragma warning( once: ThatWarning ) //code with ThatWarning here #pragma warning( pop )
#pragma warning( push ) #pragma warning( once: ThatWarning ) //这里使用ThatWarning 的代码#pragma warning( pop )