Implementation defined behavior is controlled by#pragma directive.
Contents |
#pragmapragma_params | (1) | ||||||||
_Pragma(string-literal) | (2) | (since C99) | |||||||
\" with" and each\\ with\, then tokenizes the result (as intranslation stage 3), and then uses the result as if the input to#pragma in(1).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.
The following three pragmas are defined by the language standard:
#pragma STDC FENV_ACCESSarg | (1) | (since C99) | |||||||
#pragma STDC FP_CONTRACTarg | (2) | (since C99) | |||||||
#pragma STDC CX_LIMITED_RANGEarg | (3) | (since C99) | |||||||
wherearg is eitherON orOFF orDEFAULT.
ON, informs the compiler that the program will access or modifyfloating-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, usuallyOFF.ON.OFF.Note: compilers that do not support these pragmas may provide equivalent compile-time options, such as gcc's-fcx-limited-range and-ffp-contract.
#pragma once is a non-standard pragma that is supported by thevast 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 usinginclude guards:
#ifndef LIBRARY_FILENAME_H#define LIBRARY_FILENAME_H// contents of the header#endif /* LIBRARY_FILENAME_H */
So that all but the first inclusion of the header in any translation unit are excluded from compilation. All modern compilers record the fact that a header file uses an include guard and do not re-parse the file if it is encountered again, as long as the guard is still defined (see e.g.gcc).
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.
This family of pragmas control the maximum alignment for subsequently defined structure and union members.
#pragma pack(arg) | (1) | ||||||||
#pragma pack() | (2) | ||||||||
#pragma pack(push) | (3) | ||||||||
#pragma pack(push,arg) | (4) | ||||||||
#pragma pack(pop) | (5) | ||||||||
wherearg is a small power of two and specifies the new alignment in bytes.
#pragma pack may decrease the alignment of a structure, however, it cannot make a structure overaligned.
See also specific details forGCC andMSVC.
| This section is incomplete Reason: Explain the effects of this pragmas on data members and also the pros and cons of using them. Sources for reference: |
| This section is incomplete Reason: no example |
C++ documentation forImplementation defined behavior control |
| 1. | C++ pragmas in Visual Studio |
| 2. | Pragmas accepted by GCC |
| 3. | Individual pragma descriptions andStandard pragmas in IBM AIX XL C 16.1 |
| 4. | Appendix B. Pragmas in Sun Studio 11 C++ User's Guide |
| 5. | Intel C++ compiler pragmas |
| 6. | HP aCC compiler pragmas |