Should C++ namespace aliasing be used in header files?

I do it with unnamed namespaces this way:

#include <whatyouneed>
...
namespace {

typedef ...
using ..
namespace x = ...

// anything you need in header but shouldn't be linked with original name

}

// normal interface
class a: public x::...

If you put a namespace alias into your header this alias will become part of your (public) API.

Sometimes this technique is used to do ABI compatible versioning (or at least to make breakage visible) like this:

namespace lib_v1 { ... }
namespace lib_v2 { ... }
namespace lib = lib_v2;

or more commonly:

namespace lib {
   namespace v1 {}
   namespace v2 {}
   using namespace v2;
}

On the other hand if you do it just to save some typing it is probably not such a good idea. (Still much better than using a using directive)

Tags:

C++

Namespaces