Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endif
pre-compiler directives scheme also called macro-guard scheme (see code snippet below).
#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP
...
#endif
In most implementations/compilers (see picture below) however, there's a more "elegant" alternative that serves the same purpose as the macro-guard scheme called #pragma once
. #pragma once
has several advantages compared to the macro-guard scheme, including less code, avoidance of name clashes, and sometimes improved compile speed.
Doing some research, I realized that although #pragma once
directive is supported by almost all known compilers, there's a turbidness on whether #pragma once
directive is part of the C++11 standard or not.
Questions:
- Could someone clarify whether
#pragma once
directive is part of the C++11 standard or not? - If it's not part of the C++11 standard, are there any plans on including it on later releases (e.g., C++14 or later)?
- It would also be nice if someone could further elaborate on the advantages/disadvantages in using either one of the techniques (i.e., macro-guard versus
#pragma once
).
#pragma once
usually doesn't. – Headrail