So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double guard is as follows:
Header file, "header_a.hpp":
#ifndef __HEADER_A_HPP__
#define __HEADER_A_HPP__
...
...
#endif
When including the header file anywhere, either in a header or source file:
#ifndef __HEADER_A_HPP__
#include "header_a.hpp"
#endif
Now I understand that the use of the guard in header files is to prevent multiple inclusion of an already defined header file, it's common and well documented. If the macro is already defined, the entire header file is seen as 'blank' by the compiler and the double inclusion is prevented. Simple enough.
The issue I don't understand is using #ifndef __HEADER_A_HPP__
and #endif
around the #include "header_a.hpp"
. I'm told by the coworker that this adds a second layer of protection to inclusions but I fail to see how that second layer is even useful if the first layer absolutely does the job (or does it?).
The only benefit I can come up with is that it outright stops the linker from bothering to find the file. Is this meant to improve compilation time (which was not mentioned as a benefit), or is there something else at work here that I am not seeing?
#pragma once
seems to be supported by major compilers? – Lalalalage#pragma once
is not standard, however it is supported by many known compilers. – Canonist#pragma once
is supported but is not standard. – Crosby__HEADER_A_HPP__
) and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. Don't use them in your code. – Wretch#pragma once
is supported by every major compiler. It is not a de jure, but a de facto standard. – Obsolesce#pragma once
, and that's what everyone was encouraged to use. @Minnich – Woven#define __HEADER_A_HPP__
in the source file also, below the#include
. Only then would it have made sense. Not much though. – Sioux