C++ Reference ยท Preprocessor, Tools and Concepts
The #pragma directive
Reference for the C++ #pragma directive: implementation-defined hints such as once, pack and GCC diagnostic, the _Pragma operator, and portability caveats.
#pragma is the standard's escape hatch for compiler-specific instructions. Whatever follows the word pragma is implementation-defined, and a compiler that does not recognize a pragma is allowed to ignore it (many warn about it with -Wunknown-pragmas). The directive therefore never changes the meaning of a portable program; it changes layout, diagnostics, optimization hints or how headers are included.
The only pragma that C++ itself defines is #pragma STDC ... from C (such as FENV_ACCESS), which GCC mostly ignores. Everything else is a vendor extension, yet a few are so widespread that you will meet them in nearly every code base: #pragma once, #pragma pack and the diagnostic controls. C++11 also added the _Pragma("text") operator, which does the same job but can appear inside macros.
#pragma once // in a header: include this file only once #pragma pack(push, 1) // lay out the following structs without padding #pragma GCC diagnostic ignored "-Wunused-variable"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
#pragma pp-tokens // implementation-defined _Pragma("string") // C++11 operator; the string is unescaped and treated like #pragma
| Pragma (GCC/Clang unless noted) | Description |
|---|---|
#pragma once |
In a header: include it at most once per translation unit. Supported by every major compiler but not in the standard. |
#pragma pack(push, n) / pack(pop) |
Set (and restore) the maximum member alignment for following structs. |
#pragma GCC diagnostic push / pop / ignored "-Wname" |
Save, restore or change warning settings for a region. Clang also accepts clang diagnostic; MSVC uses #pragma warning(...). |
#pragma GCC unroll N |
Hint to unroll the next loop. |
#pragma omp ... |
OpenMP directives, honored only with -fopenmp. |
#pragma message("text") |
Print a note at compile time. |
#pragma GCC poison name |
Make any later use of name an error. |
#pragma STDC FENV_ACCESS ON |
Standard C pragma; tells the compiler the code reads floating-point flags. GCC does not implement it. |
Errors / undefined behavior: an unrecognized pragma is ignored, not an error. Using a pragma incorrectly (for example a pack(pop) without a matching push) can produce a diagnostic or silently different layout.
Usage notes
#pragma onceversus include guards.#pragma onceis shorter and cannot suffer from a name collision, but it relies on the compiler recognizing "the same file", which can fail with symbolic links, copies of a file or network file systems. Guards are portable by definition. Using#pragma onceis common and acceptable, but do not put it in the main source file (GCC warns "#pragma oncein main file").- Packed structs are risky. Members may be misaligned, which is slower or even faulting on some CPUs, and binding a reference to a member of a packed struct is a warning in GCC and undefined in practice. Use packing only to match a file or network format, and copy fields out.
- Layout is not portable. The padding numbers in the example hold on common 64-bit platforms. Use
static_assert(sizeof(T) == n)to catch differences. - Scope diagnostics narrowly: wrap only the offending lines in
push/pop. A globalignoredhides real problems. - Prefer standard facilities when they exist:
[[maybe_unused]]instead of ignoring unused warnings,alignasandalignoffor alignment,[[likely]]for hints,std::source_locationfor messages. _Pragmainside macros is the only way to emit a pragma from a macro, because#directives cannot be produced by macro expansion.- Don't count on order across compilers: the same pragma may exist with different meaning (or not at all) elsewhere. Guard vendor-specific ones with
#if defined(__GNUC__)or similar. - OpenMP-style pragmas quietly do nothing when the flag is missing, so the program stays correct but sequential.