使用#pragma警告push / pop正确的方法暂时改变警告级别? [英] Is using #pragma warning push/pop the right way to temporarily alter warning level?

查看:1032
本文介绍了使用#pragma警告push / pop正确的方法暂时改变警告级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,很难编写不会发出警告的C ++代码。启用警告是一个好主意。因此,通常需要禁用某些特定结构的警告,并在所有其他代码中启用它们。

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.

我已经看到了到目前为止的两种方式。

I've seen two ways of doing that so far.

第一个是使用 #pragma warning(push) #pragma warning (pop)

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

第二个是使用 #pragma warning(默认)

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

第一种方法看起来很干净。有什么问题吗?有没有更好的方法来实现相同?

The first approach looks clean. Are there any problems with it? Are there any better ways to achieve the same?

推荐答案

第一种方法是最好的方法,IMO。我知道没有问题。

The first method is the best way to do it, IMO. I know of no problems with it.

请记住,#pragma是编译器专用的,所以不要期望它可以在每个编译器上工作:)

Simply bear in mind that a #pragma is compiler specific so don't expect it to work on every compiler out there :)

这篇关于使用#pragma警告push / pop正确的方法暂时改变警告级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆