1

In Java, I find it very straightforward to use namespaces. For each qualified identifier Ident I use in a source file, I put an import ns1.ns2.ns2.ns3.ns4.Ident; at the top of the file. Then I can use (short) unqualified names everywhere in my source code. The import statement cannot cause any problems, because it applies only to the file in which it is written down.

However, I'm not quite sure how to get rid of namespace qualifiers in C++ the best way.

The most obivous solution would probably be the using and using namespace statement. However, that seems to be a rather bad solution, at least in the case of header files, because the using statements are not restricted to the single file where they are written down. So using is ruled out in the case of e.g. slim libraries consisting only of header files with the implementions directly inside or in the case of header files in general.

Another option, which I use so far, is to add for each qualified name I use in a class a corresponding typedef in the private section of the class. So when comparing this approach to Java, I basically take the whole import statement list, replace the import with typedef and place it in the class declaration.

However, I don't really like this approach, because users of my classes - strictly speaking -don't know the types of return values and parameter values, because the types in the method declarations are private types of the corresponding classes.

OK, now we could make all this typedef stuff public. But that's probably a not so good idea, as we would redefine each type many many times. Just think of a struct ns1::ns2::ns3::MyStructure and two Classes MyClassA and MyClassB. Both classes have a method which actually should take as parameter an instance of ns1::ns2::ns3::MyStructure. But because every class redefines the types it uses to get rid of the long qualified names, the two methods now take parameters of "different" types, say MyClassA::MyStructure and MyClassB::MyStructure. It becomes even catchier when we have a third class MyClassC which works with an instance of MyStructure and need to call both methods with it. Should this class declare this instance with type MyClassA::MyStructure, MyClassB::MyStructure or MyClassC::MyStructure?

Well, what I simply want to know is: What is the best practise for getting rid of the namespace qualifiers?

user1494080
  • 2,064
  • 2
  • 17
  • 36
  • See http://stackoverflow.com/questions/223021/whats-the-scope-of-the-using-declaration-in-c – JBentley Jan 15 '14 at 01:33
  • And this: http://stackoverflow.com/questions/4362831/scoped-using-directive-within-a-struct-class-declaration – JBentley Jan 15 '14 at 01:36
  • To sum up the answers from the other two questions: There is no such thing as class/file-scoped using statements in C++. A workaround could be to wrap the using statements and the class declaration/definition into a namespace dummy { ... } and place a typedef dummy::MyClass MyClass outside the dummy namespace. For me, it seems a bit nasty to wrap each file into a dummy namespace. I hoped there would be a common "design pattern" (from Effective C++ or similar books) for my problem. Are "dummy namespaces" really the common, wide-spread solution? – user1494080 Jan 15 '14 at 02:20

0 Answers0