I was looking at the Source Engine SDK and noticed a header file that has #pragma once at the top followed by inclusion guards.
I thought they did pretty much the same thing; is there any reason why they both were used or is it just superfluous?
Printable View
I was looking at the Source Engine SDK and noticed a header file that has #pragma once at the top followed by inclusion guards.
I thought they did pretty much the same thing; is there any reason why they both were used or is it just superfluous?
Seems a complete waste of effort to me.
Allegedly, it's slightly quicker to use the #pragma, but since it's non-portable you have to rely on the include guards at some point anyway.
But this can be just a reasonQuote:
Originally Posted by Salem
On compilers supporting this pragma - the file is not included at all after the first time
On compilers that have no support - pragma is ignored, file is included, but it content is empty due to include guards on each include after the first
>> But this can be just a reason
From what I can tell this is compiled for the XBox (haven't seen HL2 for the XBox in stores though) given the #ifdef _XBOX (etc) in various places.
Perhaps the compiler for the XBox version doesn't have this pragma.
On VC++, using the standard include guards causes the preprocessor open the file, read and process its contents, and thus end up with an empty file on second inclusion.
Using the pragma, it adds the path of the header to a list and doesn't even open it on the second time, leading to faster compilation.
GCC, on the other hand, recognizes the include guard pattern and adds the header path to a list then, so it gets this benefit without the need for a proprietary pragma. (But GCC 4+ have full support for #pragma once anyway.)