ereOn
ereOn

Reputation: 55736

Renaming namespaces

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.

Upvotes: 37

Views: 34057

Answers (2)

user2100815
user2100815

Reputation:

This:

namespace old = newns::nested;

would seem to be what you want.

Upvotes: 34

Xeo
Xeo

Reputation: 131799

using namespace new::nested;

Example at Ideone.

Or if you actually want a real alias:

namespace on = one::nested;

Example at Ideone.

Upvotes: 69

Related Questions