include guards

This is a discussion on include guards within the C++ Programming forums, part of the General Programming Boards category; I've finally made it to the creation of classes, and my book (Gaddis) suggests as include guard only #ifndef ... ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    include guards

    I've finally made it to the creation of classes, and my book (Gaddis) suggests as include guard only #ifndef ... #endif. But since I've seen #pragma once in various spots and it is used when MS Visual C++ generates an empty class, I looked that up in Wikipedia and learned that it's roughly equivalent to #ifndef ... #endif, saves a bit of space and has a few other advantages that I don't understand but also some disadvantages such as not being universally supported, possibly a bit more error-prone.

    So, I just wanted to ask what more experienced programmers prefer as include guard when declaring classes.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I always use:
    Code:
    #ifndef SOME_LONG_STRING
    #define SOME_LONG_STRING
    ...
    #endif // SOME_LONG_STRING
    Since #pragma once is mostly a Microsoft thing, I just delete it and use include guards instead.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    I never employ "#pragma once", and only ever employ include guards.

    Pragmas are specific to particular compilers; include guards are guaranteed by the standard to work with all compilers (or, more accurately, preprocessors).
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Some say you can get the best of both, by er ... using both.
    Unknown pragmas are supposed to be ignored by the compiler

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by iMalc View Post
    Some say you can get the best of both, by er ... using both.
    If two approaches achieve the same purpose, but one is not guaranteed to work when you change compilers, better to use the other.
    Quote Originally Posted by iMalc View Post
    Unknown pragmas are supposed to be ignored by the compiler
    Different compilers are also allowed to do different things, when given the same pragma. The whole intent of pragmas is that they do compiler-specific things.

    I have heard - but not confirmed - reports of a compiler that supports a #pragma once statement to ensure the body of while loops are executed at least once (i.e. making it equivalent to do .... while).
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    242
    Wow! I kind of expected varying opinions on this one, but it sounds pretty much unanymous for #ifndef, etc. over #pragma once.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, the #ifdef guard is the standard and accepted way of doing it. #pragma once is an MS invention that they use to ensure that the compiler needn't even open the file again. This can save time.
    However, every compiler but Microsoft's has been taught to recognize the #ifdef pattern and apply the same optimization. Thus, #pragma once wouldn't have any benefit besides being one line instead of three.
    GCC supports #pragma once, but not very well. A performance test by someone in Boost has shown that sometimes GCC gets slower if files have #pragma once in addition to include guards.
    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

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    GCC also allows the #import directive as a replacement for #include and it has an include guard, but again, it's not standard so you cannot count on it for portability. In fact, VC uses #import for including it's own special files.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by CornedBee View Post
    GCC supports #pragma once, but not very well. A performance test by someone in Boost has shown that sometimes GCC gets slower if files have #pragma once in addition to include guards.
    I suspect that would be because include guards are managed by the preprocessor, and the working of #pragma once is handled by the compiler proper. Net effect of using both is that the relevant workings get executed twice - particularly as gcc generally implements the preprocessing and compilation as distinct phases (in fact, in separate executables that are invoked by the gcc driver).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use this part of dirent.h
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2009, 07:51 AM
  2. Weird, cards not outputting correctly
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2007, 02:26 PM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 11:44 AM
  4. dont konw what to do next.
    By negevy in forum C Programming
    Replies: 29
    Last Post: 09-09-2005, 03:06 PM
  5. #includes don't seem to be working
    By Inquirer in forum C++ Programming
    Replies: 12
    Last Post: 08-05-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21