Thread: namepaces and header file defines

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    namepaces and header file defines

    well namespaces are a good inclusion in c++ to avoid name pollution.
    but take this example:
    Code:
    #ifndef MYHEADER
    #define MYHEADER
    namespace mynamespace2007 {
     class myclass {
      int myint;
     };
    }
    #endif
    see my class is secure in namespace, but what about the #defined sym used to prevent
    multiple inclusion. is it not as likely as a class to make a name conflict? is there a way to avoid
    header defines name pollution?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use a really long and randomized name. MS VC++ should create a long GUID name for you, but when doing it myself, I just do something like this:

    <FILENAME>_H_INCLUDED_<MONTH>_<DAY>_<YEAR>
    You can include Hour, Minute & Second if you want, but that's too much typing for me.

    Ex.: FILEUTIL_H_INCLUDED_OCT_15_2007

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... but it seems to me that if mynamespace2007 is a suitable namespace name, MYNAMESPACE2007_MYHEADER_H should be a suitable name for a header inclusion guard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Macros generally ignore namespaces. That's one of the reasons to avoid them as much as possible.

    In the case of header guards, where usability is not an issue, it's easy to use long, unambiguous names. I typically form my header names of project name, full in-project path of the file, and the suffix _SR_H to distinguish it from other people's headers.
    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

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    From Qt sources ...
    Code:
    #ifndef QBACKINGSTORE_P_H
    #define QBACKINGSTORE_P_H
    ; ...
    #endif
    
    #ifndef QPAINTER_H
    #define QPAINTER_H
    ; ...
    #endif
    
    #ifndef QCOLOR_H
    #define QCOLOR_H
    ; ...
    #endif
    They don't seem to worry too much about it at Qt.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Oh? How many other projects would you expect to #define a pile of macro names starting with the letter Q, followed by a word in upper case, and ending with _H? And, among those, how many would you expect to #define a macro named QPAINTER_H?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by grumpy View Post
    Oh? How many other projects would you expect to #define a pile of macro names starting with the letter Q, followed by a word in upper case, and ending with _H? And, among those, how many would you expect to #define a macro named QPAINTER_H?
    I could say the same thing about names of standard C/C++ classes & functions which are all lower-case. I always use mixed case for my stuff. But then again, if I use 3rd party stuff, they might use all lower-case (or maybe the same name as one of my own function/classes). Which is why we have namespaces to make the names longer and harder to collide (and we have the using keyword so we can type less in places where we know it's safe to do so).

Popular pages Recent additions subscribe to a feed