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
Renaming namespaces
提问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::nested
into old
without having to typedef
explicitely every type ?
但是有没有一种方法可以将所有内容从_new::nested
into 导入old
而不必typedef
明确地导入每种类型?
Something similar to Python import * from ...
.
类似于 Python 的东西import * from ...
。
Thank you.
谢谢你。
回答by Xeo
using namespace new::nested;
Or if you actually want a real alias:
或者,如果您确实想要一个真正的别名:
namespace on = one::nested;
回答by Xeo
This:
这个:
namespace old = newns::nested;
would seem to be what you want.
似乎是你想要的。