C++ › Preprocessor & Macros

How does #pragma once compare to traditional include guards?

Advanced

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 guards
Real-world example Most modern headers use #pragma once for brevity, falling back to guards only where strict portability is required.

Common follow-ups: What is the advantage of #pragma once? | What is its edge case?