views:

255

answers:

7

What are the differences (in performance, usability and functionality) in using #pragma once and #if !defined MYHEADER_INCLUDED_ constructs? Or what is the difference between the two?

+7  A: 

Wikipedia has all your answers for this and is easy enough to find

From the article

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation.

Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoiding name clashes, and improved compile speed.

It has a more in-depth look at the advantages and disadvantages in the article. If you are really interested I suggest you read it completely, not just the blurb above.

Dan McGrath
:) thanks, let me read the article!
bdhar
Thanks Donal, I've been having issues when using the block quote button lately so I've been tending to avoid it. I shouldn't be so lazy and just do it manually...
Dan McGrath
+4  A: 
  1. performance -- usually the #pragma once implementation compiles faster, because it's intent is clear
  2. functionality -- both serve the same purpose -- to avoid more than one inclusion of a header file. The defined version has of course wider uses too, and if used carefully will allow to check in another file if the former was included (for conditional compilation for example)
  3. usability -- if portability is not an issue go with #pragma once -- however, this is a non-standard extension
Kornel Kisielewicz
The day #pragma once becomes standard, my life will become a little bit better.
mizipzor
@mizipzor, I want no stinkin' #pragma's, I want MODULES! http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2073.pdf :D
Kornel Kisielewicz
The day `#pragma once` becomes unnecessary, my life will become a little bit better.
Dennis Zickefoose
@Dennis: Touché
mizipzor
+1 for mentioning that it's not standard
Geoff
+1  A: 

As Dan McG said, they are almost the same. But I don't recommend using #pragma unless in the cant-avoid things (like #pragma section) .It just make the code more non-standard.

And there is no functionality/performance issue. Because this is in compiling time. It might increase compiling time, but I didn't experience any difference!

Yousf
+1  A: 

While pragma once is superior in many aspects to #ifdef guards, it has one major flaw: it is based on file system paths: it uses the basic assumption that two different (absolute) paths are also two different files. Unfortunately this is not always true.

While it is not very likely you'll run into problem with this, it is something to be aware of.

Koert
+3  A: 

As far as performance goes, I haven't measured it, but it's widely reported that compilers recognize the #include guard idiom and do not reopen/reprocess files that use it (therefore treating them the same as having used #pragma once).

Other than that potential performance benefit (which probably doesn't exist), I see little that #pragma once offers - it's somewhat more convenient when you create a new header - but guards really aren't that onerous to put in place. Since #pragma once isn't particularly compelling and since I still occasionally deal with a compiler that doesn't support it, I use guards.

If it was shown that #pragma once had a significant performance effect, I'd certainly start using it.

That said - I wish that C had been defined to only include headers once - unless some mechanism was invoked to indicate the header should be processed each time it was included. The times when you want that behavior are far, far out numbered by the times you don't (and I still come across headers every now and again that don't have guards or #pragma once). But it wasn't defined that way, so include guards it is...

Michael Burr
+2  A: 

Some more background on this:

MadKeithV
+1  A: 

#pragma once is non standard.

If you're happy with the prospect of changing every single one of your header files when you discover your app or library won't build when you try to port it to a different platform, it's probably a good choice.

Otherwise, stick with #ifndef/#define/#endif. Especially if you're building library code for other people to use.

Matt Curtis