在 C++ 中,什么是“命名空间别名”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1211399/
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-27 19:11:45  来源:igfitidea点击:

In C++, what is a "namespace alias"?

c++namespaces

提问by Martin B

What is a "namespace alias" in C++? How is it used?

什么是 C++ 中的“命名空间别名”?它是如何使用的?

回答by Martin B

A namespace alias is a convenient way of referring to a long namespace name by a different, shorter name.

名称空间别名是一种通过不同的较短名称来引用长名称空间名称的便捷方式。

As an example, say you wanted to use the numeric vectors from Boost's uBLAS without a using namespacedirective. Stating the full namespace every time is cumbersome:

例如,假设您想在没有using namespace指令的情况下使用 Boost 的 uBLAS 中的数字向量。每次都声明完整的命名空间很麻烦:

boost::numeric::ublas::vector<double> v;

Instead, you can define an alias for boost::numeric::ublas-- say we want to abbreviate this to just ublas:

相反,您可以定义一个别名boost::numeric::ublas- 假设我们想将其缩写为ublas

namespace ublas = boost::numeric::ublas;


ublas::vector<double> v;

回答by user2168377

Quite simply, the #define won't work.

很简单,#define 不起作用。

namespace Mine { class MyClass { public: int i; }; }
namespace His = Mine;
namespace Yours { class Mine: public His::MyClass { void f() { i = 1; } }; }

Compiles fine. Lets you work around namespace/class name collisions.

编译得很好。让您解决命名空间/类名冲突。

namespace Nope { class Oops { public: int j; }; }
#define Hmm Nope
namespace Drat { class Nope: public Hmm::Oops { void f () { j = 1; } }; }

On the last line, "Hmm:Oops" is a compile error. The pre-processor changes it to Nope::Oops, but Nope is already a class name.

在最后一行,“Hmm:Oops”是一个编译错误。预处理器将其更改为 Nope::Oops,但 Nope 已经是一个类名。

回答by Justin Time - Reinstate Monica

Also note that namespace aliases and using directives are resolved at compile time, not run time. (More specifically, they're both tools used to tell the compiler where else to look when resolving names, if it can't find a particular symbol in the current scope or any of its parent scopes.) For example, neither of these will compile:

另请注意,命名空间别名和 using 指令在编译时解析,而不是运行时解析。(更具体地说,它们都是用于告诉编译器在解析名称时查看其他位置的工具,如果它在当前作用域或其任何父作用域中找不到特定符号。)例如,这些都不会编译:

namespace A {
    int foo;
    namespace AA {
        int bar;
    } // namespace AA
    namespace AB {
        int bar;
    } // namespace AB
} // namespace A
namespace B {
    int foo;
    namespace BA {
        int bar;
    } // namespace BA
    namespace BB {
        int bar;
    } // namespace BB
} // namespace B

bool nsChooser1, nsChooser2;
// ...

// This doesn't work.
namespace C = (nsChooser1 ? A : B);
C::foo = 3;

// Neither does this.
// (Nor would it be advisable even if it does work, as compound if-else blocks without braces are easy to inadvertently break.)
if (nsChooser1)
    if (nsChooser2)
        using namespace A::AA;
    else
        using namespace A::AB;
else
    if (nsChooser2)
        using namespace B::BA;
    else
        using namespace B::BB;

Now, a curious mind may have noticed that constexprvariables are also used at compile time, and wonder whether they can be used in conjunction with either an alias or a directive. To my knowledge, they cannot, although I may be wrong about this. If you need to work with identically-named variables in different namespaces, and choose between them dynamically, you would have to use references or pointers.

现在,一个好奇的头脑可能已经注意到constexpr变量也在编译时使用,并且想知道它们是否可以与别名或指令结合使用。据我所知,他们不能,尽管我可能错了。如果您需要使用不同命名空间中的同名变量,并在它们之间动态选择,则必须使用引用或指针。

// Using the above namespaces...
int& foo = (nsChooser1 ? A::foo : B::foo);

int* bar;
if (nsChooser1) {
    if (nsChooser2) {
        bar = &A::AA::bar;
    } else {
        bar = &A::AB::bar;
    }
} else {
    if (nsChooser2) {
        bar = &B::BA::bar;
    } else {
        bar = &B::BB::bar;
    }
}

The usefulness of the above may be limited, but it should serve the purpose.

以上内容的用处可能有限,但应该可以达到目的。

(My apologies for any typoes I may have missed in the above.)

(对于我在上面可能遗漏的任何错字,我深表歉意。)

回答by kiriloff

More on this topic http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-1-of-n

有关此主题的更多信息http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-1-of-n

It is all about choosing an alias for a looong namespace name, such as:

这就是为 looong 命名空间名称选择别名,例如:

namespace SHORT = NamespaceFirst::NameSpaceNested::Meow

namespace SHORT = NamespaceFirst::NameSpaceNested::Meow

Then later, you can typedef

稍后,您可以 typedef

typedef SHORT::mytype

typedef SHORT::mytype

instead of

代替

typedef NamespaceFirst::NameSpaceNested::Meow::mytype

typedef NamespaceFirst::NameSpaceNested::Meow::mytype

This syntax only works for namespaces, cannot include classes, types after the namespace NAME =

此语法仅适用于命名空间,不能包含类和类型 namespace NAME =

回答by mai

Namespace is used to prevent name conflicts.

命名空间用于防止名称冲突。

For example:

例如:

namespace foo {
    class bar {
        //define it
    };
}

namespace baz {
    class bar {
        // define it
    };
}

You now have two classes name bar, that are completely different and separate thanks to the namespacing.

您现在有两个类名称栏,由于命名空间,它们完全不同并且是分开的。

The "using namespace" you show is so that you don't have to specify the namespace to use classes within that namespace. ie std::string becomes string.

您显示的“使用命名空间”是为了让您不必指定命名空间来使用该命名空间中的类。即 std::string 变为字符串。

my resource: https://www.quora.com/What-is-namespace-in-C++-1

我的资源:https: //www.quora.com/What-is-namespace-in-C++-1