简体   繁体   English

如何在C ++中将枚举导入不同的命名空间?

[英]How do you import an enum into a different namespace in C++?

I have an enum in a namespace and I'd like to use it as if it were in a different namespace. 我在命名空间中有一个枚举,我想使用它,就像它在不同的命名空间中一样。 Intuitively, I figured I could use 'using' or 'typedef' to accomplish this, but neither actually work. 直观地说,我认为我可以使用'using'或'typedef'来实现这一目标,但实际上都没有。 Code snippet to prove it, tested on GCC and Sun CC: 用于证明它的代码片段,在GCC和Sun CC上测试:

namespace foo
{

enum bar {
    A
};

}

namespace buzz
{
// Which of these two methods I use doesn't matter,
// the results are the same.
using foo::bar;
//typedef foo::bar bar;
}

int main()
{
    foo::bar f; // works
    foo::bar g = foo::A; // works

    buzz::bar x; // works
    //buzz::bar y = buzz::A; // doesn't work
    buzz::bar z = foo::A;
}

The problem is that the enum itself is imported but none of its elements. 问题是枚举本身是导入的,但没有导入。 Unfortunately, I can't change the original enum to be encased in an extra dummy namespace or class without breaking lots of other existing code. 不幸的是,我无法将原始枚举更改为包含在额外的虚拟命名空间或类中,而不会破坏许多其他现有代码。 The best solution I can think of is to manually reproduce the enum: 我能想到的最好的解决方案是手动重现枚举:

namespace buzz
{
enum bar
{
    A = foo::A
};
}

But it violates the DRY principle . 但它违反了DRY原则 Is there a better way? 有没有更好的办法?

Wrap the existing namespace in a nested namespace which you then "use" in the original namespace. 将现有命名空间包装在嵌套命名空间中,然后在原始命名空间中“使用”。

namespace foo
{
    namespace bar_wrapper {
        enum bar {
            A
        };
    }
    using namespace bar_wrapper;
}

namespace buzz
{
    using namespace foo::bar_wrapper;
}

While i like the approach of Mark B best, because it doesn't break existing code, you can also do the following: 虽然我最喜欢Mark B的方法,因为它不会破坏现有代码,您还可以执行以下操作:

namespace foo {
 enum bar {
  A, B [..]
 };
}

namespace buzz {
 using foo::bar;
 using foo::A;
 using foo::B;
 [..]
}

The problem here is that the using declaration pulls in only the name of the enum, and not the names of its values. 这里的问题是using声明只引入枚举的名称,而不是其值的名称。 Enum's aren't scopes and don't carry along the names of the enumerators . 枚举不是范围,也不带有普查员的姓名 I don't think it is possible to import the enum values themselves. 我认为不可能自己导入枚举值。 Try wrapping the enum in a struct/namespace and use it. 尝试将enum包装在struct / namespace中并使用它。

Starting from C++11 you can use enum class . 从C ++ 11开始,您可以使用enum class Importing enum class imports all its values: 导入enum class导入其所有值:

namespace foo
{

enum class bar {
    A
};

}

namespace buzz
{
using foo::bar;
}

int main()
{
    foo::bar f;
    foo::bar g = foo::bar::A;

    buzz::bar x;
    buzz::bar y = buzz::bar::A;
    buzz::bar z = foo::bar::A;
}

The code above successfully compiles: http://coliru.stacked-crooked.com/a/2119348acb75d270 . 上面的代码成功编译: http//coliru.stacked-crooked.com/a/2119348acb75d270

If you really need to do this, try using namespace foo; 如果你真的需要这样做,请尝试using namespace foo; instead of using foo::bar; 而不是using foo::bar; . However, it's a much better idea to encapsulate the enum in a class or in another namespace. 但是,将枚举封装在类或另一个命名空间中是一个更好的主意。

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

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