简体   繁体   English

C ++关于名称空间和别名的混淆:没有嵌套,没有using指令

[英]C++ Confusion about namespaces and aliasing: no nesting and no using directives

I'm not trying to nest a namespace, I'm trying not to use a using directive in my header. 我不是在尝试嵌套名称空间,而是在标题中不使用using指令。 I'm trying to alias a namespace 'name' with another name. 我正在尝试使用另一个名称来别名命名空间“名称”。 In my header I have a global empty namespace with a full name. 在标题中,我有一个全名的全局空名称空间。 I want to assign another namespace to this name and then populate it with my source. 我想为此名称分配另一个名称空间,然后用我的源填充它。 I'm not sure what the appropriate syntax is for doing such a thing. 我不确定执行此操作的合适语法是什么。 Here is an example of what I have in my header file. 这是我的头文件中的示例。

#ifndef SOME_HEADER_H
#define SOME_HEADER_H

namespace SomeLongNamespaceHere {} // Empty Global
namespace SLNH = SomeLongNamespaceHere; // Alias
namespace slnh = SLNH; // another lowercase alias

namespace slnh { // Compiler Error:
    // My Declarations Here!
    class foo{};    
}

#endif // SOME_HEADER_H

1>------ Build started: Project: FileTester, Configuration: Debug Win32 ------
1>  include.cpp
1>c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\include.h(8): error C2757: 'mfbid': a symbol with this name already exists and therefore this name cannot be used as a namespace name
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I understand the compiler error and that is not the issue or the problem. 我了解编译器错误,但这不是问题所在。


However when I do this: 但是,当我这样做时:

namespace slnh{}

namespace slnh{
    class foo{};
}

It compiles just fine. 它编译就好了。 I'm not sure if I'm misunderstanding what a namespace alias exactly is and does or if I'm not using the proper syntax. 我不确定是否误解了名称空间别名的确切含义和用途,或者是否使用的语法不正确。


I am trying to use the shorter version in place of the longer version so that the user can just use the shorter version directly and when they hover over the shorter namespace it will automatically resolve back to the longer namespace and can be seen through features such as MSVS's intellisense or something similar. 我正在尝试使用较短的版本代替较长的版本,以便用户可以直接使用较短的版本,并且当它们悬停在较短的名称空间上时,它将自动解析回较长的名称空间,并可以通过诸如以下功能查看MSVS的智能感知或类似的东西。 How can I achieve or mimic the behavior that I have described above? 如何实现或模仿我上面描述的行为?

You can reopen namespaces as many times as you want that is why this works: 您可以根据需要多次重新打开命名空间,这就是这样做的原因:

namespace slnh{}

namespace slnh{
    class foo{};
}

But you cannot redeclare a namespace with the same name of an alias, it is a conflict: 但是您不能使用别名相同的名称重新声明名称空间,这是冲突的:

namespace SomeLongNamespaceHere {} // Empty Global
namespace SLNH = SomeLongNamespaceHere; // Alias
namespace slnh = SLNH; // another lowercase alias

//This namespace clashes with the alias above.
namespace slnh { // Compiler Error:
// My Declarations Here!
    class foo{};    
}

It does not work because you are associating the same name to a namespace and to a namespace alias. 它不起作用,因为您要将同一个名称与名称空间和名称空间别名相关联。 This would clash AFAIK. 这会冲突AFAIK。

EDIT: You cannot in anyway open a namespace with an aliased name. 编辑:无论如何您都不能使用别名来打开名称空间。 So it cannot be done. 因此无法完成。

Maybe a possible workaround (not recommended at all) is to use a shorter namespace and put a using directive: 可能的解决方法(根本不建议使用)是使用较短的名称空间并放入using指令:

namespace SomeLongNamespace {
    using namespace SLN;
}

namespace SLN {
   //Populate here
   class MyClass {};
}


SLN::MyClass ...; //works 
SomeLongNamespace::MyClass ...; //works

Another ugly workaround would be using macros (not recommended either): 另一个丑陋的解决方法是使用宏(也不推荐):

#define SLN SomeLongNamespace

This can be bad for just saving a few keystrokes and macros are evil so do not do it. 仅保存一些击键就可能是不好的,而宏是邪恶的,所以不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM