How to shorten long (qualified) identifiers in C++?

908 views Asked by At

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?

1

There are 1 answers

0
Speaker-to-Animals On

All the namespaces I generate are nested inside my username namespace, which has a short name.

You could generate a semantically meaningful namespace name inside your username namespace which then serves as an alias for some really long path, thereby helping insulate it from variable collisions.

You don't need to invoke the dangers of type definitions for this. You can simply use a semantically meaningful namespace alias (https://en.cppreference.com/w/cpp/language/namespace_alias) instead:

  • suppose my root namespace is mr_user and my project name is colored_balloons.
namespace mr_user_cb01_psp =
mr_user::colored_balloons::my_package_namespace::my_subpackage_namespace;

mr_user_cb01_psp::MyClass myClass;

Depending on how you use this, to avoid collisions in the long term, you should probably be tracking your namespace aliases in a safe place apart from your projects. Most of the time I don't bother with this: I simply use the original namespace name in its fully qualified form.