From 3c1bd6c5ed0d76d11a4bdfe6362248cf262188a4 Mon Sep 17 00:00:00 2001 From: Amish Naidu Date: Sun, 6 Oct 2019 19:03:44 +0530 Subject: [PATCH] content(resources): Add pragma-once-faq for C++ --- src/content/resources/cpp/pragma-once-faq.md | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/content/resources/cpp/pragma-once-faq.md diff --git a/src/content/resources/cpp/pragma-once-faq.md b/src/content/resources/cpp/pragma-once-faq.md new file mode 100644 index 0000000..310698f --- /dev/null +++ b/src/content/resources/cpp/pragma-once-faq.md @@ -0,0 +1,39 @@ +--- +authors: + - "sudonym#8623" +created_at: 2019/10/6 +title: "pragma once FAQ" +--- + +## 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](https://en.wikipedia.org/wiki/Include_guard) 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 ? + +1. Less typing if your IDE/editor doesn't do it for you ¯\\\_(ツ)\_/¯ +2. 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. +3. You find header guards icky. + +### Why should I not use it ? + +1. You want to be standards-compliant +2. You want to follow CppCoreGuidelines: +3. You use a `compiler:version` which doesn't support `pragma once` or has a buggy implementation (see the section on compiler support) + +### Why not both? + +![Why not both](https://i.imgur.com/DBcUtoo.png) + +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](https://en.wikipedia.org/wiki/Pragma_once#Portability) for more information.