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

C++EnumsC++11Language Lawyer

C++ Problem Overview


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 class is introduced just to save 2 curly braces {};! Am I missing any important aspect ?

As a minor question, is there any difference between enum class and enum struct other than syntax (because both have public access specifier) ?

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

> Do we really need “enum class” in 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.

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

enum class has the following properties:

  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 class definitions. Without this macro, you have to spend quite a bit of effort getting all of the corner cases to work. And even so, someone had to write and debug that macro.

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

Solution 3 - C++

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

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."

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

Solution 4 - C++

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)

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

Solution 5 - C++

Yes, we do. Looks like no one pointed out this before. What about if you need to set size of an enum and keep still according to C++ standard? enum class can do. And with type safety as already mentioned. It's reduce so much possible bugs in code and the mess to mixing int and 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_t and not up to implementation decide what's "better" for me.

EDIT:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniammilindView Question on Stackoverflow
Solution 1 - C++KosView Answer on Stackoverflow
Solution 2 - C++Nicol BolasView Answer on Stackoverflow
Solution 3 - C++James McNellisView Answer on Stackoverflow
Solution 4 - C++DavidView Answer on Stackoverflow
Solution 5 - C++The MaskView Answer on Stackoverflow