• Advertisement
Sign in to follow this  

#pragma once vs #ifndef

This topic is 2559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been reading up on the two differences and require a little conversation to get a better idea as to why I would use one over the other since what I find isn't that helpful. Mainly why I would use #pragma once over #ifndef or both together. It appears that using both would be benificial as well, taking advantage of any optimizations the compiler may have and for writing portable code. A better insight on this would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
"#pragma once" is non-standard, meaning the compiler is absolutely allowed to ignore it, so IMO you should use "#pragma once" first (due to it being a compiler optimisation), but also use include guards (#ifndef x, #define x, #endif) as a backup.

In reality though, every modern C++ compiler does support "#pragma once", so it's pretty safe to leave out the traditional include guards.

Share this post


Link to post
Share on other sites
#pragma once is not, strictly speaking, portable. #ifndef guards usually are, if properly implemented (that is if you're not using reserved symbols for the guard). It doesn't really matter in general. I almost never use #pragma once.

Share this post


Link to post
Share on other sites
IMO you should use "#pragma once" first (due to it being a compiler optimisation)[/quote]

Is it an optimisation though? Last time I investigated, MSVC was just as fast with portable scope guards as it was with #pragma once. I presume they accomplish that by applying the same optimization to scope guards as they do to #pragma once (it's essentially the same, with the additional detail that you have to store a mapping of file paths to scope guard names rather than just file paths - but it isn't tricky).

Share this post


Link to post
Share on other sites
It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.

Share this post


Link to post
Share on other sites

It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.


That's right. There's an article out there with numbers. The difference between #ifdef and #pragma once is not noticeable and often not even measurable.
More effective ways to speed up compilations times are:
* Minimizing your header dependencies
* Precompiled headers
* Big cpp method (#include all .cpps into one big .cpp and compile that. Speeds up linking as well, but only good for complete rebuilds and may have side effects when depending on compilation unit scopes)
* ccache or similar
* distributed compilation

Share this post


Link to post
Share on other sites
Portability is only one issue. What is more important for me is that #pragma once doesn't allow including a very certain file twice given its location. This little detail may seem unimportant, but let's just imagine you are using some version control that allows sharing files among different directories. It is then tempting to copy some commonly used headers to different modules of the application (different projects in MSVC terminology). Now just imagine following situation:

file A.cpp includes some shared header from A location - G.h, but also includes some header from other module (fe. DLL) - D.h.
Unfortunately file D.h includes the same shared header, but from D location - G.h
Please note that G.h is finally included twice when compiling A.cpp, but each time from a different location, so they are 2 distinct files on a hard drive.

In such a case using #pragma once will result in including this G.h file twice and probably resulting in some redefinition errors, while using #ifndef will work as expected (even though they are 2 distinct files, their content is the same, so name of preprocessor definition is exactly the same).

That's generally why I have switched to using #ifndef... #define... #endif guards from #pragma once

Share this post


Link to post
Share on other sites

[quote name='Rubicon' timestamp='1295428062' post='4761142']
It is an optimisation, but only technically.

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

In practice it's never made a noticeable difference ime and I've worked on fairly large projects using both methods. Nowadays we use the traditional way, as it's absolutely guaranteed to still work on some of the crappier compilers our work often sees us faced with.After all, it's hardly a hassle.


That's right. There's an article out there with numbers. The difference between #ifdef and #pragma once is not noticeable and often not even measurable.
More effective ways to speed up compilations times are:
* Minimizing your header dependencies
* Precompiled headers
* Big cpp method (#include all .cpps into one big .cpp and compile that. Speeds up linking as well, but only good for complete rebuilds and may have side effects when depending on compilation unit scopes)
* ccache or similar
* distributed compilation
[/quote]

gcc claims to have optimized code for traditional include guards, and I have no reason to not believe it. So if there is a big difference, then prolly on compilers that don't optimize #ifndef-guards :)

Share this post


Link to post
Share on other sites

Portability is only one issue. What is more important for me is that #pragma once doesn't allow including a very certain file twice given its location. This little detail may seem unimportant, but let's just imagine you are using some version control that allows sharing files among different directories. It is then tempting to copy some commonly used headers to different modules of the application (different projects in MSVC terminology). Now just imagine following situation:

file A.cpp includes some shared header from A location - G.h, but also includes some header from other module (fe. DLL) - D.h.
Unfortunately file D.h includes the same shared header, but from D location - G.h
Please note that G.h is finally included twice when compiling A.cpp, but each time from a different location, so they are 2 distinct files on a hard drive.

In such a case using #pragma once will result in including this G.h file twice and probably resulting in some redefinition errors, while using #ifndef will work as expected (even though they are 2 distinct files, their content is the same, so name of preprocessor definition is exactly the same).

That's generally why I have switched to using #ifndef... #define... #endif guards from #pragma once


A much better solution is to move common headers into a "shared" path such that you can therefore keep track of where exactly you reuse such files. This also helps clean up source control a bit, and makes maintaining compatibility between APIs/clients much easier, since any change in the shared path is guaranteed to break all clients.

Share this post


Link to post
Share on other sites

A much better solution is to move common headers into a "shared" path such that you can therefore keep track of where exactly you reuse such files. This also helps clean up source control a bit, and makes maintaining compatibility between APIs/clients much easier, since any change in the shared path is guaranteed to break all clients.

I definitely agree, just wanted to point out that there can be such approach to code organization due to for example SourceSafe possibilities (seems quite common from my own experience despite the fact I think it's messy) and #pragma once just doesn't go together with it.

Share this post


Link to post
Share on other sites
The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.[/quote]
That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.

The optimization works like this (pseduo code)
[html]
FOR EACH translation unit
FOR EACH #include directive encountered
IF the file is in the set of "#pragma once" files
OR the file is in the set of files with include guards and the relevent preprocessor symbol is still defined:
We're finished processing the include (no need to open the file at all)
ELSE:
Process the file
If the file contained a pragma once, add it to the set of "#pragma once" files
If the file contained an include guard add it to the set of files with include guards,
along with the record of the relevent preprocessor symbol
[/html]

The important things to note are:
* The optimization works just as well with #pragma once as it does with include guards - it's slightly easier to implement with #pragma once, which is why the #pragma once optimization precedes the optimization being applied to normal include guards.
* An #include directive does not require the included file to be opened and read a second time, cutting down on file IO.

Share this post


Link to post
Share on other sites

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.
[/quote]
Yes, you're right of course. I blame that brain fart on having just got up! :)

It still doesn't make any difference in my world. In fact, in my standard non-standard ways that Apoqpiq loves so much, I get the most speed boost by breaking ALL the rules.

We have a single header file that includes all the others. This single file is made into a PCH. No other source files include anything other than this one master file. Which sounds about as suboptimal as you can be, right? Especially when you touch a single header and have to suffer a full rebuild for your trouble.

But, I urge anyone to at least try it one wet weekend. If you stick rigidly to the principal of including this ONE file only in all your sources, the speed boost you see will leave you slack jawed and you'll suddenly understand that a full rebuild each time you touch a loose header really isn't a problem when it only takes several seconds.

EDIT: Dunno if this works on gcc too well, depends on the version. DevStudio fair zings along, but the biggest win we ever had was switching someone elses project (done in the usual way) to our uber-header method when porting to DS. The PCH speed-up in CodeWarrior is ridiculous. A full rebuild of defcon took a couple of seconds, I shit you not.

Share this post


Link to post
Share on other sites
My pattern looks like this:

#ifndef FOO
#define FOO
#pragma once
// ... header stuff ...
#endif

Compiler can choose which one it likes best :) The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:

#include "Foo.h"

#include "Bar.h"

// Hacky fun time
#undef FOO
#include "Foo.h"

But this is an exception to the rule and certainly should be handled on a case-by-case basis for any header that needs to do this.

Share this post


Link to post
Share on other sites
Compiler can choose which one it likes best The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:[/quote]

I wouldn't call this a downside. #pragma once is designed specifically to call a header once, without the rather inelegant method of 3 separate # directives and having to make sure you're using a unique symbol.
If you need to call a header twice in the same translation unit then by all means use something other than the compiler directive designed specifically not to call the same header twice.


I use it in MSVC and it apparently works on the other major compilers too. If a feature is supported on the compilers you're using then I don't see a reason not to use it. While it's not supported on *every* compiler, the same could be said about several other c++ features which are standard compliant and *still* have poor compiler support.

Share this post


Link to post
Share on other sites
#pragma once is pretty much safe to use cross compiler nowadays. If you want to use it you can. I think it was add in GCC 4.0.0 maybe earlier. The new compiler Clang supports it also afaik for Mac.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement