没有明确作用域的强类型枚举?
我想要强枚举类型。 C++0x 具有此功能,但不幸的是它们还需要显式范围界定:
enum class E {e1, e2, e3};
E x = E::e1; //OK
E y = e1; //error
有时这是可取的,但有时它只是不必要的冗长。标识符本身可能足够唯一,或者枚举可能已经嵌套在类或命名空间内。
所以我正在寻找解决方法。在周围范围内声明枚举值的最佳方法是什么?
I want strong enum types. C++0x has this feature but unfortunately they also require explicit scoping:
enum class E {e1, e2, e3};
E x = E::e1; //OK
E y = e1; //error
Sometimes this is desirable, but sometimes it's just unnecessarily verbose. The identifiers might be unique enough by themselves or the enum might already be nested inside a class or namespace.
So I'm looking for a workaround. What would be the best way to declare the enum values also in the surrounding scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望值在周围范围内可见,只需添加几个常量:
If you want the values visible in the surrounding scope, just add a couple of constants: