如何将枚举导入到 C++ 中的不同命名空间中?
我在命名空间中有一个枚举,我想像在不同的命名空间中一样使用它。直觉上,我认为我可以使用“using”或“typedef”来完成此操作,但实际上都不起作用。证明这一点的代码片段,在 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;
}
问题是枚举本身已导入,但它的任何元素都没有导入。不幸的是,我无法在不破坏许多其他现有代码的情况下将原始枚举更改为包含在额外的虚拟命名空间或类中。我能想到的最好的解决方案是手动重现枚举:
namespace buzz
{
enum bar
{
A = foo::A
};
}
但它违反了 DRY 原则。有更好的办法吗?
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. Code snippet to prove it, tested on GCC and 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. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您确实需要这样做,请尝试
using namespace foo;
而不是using foo::bar;
。然而,将枚举封装在类或另一个命名空间中是一个更好的主意。If you really need to do this, try
using namespace foo;
instead ofusing foo::bar;
. However, it's a much better idea to encapsulate the enum in a class or in another namespace.从 C++11 开始,您可以使用
枚举类
。导入enum class
导入其所有值:上面的代码成功编译:http: //coliru.stacked-crooked.com/a/2119348acb75d270。
Starting from C++11 you can use
enum class
. Importingenum class
imports all its values:The code above successfully compiles: http://coliru.stacked-crooked.com/a/2119348acb75d270.
C++20 中终于有一个很好的解决方案。
using enum
声明将所有枚举值导入到当前作用域中。在您的示例中:请注意,您仍然需要一个简单的
using
声明才能在范围内获取枚举的名称。There is finally a good solution for this in C++20. The
using enum
declaration imports all of the enumeration values into the current scope. In your example:Note that you still also need a plain
using
declaration in order to get the enumeration’s name in the scope.将现有命名空间包装在嵌套命名空间中,然后在原始命名空间中“使用”该命名空间。
Wrap the existing namespace in a nested namespace which you then "use" in the original namespace.
虽然我最喜欢 Mark B 的方法,因为它不会破坏现有代码,但您还可以执行以下操作:
While i like the approach of Mark B best, because it doesn't break existing code, you can also do the following:
这里的问题是 using 声明仅引入枚举的名称,而不是其值的名称。 枚举不是作用域,也不带有枚举的名称
枚举器。我认为不可能自行导入枚举值。尝试将枚举包装在结构/命名空间中并使用它。
The problem here is that the using declaration pulls in only the name of the enum, and not the names of its values. 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.