#pragma once

Started by
12 comments, last by krakrazor 20 years, 3 months ago
quote:Original post by Doc
You''re wasting your time. #ifndef... will always work, thus your #pragma once is redundant and pointless.


Include guards will always work and so are recommended, but adding #pragma is a good idea as well.

Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.
Advertisement
quote:Original post by OrangyTang
quote:Original post by Doc
You''re wasting your time. #ifndef... will always work, thus your #pragma once is redundant and pointless.


Include guards will always work and so are recommended, but adding #pragma is a good idea as well.

Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.


That''s exactly what I meant (and wanted to say); thanks for supporting me.

---
shurcooL`
quote:Original post by OrangyTang
Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.


Meh. You could reasonably easily implement a file cache and include guard detection method into the compiler. #pragma once is a cop-out.
I still reckon the whole #pragma once thing is a waste of time. I mean, really, how slow is your computer that you notice the time it takes to open a file several times? The overwhelming majority of time taken is the actual compilation.
And if your include file is being opened so frequently, shouldn''t the OS cache it anyway?

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

My stuff.Shameless promotion: FreePop: The GPL god-sim.
Here''s an excerpt from our intranet C++ coding guidelines on include guards:

----------------------------
External include guards exist to stop the pre-processor opening up Header.h, scanning the file in its entirety, and returning only to find that the file never needed to be opened in the first place. This can affect compile speeds dramatically.

It has been found that the use of #pragma once, on compilers that support it, does not replace the functionality of external include guards. Experiments with the latest VC7.1 compiler show that #pragma once is a lot slower than external include guards. See the following page for details of the experiment:

http://www.eskimo.com/~hottub/software/include/

The results obtained by putting #pragma once in the include guard test were as follows:

* External Include Guards: 0.08s
* Internal Include Guards: 6.25s
* No Include Guards: 9.70s

Note that external include guards are not needed when including from .cpp files.
----------------------------

Bottom line: #pragma once is useless.

Beside that point, there''s been interesting discussion on the gcc compiler mailing list about whether the person writing the header file should be allowed to dictate the use of their header file. This was the reason #pragma once or something similar was deprecated - gcc now handles all this automatically if you have include guards (I think, anyway).

This topic is closed to new replies.

Advertisement