namepaces and header file defines

This is a discussion on namepaces and header file defines within the C++ Programming forums, part of the General Programming Boards category; well namespaces are a good inclusion in c++ to avoid name pollution. but take this example: Code: #ifndef MYHEADER #define ...

  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
    21,630
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,259
    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

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