Scoped Enumerations

In this lesson, we'll discuss how enumerations are used to define custom data-type containing integer constants.

Enumerations allow us to define a custom data-types containing integer constants. These integer constants are called enumerators. Sadly, classical enums have a few drawbacks.

Drawbacks of enumerations in classical C++ #

Three of the biggest drawbacks of classical enumerations are listed below:

  1. Enumerators implicitly convert to int.

  2. Enumerators in the enclosing scope.

  3. The type of the enumeration cannot be specified.

First, let’s focus on 3. Enumerations cannot be forward declared because their type is unknown. There is only one guarantee for enumerations in classical C++: the type must be integral and big enough to hold the enumerator values.

Points 1 and 2 are more surprising. Have a look at the code below:

Get hands-on with 1200+ tech skills courses.