Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we really need "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 

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) ?

like image 768
iammilind Avatar asked Aug 04 '11 03:08

iammilind


People also ask

Is enum useful in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Why do we need enum class?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

Does enum class exist in C?

C treats enums as a type. Try doing a web search for "C enum type", and you'll find some proper links. Here is an explanation of the difference between enum type and class.

What does an enum class do?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.


2 Answers

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

like image 188
Kos Avatar answered Oct 12 '22 19:10

Kos


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.

like image 26
Nicol Bolas Avatar answered Oct 12 '22 17:10

Nicol Bolas