问题
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.
回答1:
using namespace new::nested;
Example at Ideone.
Or if you actually want a real alias:
namespace on = one::nested;
Example at Ideone.
回答2:
This:
namespace old = newns::nested;
would seem to be what you want.
来源:https://stackoverflow.com/questions/6108704/renaming-namespaces