Enumeration Type in C++

Jinku Hu Oct 12, 2023
  1. Unscoped Enumerations in C++
  2. Scoped Enumerations in C++
Enumeration Type in C++

This article will demonstrate how to use the enumeration type in C++.

Unscoped Enumerations in C++

Enumerations have been a part of the C language for a while. The C++ also implemented the same concept with a few additional features. Generally, enumeration is the mechanism to name constant integers. The programmer defines enumerator names. Integral values can be specified explicitly or inferred by the position of the given name.

Enumerations are determined using the keyword enum, and they can optionally define a name for the type (e.g. Color in the following code snippet). Note that the C++ enumeration constants have implementation-defined integral types. So if the given values need larger space than the int, the corresponding integral type is chosen as the underlying type. The type can also be explicitly specified by the user after the enumeration name and needs to be separated with a single colon (shown in the fourth code snippet in this article).

In the following example, we define an enumeration named Color and specify the six color names as the enumerated constants. Since none of these constants have the explicit value assigned to them, the integral values are deduced with their positions starting from zero. This code implements a color to string conversion function, which is limited to the given values. The variable Color is also an unscoped enumeration; this means that its enumerators (constants) are accessible from the same scope as the enumeration itself.

#include <iostream>

using std::cout;
using std::endl;
using std::string;

enum Color { RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW };

string ColorToString(Color c) {
  switch (c) {
    case RED:
      return "Red";
    case BLUE:
      return "Blue";
    case GREEN:
      return "Green";
    case CYAN:
      return "Cyan";
    case MAGENTA:
      return "Magenta";
    case YELLOW:
      return "Yellow";
    default:
      return "NAN";
  }
}

int main() {
  Color col1 = RED;

  cout << "the given color is " << ColorToString(col1) << endl;

  return EXIT_SUCCESS;
}

Output:

the given color is Red

Alternatively, we can define an enumeration (e.g. WeekDay) where each enumerator has the explicitly assigned integral value. You can utilize this feature to implement a flexible structure that can be modified according to different date conventions. Explicit and positional values can be mixed in the same enumeration object if needed.

#include <iostream>

using std::cout;
using std::endl;

enum WeekDay { MON = 1, TUE = 2, WED = 3, THU = 4, FRI = 5, SAT = 6, SUN = 7 };

int main() {
  auto today = SAT;
  cout << "today is " << today << "th day" << endl;

  return EXIT_SUCCESS;
}

Output:

today is 6th day

Scoped Enumerations in C++

Another helpful feature of enumerations is to have a scope; thus, it’s called scoped enumerations. The latter one can be declared using enum class or enum struct keywords. Scoped enumerations follow the usual conventions of scoping and are not accessible outside the enumeration scope. On the plus side, globally-scoped enumerations won’t result in name collisions as the unscoped enumerations might do.

The next code sample defines WeekDay from the previous example as the scoped enumeration. Notice that the unscoped enumerations can be printed using the cout function as any other variable, but the scoped one needs to be cast to the corresponding integral value.

#include <iostream>

using std::cout;
using std::endl;

enum class WeekDay {
  MON = -1,
  TUE = 2,
  WED = 3,
  THU = 4,
  FRI = 5,
  SAT = 6,
  SUN = 7
};

int main() {
  auto today = WeekDay::MON;
  cout << "today is " << static_cast<unsigned>(today) << "th day" << endl;

  return EXIT_SUCCESS;
}

Output:

today is 6th day

Scoped enumerations must have a name, in contrast to unscoped ones. On the other hand, scoped enumeration does not have a default underlying integral type. Be aware that the underlying type conversion may result in overflows when the enumerators are cast to different types.

#include <iostream>

using std::cout;
using std::endl;

enum class WeekDay : int {
  MON = -1,
  TUE = 2,
  WED = 3,
  THU = 4,
  FRI = 5,
  SAT = 6,
  SUN = 7
};

int main() {
  auto today = WeekDay::MON;
  cout << "today is " << static_cast<unsigned>(today) << "th day" << endl;

  return EXIT_SUCCESS;
}

Output:

today is 4294967295th day
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Enum