Jump to content
  • Advertisement
Sign in to follow this  
wendigo23

#ifndef/#define or #pragma once?

This topic is 5440 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

Quote:
Original post by Anonymous Poster
Quote:
Original post by Anonymous PosterAt leas gcc (and I suspect MSVC too) has a special case to detect inclusion guards, so it probably won't slow things down at all.


so how is that done then ? you have to make sure the #if/#endif encloses all of the code. you can't just check at the bottom of the file for #endif since you have no way of telling that it belongs to the #if at the top either.
I'm guess it detects the inclusion guard the first time you include it, then remebers enough info to know that the next time it is included to the same file, it doesn't need to actually do anything. It only needs to 'quick detect' the inclusion guard after the first inclusion, so it has already fully parsed the file once by then.

Share this post


Link to post
Share on other sites
Advertisement
The performance gain from using "#pragma once" is so close to 0 that it might as well be 0.

But I do prefer to use "#pragma once", just because you get a small gain in readability, and it's easier to type. And how often are you going to switch compilers during a project? And even if you do switch compilers, it would take like 2 seconds to fix.

Also, a specific complaint to the AP: why do so many people completely overreact to bad style decisions? I swear, this is at least the third thread I can remember where, as we are talking about potentially violating standards or style guidelines, one guy chimes in and says "Oh, if someone wrote code like that, I would totally fire them on the spot!". As if professional programmers are so dense that you can't just TELL them, you have to actually fire them.

Share this post


Link to post
Share on other sites
I just use #pragma once for my own code not out of concern for compile speed, but just because it's easier to get right, and it presents less eye candy to distract from the actual content of the header. Clarity is something which C++ desperately needs, so I'll take whatever I can get. :)

Support from GCC 3.x and MSVC 7.x is enough to count as "portable" for my purposes, so that's not a problem.

(your mileage may vary, of course. Season to taste. Not suitable for children under the age of 40. Keep away from open flame)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by wendigo23
Someone at my company just sent an email to everyone saying to stop using the #ifndef/#define combo to guard headers, and instead to use #pragma once because its more crap for the compiler to parse. HAHAHA right, like using a pragma vs a ifndef is going to take a lot less time to build.

What do folks around here think is the better way?


This guy is an inexperienced idiot.

Parsing #ifdef/#ifndef or #pragma doesn't make any difference performance-wise. Compiling a lot of STL headers does.

So the thing to do is to put as many headers in the .cpp as possible, and use precompiled headers when available (with VC++ and with the ccache tool for gcc).

#pragma once being a #pragma is non-standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
Also, a specific complaint to the AP: why do so many people completely overreact to bad style decisions?


Code is religion.

Also just a shitty day. I have apparently zero desk priority where I am working. No desk set aside for little old me, but there's one that's usually empty. Except when this one person comes to this office for a week every here and there, I get booted, and have to share a 7x7 (feet, for you non-USAians) cube with another guy. It's a total lack of respect for someone doing quality work and it just pisses me off when it happens.

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
pragma once makes a bigger difference as the project grows in size (I tend to seperate it out into libraries before that though).

I must agree, although partialy. When the project really grows its much easier and faster to start exporting code to libraries (why would you want to recompile debugged code over and over?).

However I prefer using the #ifndef construct since its more portable and I find it more readable and informative than the #pragma once directive.

The #ifndef gives the reader a reminder of which file he is currently reading and also its two lines in stead of one - making it easier to spot.

As some in here say it is fairly easy to correct the #pragma if you port to a compiler that does not support this, but why bother? If you use the #ifndef construct from the very beginning of your coding then that should pose no problem at all.

I use this template for my header files:

#include "app.h" //application constants and includes such as stl

#ifndef CMyClass_h
#define CMyClass_h

class CMyClass{
private:
public:
};

#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by bobstevens
If you really want to speed up your compiles GET RID OF ALL THOSE COMMENTS.

This is actually not as dumb as it sounds ;)
Placing the module documentation in the source instead of the header might not be such a bad idea, especially on some of the more gigantic projects out there where every little detail needs to be thoughly documented and placed in a predefined commenting 'frame'.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!