Implementation defined behavior control
Implementation defined behavior is controlled by #pragma
directive.
Syntax
#pragma pragma_params
|
(1) | ||||||||
_Pragma ( string-literal )
|
(2) | (since C99) | |||||||
\"
with "
and each \\
with \
, then tokenizes the result (as in translation stage 3), and then uses the result as if the input to #pragma
in (1).Explanation
The pragma directive controls implementation-specific behavior of the compiler, such as disabling compiler warnings or changing alignment requirements. Any pragma that is not recognized is ignored.
Standard pragmas
The following three pragmas are defined by the language standard:
#pragma STDC FENV_ACCESS arg
|
(1) | ||||||||
#pragma STDC FP_CONTRACT arg
|
(2) | ||||||||
#pragma STDC CX_LIMITED_RANGE arg
|
(3) | ||||||||
where arg is either ON
or OFF
or DEFAULT
.
ON
, informs the compiler that the program will access or modify floating-point environment, which means that optimizations that could subvert flag tests and mode changes (e.g., global common subexpression elimination, code motion, and constant folding) are prohibited. The default value is implementation-defined, usually OFF
.ON
.+v2
), and |x+iy| = √x2
+y2
, despite the possibility of intermediate overflow. In other words, the programmer guarantees that the range of the values that will be passed to those function is limited. The default value is
OFF
Non-standard pragmas
#pragma once
#pragma once is a non-standard pragma that is supported by the vast majority of modern compilers. If it appears in a header file, it indicates that it is only to be parsed once, even if it is (directly or indirectly) included multiple times in the same source file.
Standard approach to preventing multiple inclusion of the same header is by using include guards:
#ifndef FILENAME_H #define FILENAME_H // contents of the header #endif /* FILENAME_H */
So that all but the first inclusion of the header in any translation unit are excluded from compilation.
With #pragma once, the same header appears as
#pragma once // contents of the header
Unlike header guards, this pragma makes it impossible to erroneously use the same macro name in more than one file. On the other hand, since with #pragma once files are excluded based on their filesystem-level identity, this can't protect against including a header twice if it exists in more than one location in a project.
#pragma pack
This section is incomplete |
References
- C11 standard (ISO/IEC 9899:2011):
- 6.10.9 Pragma operator (p: 178)
- C99 standard (ISO/IEC 9899:1999):
- 6.10.6 Pragma directive (p: 159)
- C89/C90 standard (ISO/IEC 9899:1990):
- 3.8.6 Pragma directive
See also
C++ documentation for Implementation defined behavior control
|
External links
- C++ pragmas in Visual Studio 2010
- Pragmas accepted by GCC 4.9.2
- Individual pragma descriptions and Standard pragmas in IBM AIX XL C 13.1
- Appendix B. Pragmas in Sun Studio 11 C++ User's Guide
- Intel C++ compiler pragmas
- HP aCC compiler pragmas