mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-05 16:06:15 +02:00
1.7 KiB
1.7 KiB
authors, created_at, title
| authors | created_at | title | |
|---|---|---|---|
|
2019/10/6 | pragma once |
What is #pragma once?
The #pragma once directive tells a compiler to only include and parse a header file once even if it is included multiple times in the same source file.
They provide a cleaner alternative to include-guards that are conventionally used for this purpose.
Is it portable ?
No. #pragma directives are used to control implementation-defined behavior and are thus compiler-specific.
Why should I use it ?
- Less typing if your IDE/editor doesn't do it for you ¯\_(ツ)_/¯
- Faster compiling, as the compiler can now completely ignore processing/loading this file, it'd have to load it all if you were using a header guard.
- You find header guards icky.
Why should I not use it ?
- You want to be standards-compliant
- You want to follow CppCoreGuidelines: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-guards
- You use a
compiler:versionwhich doesn't supportpragma onceor has a buggy implementation (see the section on compiler support)
Why not both?
Some people/projects do this, so that you can both be standards-compliant (since unrecognized pragmas will be ignored) and get that speed bonus. I can't tell you if you should either use pragma, header guards or both, that's just one of those opinion topics nobody agrees on.
What compilers support it?
Virtually all popular and modern compilers. See this list for more information.
