没有明确作用域的强类型枚举?

发布于 2024-11-28 07:06:57 字数 237 浏览 0 评论 0原文

我想要强枚举类型。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2024-12-05 07:06:57

如果您希望值在周围范围内可见,只需添加几个常量:

enum class E {e1, e2, e3};

const E e1 = E::e1;
const E e2 = E::e2;
const E e3 = E::e3;

If you want the values visible in the surrounding scope, just add a couple of constants:

enum class E {e1, e2, e3};

const E e1 = E::e1;
const E e2 = E::e2;
const E e3 = E::e3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文