What is the scope of a pragma directive?
What is the scope of a pragma directive? For开发者_如何学编程 example, if I say #pragma warning(disable: 4996)
in a header file A that is included from a different file B, will that also disable all those warnings inside B? Or should I enable the warning at the end of file A again?
It is till the end of the translation unit. Informally, a TU is the source file with its include files.
The usual pattern is this:
#pragma warning (push) //save
#pragma warning (disable: xxxx)
#pragma warning (disable: yyyy)
...
//code
#pragma warning (pop) //restore prev settings
for example
//A.h
#pragma once
#pragma warning (disable: 1234)
#include "b.h"
//b.h
#pragma once
//when included after a.h 1234 will be disabled
//c.cpp
#include "a.h" //warnings 1234 from b.h is disabled
//d.cpp
#include "b.h" //warnings 1234 from b.h are not disabled
#include "a.h"
Pragmas are specific for the compiler and platform in use. So the best bet is to look at compiler's documentation.
For IBM compilers, for example:
Many pragma directives can be specified at any point within the source code in a compilation unit; others must be specified before any other directives or source code statements. In the individual descriptions for each pragma, the "Usage" section describes any constraints on the pragma's placement.
In general, if you specify a pragma directive before any code in your source program, it applies to the entire compilation unit, including any header files that are included. For a directive that can appear anywhere in your source code, it applies from the point at which it is specified, until the end of the compilation unit.
You can further restrict the scope of a pragma's application by using complementary pairs of pragma directives around a selected section of code. For example, using #pragma options source and #pragma options nosource directives as follows requests that only the selected parts of your source code be included in your compiler listing:
#pragma options source /* Source code between the source and nosource pragma options is included in the compiler listing */ #pragma options nosource
Many pragmas provide "pop" or "reset" suboptions that allow you to enable and disable pragma settings in a stack-based fashion; examples of these are provided in the relevant pragma descriptions.
Generally, pragma should have effect right after its declaration, no matter from what header it comes, until the end of translation unit. However, there are some pragmas that affect the whole program. For example, Microsoft-specific "link" pragma that adds dependency on some library to the translation unit and all its "users".
Yes, it will also disable the warnings inside B.
A translation unit is a .cpp file and all its included files expanded out into one great big file. That pragma will last to the end of the translation unit, or until another #pragma warning changes the setting. Or, if you're compiler supports #pragma push and #pragma pop, it will last until the next #pragma pop.
'#pragma push' and '#pragma pop' allow you to create scopes. #pragma warnings within such a scope will apply to the end of the scope.
精彩评论