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?
Best Answer
This will work with multiple compilers (and different versions of compilers).
Header "push"
Header "pop"
Some warning
Usage
Additionally include guards can check that there is no double push/pop/disable-warning pragmas.
Update