Renaming namespaces

ereOn picture ereOn · May 24, 2011 · Viewed 22.1k times · Source

I've been doing C++ for a long time now but I just faced a question this morning to which I couldn't give an answer: "Is it possible to create aliases for namespaces in C++ ?"

Let me give an example. Let's say I had the following header:

namespace old
{
  class SomeClass {};
}

Which, for unspecified reasons had to become:

namespace _new
{
  namespace nested
  {
    class SomeClass {}; // SomeClass hasn't changed
  }
}

Now if I have an old code base which refers to SomeClass, I can quickly (and dirtily) "fix" the change by adding:

namespace old
{
  typedef _new::nested::SomeClass SomeClass;
}

But is there a way to import everything from _new::nested into old without having to typedef explicitely every type ?

Something similar to Python import * from ....

Thank you.

Answer

Xeo picture Xeo · May 24, 2011
using namespace new::nested;

Example at Ideone.

Or if you actually want a real alias:

namespace on = one::nested;

Example at Ideone.