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.
The first one is to use #pragma warning( push )
and #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( 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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…