Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an enum class and why should I care?

For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me:

What is an "enum class" and why do we need it?

like image 849
sbi Avatar asked Dec 26 '12 14:12

sbi


People also ask

Why do we need enum class?

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

What is an enum class?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

Why is enum important?

An enum is a data type that contains fixed set of constants. An enum is just like a class , with a fixed set of instances known at compile time. Advantages of enum: enum improves type safety at compile-time checking to avoid errors at run-time.

What is an enum and what is it used for?

An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.


1 Answers

enum class is called a scoped enumeration. It prevents polluting the namespace where the enumeration appears with the names of the enumerators.

In C++03, you could do effectively the same thing by putting the enum inside a dedicated class. Perhaps that's the source of the syntax, which is a bit confusing.

Another difference is that the enumerators of such a type don't convert implicitly to int (static_cast<int> is required). This may be seldom needed but it makes it safe to overload a function taking an int argument with one taking enum type. You can be sure the int won't be called by accident. Or you can define pseudo-integral types with dedicated operator functions, and be sure that built-in operators won't interfere.

It's a bit annoying that these two unrelated differences come in the same package, and that you can't get an unscoped enumeration with no implicit conversion, but generally both changes are Good Things and enum class is a good default practice in C++11.

EDIT: A scoped enumeration is defined like this:

enum class duck { huey, dewey, louie };

and must be used with the scope resolution operator :: like this:

duck culprit = duck::huey; // or "auto culprit" to avoid redundancy

Note that the :: operator also works with C++03 unscoped enumerations, so the second line above would work even if the first was missing class.

This might be excessive detail, but class does not go into the elaborated-type-specifier if forward declaring the enumerated type, as in

void quack( enum duck whom ); // not "enum class"

However, there is a construct new in C++11, the opaque-enum-declaration, which does include the class keyword and defines a complete type.

enum duck; // duck is declared as incomplete type
enum class duck; // duck is now complete type; underlying type defaults to int

The keyword struct can be substituted for class with no semantic difference.

like image 185
Potatoswatter Avatar answered Sep 17 '22 22:09

Potatoswatter