There are two useful #pragma directives I like to use in my code: one let’s the preprocessor know that you want to include a header fine only once, and another deals with structure packing.
Instead of using the header include guards, which are ugly as sin, use #pragma once at the beginning of your header files, like this:
For structure packing, use #pragma pack directive. It tells the compiler about the default field alignment. On Clang 8.0.0 the sizeof of this structure is 32 bytes:
We can pack it down to 27 bytes by using this directive (it tells the compiler to align all member fields on one byte boundary; this is useful when designing efficient network protocols or data serialization):
You can also show, at compile time, what the current packing alignment is with #pragma pack(show). Current alignment can be pushed onto a stack, then reverted back, with #pragma pack(push, 1) followed by #pragma pack(pop).
Complete listing (pragma.cpp):
32, 27
Program output.
Note that none of the pragmas are specified by the ISO standard.
The
#pragma once
is a de facto standard, AFAIK supported by all modern C++ compilers, except Cray C++ (don’t know how modern that implementation is). Wikipedia provides a list of compiler support.
#pragma pack
is also a de facto standard very much worth knowing about, but possibly not as universal as
#pragma once
. There may be differences between compilers about arguments etc. A good reason to use it is to get complete control over the memory layout. A bad reason to use it is to just save space (that’s better done by using the relevant optimization option in the compiler invocation).
#pragma once has also its drawbacks as this article points out
https://hackernoon.com/generated-include-guards-an-alternative-to-pragma-once-31cc3dee6ce