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

When we have

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

then why do we need

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

The second version of IMO does not offer more features than the first. I do not think that enum class is introduced only to save 2 curly braces {}; ! Is there an important aspect missing?

As a minor question, is there a difference between the enum class and the enum struct different from the syntax (since both have a public access specifier)?

+57
c ++ enums language-lawyer c ++ 11
Aug 4 2018-11-11T00:
source share
5 answers

In addition to what has already been mentioned, the advantage of enum class better than security - enum class enumerations are not converted implicitly to integers.

+123
Aug 04 '11 at 7:10
source share

Do we really need an "enum class" in C ++ 0x?

No, we do not need an enum class . We can get quite equivalent functionality in other ways. But by this logic, we do not need a lot of things in C ++. We do not need virtual functions and inheritance, since we can simply implement it manually using vtables, etc. We do not need "member functions"; they can be emulated if they take an extra argument.

There are language features that make life easier for programmers. Just because something can be done manually does not mean that it should.

enum class has the following properties:

  • Easy to understand; it reflects how enumerations work in other languages.
  • For compiler compilers, this is relatively small. Compare implementation efforts with features such as r-value references, varadic patterns, or user-defined literals.
  • It does not violate the syntax. It may seem a little strange at first to see the enum class , but this is true for most of the new features. Once you get used to it, all is well.
  • This is 100% backward compatible as it does not override how regular enumerations work. Old-style modes work the same as before.
  • This prevents you from writing many templates. Boost has a macro to create an enum class effect. Without this macro, you will have to spend a lot of effort for all corner cases to work. And even so, someone had to write and debug this macro.

So no, we don’t β€œneed” them. But they are still a great addition to the language.

+92
Aug 04 '11 at 7:00
source share

In the first case, the HELLO type is not E , while in the second case, the HELLO type is E

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

enum class and enum struct are "semantically equivalent" (i.e., the same), for C ++ 0x FDIS Β§7.2 / 2.

+23
Aug 04 2018-11-11T00:
source share

I think you need to read the other benefits of these new listings.

  • user defined size
  • coordinated values ​​(no more than general scaling of values)
  • implicit conversion to integral types
  • forward declaration of transfers (the biggest improvement for transfers in the API)

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

+16
Aug 04 2018-11-11T00:
source share

Yes we do. Nobody seemed to notice this. How about if you need to set the enum size and save according to the C ++ standard? enum class can do. And with security type , as already mentioned. This reduces the number of possible errors in the code and the clutter before mixing int and enum s. They were never the same for me. Amazing for example, enum class foo : int16_t { ... } I am sure that each member of int16_t , and not before implementation, decides what is "better" for me.

EDIT:

In addition, we can have duplicate values ​​(rather than names) in the list. This is of great importance depending on the context.

+2
Mar 19 '14 at 17:04
source share



All Articles