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?
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?
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
But this can be just a reasonOriginally 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
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
>> 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.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law