How does #pragma once compare to traditional include guards?
Short answer
#pragma once tells the compiler to include a file only once, achieving the same effect as #ifndef guards with less boilerplate and no risk of guard-name collisions. It's non-standard but supported by all major compilers.
Advertisement
AModel answer
#pragma once tells the compiler to include a file only once, achieving the same effect as #ifndef guards with less boilerplate and no risk of guard-name collisions. It's non-standard but supported by all major compilers. Its rare edge case is with the same file reachable via different paths/symlinks; traditional guards are fully portable.
#pragma once // top of header, instead of #ifndef guardsReal-world example
Most modern headers use #pragma once for brevity, falling back to guards only where strict portability is required.
Likely follow-ups
- What is the advantage of #pragma once? | What is its edge case?
Related topics