Implementation defined behavior control
Implementation defined behavior is controlled by
Syntax
1 | #pragma | pragma-params | |
2 | _Pragma | string-literal | |
- Behaves in implementation-defined manner.
- Removes the L prefix (if any), the outer quotes, and leading/trailing whitespace from string-literal, replaces each
\"
with"
and each\\
with\
, then tokenizes the result (as in translation phase 3), and then uses the result as if the input to #pragma in (1).
Non-standard pragmas
The ISO C++ language standard does not require the compilers to support any pragmas. However, several non-standard pragmas are supported by multiple implementations:
#pragma STDC
ISO C language standard requires that C compilers support the following three pragmas, and some C++ compiler vendors support them, to varying degrees, in their C++ frontends:
1 | #pragma | STDC | FENV_ACCESS | arg | |
2 | #pragma | STDC | FP_CONTRACT | arg | |
3 | #pragma | STDC | CX_LIMITED_RANGE | arg | |
where arg is either ON, OFF, or DEFAULT.
- If set to 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.
- Allows contracting of floating-point expressions, that is optimizations that omit rounding errors and floating-point exceptions that would be observed if the expression was evaluated exactly as written.
For example, allows the implementation of
(x * y) + z
with a single fused multiply-add CPU instruction. The default value is implementation-defined, usually ON. - Informs the compiler that multiplication, division, and absolute value of complex numbers may use
simplified mathematical formulas
(x+iy)×(u+iv) = (xu-yv)+i(yu+xv)
,(x+iy)/(u+iv) = [(xu+yv)+i(yu-xv)]/(u2+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.
#pragma once
Standard approach to preventing multiple inclusion of the same header is by using include 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
// 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 pack
This family of pragmas control the maximum alignment for subsequently defined class and union members.
1 | #pragma | pack ( arg ) | |
2 | #pragma | pack ( ) | |
3 | #pragma | pack ( push ) | |
4 | #pragma | pack ( push, arg) | |
5 | #pragma | pack ( pop ) | |
where arg is a small power of two and specifies the new alignment in bytes.
- Sets the current alignment to value arg.
- Sets the current alignment to the default value (specified by a command-line option).
- Pushes the value of the current alignment on an internal stack.
- Pushes the value of the current alignment on the internal stack and then sets the current alignment to value arg.
- Pops the top entry from the internal stack and then sets (restores) the current alignment to that value.
See also specific details for GCC and MSVC.
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
References
- C++23 standard (ISO/IEC 14882:2023):
- 15.9 Pragma directive [cpp.pragma]
- C++20 standard (ISO/IEC 14882:2020):
- 15.9 Pragma directive [cpp.pragma]
- C++17 standard (ISO/IEC 14882:2017):
- 19.6 Pragma directive [cpp.pragma]
- C++14 standard (ISO/IEC 14882:2014):
- 16.6 Pragma directive [cpp.pragma]
- C++11 standard (ISO/IEC 14882:2011):
- 16.6 Pragma directive [cpp.pragma]
- C++98 standard (ISO/IEC 14882:1998):
- 16.6 Pragma directive [cpp.pragma]