English 中文(简体)
是否利用“ #”预警推动/宣传临时改变警告水平的正确途径?
原标题:Is using #pragma warning push/pop the right way to temporarily alter warning level?

一旦难以写到C++代码,那就根本无法发出警告。 然而,发出警告是一种好的想法。 因此,常常需要围绕某些具体建筑进行可调和的警告,并在所有其他法典中加以利用。

我看到了迄今为止这样做的两种方式。

第一种是使用<代码>#pragma 警告(中程)和<代码>#pragma 警告(现用)<代码>:

 #pragma warning( push )
 #pragma warning( disable: ThatWarning )
 //code with ThatWarning here
 #pragma warning( pop )

第二种是使用<代码>#pragmasu(违约):

 #pragma warning( disable: ThatWarning )
 //code with ThatWarning here
 #pragma warning( default: ThatWarning )

我在第二种变量中看到的问题是,它放弃了最初的警告水平——警告可能已经停止,或者其警告水平可能已经改变。 使用<代码>default将消除这些改动。

第一种做法是干净的。 是否有任何问题? 是否有更好的办法实现同样的目标?

最佳回答

第一个办法是海事组织的最佳方式。 我不知道这个问题。

简略地铭记,只有专册才编成册,因此并不期望它为每个编汇者开展工作:

问题回答

这将与多个汇编者(和不同的汇编者版本)合作。

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
// ...

Update

sharp太晚,但就那里所有的神 the而言:

#pragma warning ( suppress: ThatWarning )
// one single line with ThatWarning

http://msdn.microsoft.com/en-us/library/2c8f766e%28v=vs.90%29.aspx”

#pragma warning ( push )
#pragma warning ( disable: ThatWarning )
// one single line with ThatWarning
#pragma warning ( pop )

正确做法(尽管只是略微)

#ifdef _MSC_VER
 #pragma warning( push )
 #pragma warning( once: ThatWarning )
#endif
 //code with ThatWarning here
#ifdef _MSC_VER
 #pragma warning( pop )
#endif

在项目或档案选择中,你可以提出不同的具体警告,而这一环境则作为相关范围的“实际”的缺省而适用。 VS2005的一些警告是,如果使用<代码>/W4,那么这种警告便无用/根本无法清理产出。

不动产——和不动产;C/C++ - ;高级。

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 )

这将使你知道,该法典中仍有警告,但警告信息不会消失。 但这是tas事。

第一种做法允许你改变当地的具体警告。 它首先储存所有当前的警报状态,把注意力推向 st,适用你的警告修改,然后恢复(民众)状态。

#pragma warning( push ) #pragma warning( once: ThatWarning ) //code with ThatWarning here #pragma warning( pop )





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签