2

I am using same class name in two namespaces, say A and B. Should the include guards be unique while declaring the classes with different namespaces too?

I mean can't there be two files names AFile.h (in different directories) with same include guards and declaring different namespaces?

 //File 1:

 #ifndef AFILE_H 

 #define AFILE_H

 namespace A { 

   class CAFile {...

   }; 

 };

 #endif

//File 2:

 #ifndef AFILE_H 

 #define AFILE_H

 namespace B { 

   class CAFile {...

   }; 

 };

 #endif
csguy
  • 1,354
  • 2
  • 17
  • 37
Sulla
  • 7,631
  • 9
  • 45
  • 71

4 Answers4

10

Your guards need to be different if some code (directly or indirectly) needs to see both A::CAFile and B::CAfile.

The include guards are dealt with by the preprocessor, that has no knowledge at all of classes (let alone namespaces). If both these files are included when processing a c++ file, and they have the same header guards, only one of the declarations will remain in the preprocessed source that the compiler will see.

Look at things like Boost files, they have some conventions for the header guards (if I remember correctly).

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    In general, since it's hard to predict the use of files (through maintenance) it is better for header guards to be unique throughout the application, which includes dependencies. I don't remember if BOOST has formal guidelines or not, but their style is pretty heavy :) Other technics involve using a GUID or the `#pragma once` directive – Matthieu M. Mar 04 '11 at 08:30
5

Include guards only affect the preprocessor and the preprocessor doesn't know C++ and completely ignores namespaces. So guards should be unique to a file, not to a namespace.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • His point was that it's not good enough to just have a guard called _FILE_H for file.h since some other include could use that same guard. While the preprocessor doesn't care about namespaces, it does make sense to make sure that the guard for vector.h in the std namesapce is guarded differently than vector.h in some other namespace. – Andrew White Mar 04 '11 at 07:25
  • @Andrew White: Okay, you're right, but this again boils down to "unique for each file on the filesystem" and has nothing to do with namespaces. Yes, he likely should add prefixes to the guards depending on where the file is located. – sharptooth Mar 04 '11 at 07:29
  • The truth is that include guards should be unqiue across namespaces *because* the preprocessor ignores C++ namespaces. – Doc Brown Mar 04 '11 at 07:29
  • I'm surprised that an anonymous idiot kid used his time to downvote this answer. Do the drive-by downvoters pick answers at random? That's the only explanation I can see. – Cheers and hth. - Alf Mar 04 '11 at 07:33
  • @Alf P. Steinbach: Maybe your reasoning is right, but IMO this attitude to such cases is much better: http://knowyourmeme.com/memes/keep-calm-and-carry-on – sharptooth Mar 04 '11 at 07:42
  • @Alf P.Steinbach.: I did not downvote this answer, but as I pointed out in my previous comment, the conclusion drawn in this answer is wrong, quite the opposite is true. So why are some others voting this up? – Doc Brown Mar 04 '11 at 08:19
  • @Doc Brown: I honestly don't understand what's so wrong in my answer - I state that guards should be unique to a file and that's all. – sharptooth Mar 04 '11 at 08:29
  • @DocBrown: the conclusion in this answer is correct. moreover, it's the same conclusion as in the currently selected "solution". and in this answer it is more succinctly expressed. i think that may be why people vote it up. i think the reason why the currently selected "solution" is more upvoted is because its explanation includes one intermediate step of reasoning, thus making each step more accessible to the mindless reader. – Cheers and hth. - Alf Mar 04 '11 at 09:35
0

In short, it's probably a good idea. Here is how the GCC does their's...

#ifndef _GLIBCXX_VECTOR
#define _GLIBCXX_VECTOR 1

I don't know about using a namespace per say but the include guard should be unique to your packaged interface (which could all be within one namespace or spread across many).

Andrew White
  • 52,720
  • 19
  • 113
  • 137
0

Personally, I've been using #pragma once because it's supported on the compilers I care about and you can avoid the kind of problems you mention here. If you want to use #include guards, then you may need to be clever about it. Otherwise #includeing Foo/header.h may not work because you already #included Bar/header.h.

I don't agree with the style guide in other cases, but Google recommends <PROJECT>_<PATH>_<FILE>_H_. Although that does mean that if you copy files around to different paths you're going to have to update the #include guard.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69