warning
#pragma warning( warning-specifier : warning-number-list [,warning-specifier : warning-number-list...] )
#pragma warning( push[ , n ] )
#pragma warning( pop )
Allows selective modification of the behavior of compiler warning messages.
The warning-specifier can be one of the following.
Warning-specifier | Meaning |
---|---|
once | Display the specified message(s) only once. |
default | Reset warning behavior. This also has the effect of turning a specified warning on that is off by default. |
1, 2, 3, 4 | Apply the given warning level to the specified warning message(s). |
disable | Do not issue the specified warning message(s). |
error | Report the specified warnings as errors. |
The warning-number-list can contain any warning numbers. Multiple options can be specified in the same pragma directive as follows:
#pragma warning( disable : 4507 34; once : 4385; error : 164 )
This is functionally equivalent to:
#pragma warning( disable : 4507 34 ) // Disable warning messages // 4507 and 34. #pragma warning( once : 4385 ) // Issue warning 4385 // only once. #pragma warning( error : 164 ) // Report warning 164 // as an error.
For warning numbers greater than 4699, those associated with code generation, the warning pragma has effect only when placed outside function definitions. The pragma is ignored if it specifies a number greater than 4699 and is used inside a function. The following example illustrates the correct placement of warning pragmas to disable, and then restore, the generation of a code-generation warning message:
int a; #pragma warning( disable : 4705 ) void func() { a; } #pragma warning( default : 4705 )
Note that within a function body, the last setting of the warning pragma will be in effect for the entire function.
The warning pragma also supports the following syntax:
#pragma warning( push [ ,n ] )
#pragma warning( pop )
Where n represents a warning level (1 through 4).
The pragma warning( push ) stores the current warning state for all warnings. The pragma warning( push, n**)** stores the current state for all warnings and sets the global warning level to n.
The pragma warning( pop ) pops the last warning state pushed onto the stack. Any changes made to the warning state between push and pop are undone. Consider this example:
#pragma warning( push ) #pragma warning( disable : 4705 ) #pragma warning( disable : 4706 ) #pragma warning( disable : 4707 ) // Some code #pragma warning( pop )
At the end of this code, pop restores the state of all warnings (including 4705, 4706, and 4707) to what it was at the beginning of the code.
When you write header files, you can use push and pop to ensure that changes to warning states made by the user do not prevent your headers from compiling properly. Use push at the beginning of the header and pop at the end. Suppose, for example, you have a header that does not compile cleanly at warning level 4. The following code changes the warning level to 3, then restores the original warning level at the end of the header:
#pragma warning( push, 3 ) // Declarations/ definitions #pragma warning( pop )