C++ 重命名命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6108704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:30:31  来源:igfitidea点击:

Renaming namespaces

c++namespacesalias

提问by ereOn

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++ ?"

我已经使用 C++ 很长时间了,但今天早上我刚刚遇到一个问题,我无法回答:“是否可以在 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:

现在,如果我有一个引用 的旧代码库SomeClass,我可以通过添加以下内容来快速(并且肮脏地)“修复”更改:

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

But is there a way to import everythingfrom _new::nestedinto oldwithout having to typedefexplicitely every type ?

但是有没有一种方法可以将所有内容_new::nestedinto 导入old而不必typedef明确地导入每种类型?

Something similar to Python import * from ....

类似于 Python 的东西import * from ...

Thank you.

谢谢你。

回答by Xeo

using namespace new::nested;

Example at Ideone.

在 Ideone 的例子

Or if you actually want a real alias:

或者,如果您确实想要一个真正的别名:

namespace on = one::nested;

Example at Ideone.

在 Ideone 的例子

回答by Xeo

This:

这个:

namespace old = newns::nested;

would seem to be what you want.

似乎是你想要的。