Renaming namespaces

﹥>﹥吖頭↗ 提交于 2019-11-29 16:08:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!