Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorten nested namespace names

Tags:

c++

namespaces

If you use nested namespaces, declarations in header files can get very long and unreadable.

//header1
namespace test { namespace test1 {
class Test {};
} } //namespace

In header2 of the program:

#include "header1"

namespace test2 {

class Test1 {
  void test(test::test1::Test &test) {}
  void test1(test::test1::Test &test) {}
  void test2(test::test1::Test &test1, test::test1::Test &test2) {}
};

}

Are there any possibilities to shorten the names in header2?

like image 257
bighil Avatar asked Nov 02 '09 14:11

bighil


People also ask

Can namespaces be nested in C++?

In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace.

What is nesting namespaces in Java?

When a Namespace is declared inside the other namespace that declaration is called nesting namespaces. When a Namespace is declared inside the other namespace that declaration is called nesting namespaces. You can nest namespaces to any level you want.

What is an example of a named Namespace?

An example of this is the std namespace which is declared in each of the header files in the standard library. Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined.

What is the difference between namespace inner and namespace outer?

For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace. In the line “int z = x”, x refers to outer::x. If x would not have been in outer then this x would have referred to x in global namespace.


1 Answers

Here is my favorite technique:

#include "header1"

namespace test2 {

class Test1 {
 private:
  typedef ::test::test1::Test MeaningfulName;

  void test(MeaningfulName &test) {}
  void test1(MeaningfulName &test) {}
  void test2(MeaningfulName &test1, MeaningfulName &test2) {}
};

}

I make my typedef aliases private, but I put them right at the beginning of the class declaration. It doesn't matter that they're private to the rest of the program because nobody will be using the aliased name, they will be using the actual type name or their own alias for the name.

I also really prefer to use anchored namespace names to avoid later surprises. My rule for this is that I always use an anchored name unless the namespace is one I control and/or is part of the current project or package or whatever. Then I will use the shortest possible relative name. If that relative name would start from the root namespace, I still often use an anchored name.

The main problem is the digraph <: which will crop up in template declarations a lot once you start using anchored names more often. You have to put in a space to avoid it, especially since digraph processing happens at a really early stage and can give you some very weird error messages.

like image 139
Omnifarious Avatar answered Sep 25 '22 02:09

Omnifarious