我们真的需要 C++11 中的“枚举类”吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6936030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:57:52  来源:igfitidea点击:

Do we really need "enum class" in C++11?

c++enumsc++11language-lawyer

提问by iammilind

When we have,

当我们有,

struct E { enum E_ { HELLO }; }; // 'E' is inheritable

then why do we need,

那我们为什么需要,

enum class E { HELLO };  // 'E' is not inheritable

IMO 2nd version doesn't offer more features than the 1st. I don't think that enum classis introduced just to save 2 curly braces {};! Am I missing any important aspect ?

IMO 2nd 版本没有提供比 1st 更多的功能。我不认为这enum class是为了节省 2 个花括号而引入的{};!我是否遗漏了任何重要方面?

As a minor question, is there any difference between enum classand enum structother than syntax (because both have publicaccess specifier) ?

作为未成年人的问题,是有什么区别enum classenum struct其他比语法(因为两者都具有public访问说明符)?

回答by Kos

Besides what was already mentioned, an advantage of enum classis better type safety- the enum classenumerators don't convert implicitly to integers.

除了已经提到的内容之外,一个优点enum class是更好的类型安全——enum class枚举器不会隐式转换为整数。

回答by Nicol Bolas

Do we really need “enum class” in C++0x?

我们真的需要 C++0x 中的“枚举类”吗?

No, we don't "need" enum class. We can get sufficiently equivalent functionality in other ways. But by that logic, we don't "need" a lot of stuff in C++. We don't "need" virtual functions and inheritance, since we can just implement it manually with vtables and such. We don't "need" member functions; these can be emulated by having them take an additional argument.

不,我们“不需要” enum class。我们可以通过其他方式获得足够等效的功能。但是按照这个逻辑,我们不需要 C++ 中的很多东西。我们不需要“需要”虚函数和继承,因为我们可以使用 vtables 等手动实现它。我们不需要“需要”成员函数;这些可以通过让他们采取额外的论点来模拟。

Language features exist to make programmers lives easier. Just because something canbe done manually doesn't mean that it should.

语言功能的存在是为了让程序员的生活更轻松。仅仅因为某些事情可以手动完成并不意味着它应该这样做。

enum classhas the following properties:

enum class具有以下属性:

  1. It is easy to understand; it mirrors how enums work in other languages.
  2. It requires relatively little from compiler writers. Contrast the implementation effort with features like r-value references, varadic templates, or user-defined literals.
  3. It doesn't break the syntax in any way. It may look a bit weird at first to see enum class, but that's true for most new features. Once you get used to it, it's fine.
  4. It is 100% backwards compatible, in that it doesn't redefine how regular enums work. Old-style enums work the same as they used to.
  5. It keeps you from having to write a lot of boilerplate code. Boost has a macro to create the effect of enum classdefinitions. Without this macro, you have to spend quite a bit of effort getting all of the corner cases to work. And even so, someonehad to write and debug that macro.
  1. 很容易理解;它反映了枚举在其他语言中的工作方式。
  2. 它对编译器编写者的要求相对较少。将实现工作与 r 值引用、可变参数模板或用户定义的文字等功能进行对比。
  3. 它不会以任何方式破坏语法。乍一看可能有点奇怪enum class,但对于大多数新功能来说都是如此。习惯了就没事了。
  4. 它 100% 向后兼容,因为它不会重新定义常规枚举的工作方式。旧式枚举的工作方式与以前相同。
  5. 它使您不必编写大量样板代码。Boost 有一个宏来创建enum class定义的效果。如果没有这个宏,您必须花费相当多的精力才能使所有极端情况都能正常工作。即便如此,也必须有人编写和调试该宏。

So no, we do not "need" them. But they're still a great addition to the language.

所以不,我们并不“需要”它们。但它们仍然是语言的一个很好的补充。

回答by James McNellis

In the first case, the type of HELLOis not E, whereas in the second case, the type of HELLOis E.

在第一种情况下,类型HELLO不是E,而在第二种情况下,类型HELLOE

For a good demonstration of why this is important, see Howard Hinnant's answer to "“enum class” emulation or solid alternative for MSVC 10.0."

有关为什么这很重要的一个很好的演示,请参阅 Howard Hinnant 对“MSVC 10.0 的“枚举类”仿真或可靠替代方案的回答

enum classand enum structare "semantically equivalent" (i.e., the same), per C++0x FDIS §7.2/2.

enum classenum struct根据 C++0x FDIS §7.2/2,并且是“语义等价的”(即相同)。

回答by David

I think you need to read in the other advantages of these new enums

我认为您需要阅读这些新枚举的其他优点

  • user defined size
  • scoped values (no more general scope cramming of values)
  • no implicit conversion to integral types
  • forward declaration of enums (biggest improvement for enums in APIs)
  • 用户定义尺寸
  • 范围值(没有更一般的范围填充值)
  • 没有隐式转换为整型
  • 枚举的前向声明(API 中枚举的最大改进)

http://www.stroustrup.com/C++11FAQ.html#enum

http://www.stroustrup.com/C++11FAQ.html#enum

回答by The Mask

Yes, we do. Looks like no one pointed out this before. What about if you need to set size of an enumand keep still according to C++ standard? enum classcan do. And with type safetyas already mentioned. It's reduce so much possible bugs in code and the mess to mixing intand enums. They wasn't never the same thing for me. Amazing. e.g., enum class foo : int16_t { ... }I'm sure each member is an int16_tand not up to implementation decide what's "better" for me.

是的,我们有。之前好像没有人指出这一点。如果您需要enum根据 C++ 标准设置 an 的大小并保持静止怎么办?enum class可以做。并且如前所述具有类型安全性。它减少了代码中许多可能的错误以及混合intenums的混乱。对我来说,它们从来都不是一回事。惊人的。例如,enum class foo : int16_t { ... }我确信每个成员都是一个,int16_t而不是由实施决定什么对我来说“更好”。

EDIT:

编辑:

Also, we can have duplicated values (not names) in the list. Which make a lot of sense depending to context.

此外,我们可以在列表中有重复的值(不是名称)。根据上下文,这很有意义。