enums

Reasons to Use an enum class in C++

1. Strong Typing (Type Safety)

Traditional enums are implicitly converted to integers, which can lead to unintended type mismatches or errors. An enum class does not implicitly convert to int, so it prevents accidental mixing of different enums or enums with integers.

enum Color { RED, GREEN, BLUE };
enum Size { SMALL, MEDIUM, LARGE };

Color c = RED;
Size s = SMALL;

// This compiles, but it is confusing and error-prone
c = s;  // This would be a problem with an enum class
  

With enum class, trying to assign a Size to a Color would result in a compile-time error:

enum class Color { RED, GREEN, BLUE };
enum class Size { SMALL, MEDIUM, LARGE };

Color c = Color::RED;
Size s = Size::SMALL;

// Error: cannot assign Size to Color
c = s;  // Compile-time error
  

2. Scoped Enum Values

With traditional enums, the enum values are placed in the global scope, potentially leading to naming conflicts. With enum class, the enum values are scoped to the enum itself, preventing such conflicts.

enum Color { RED, GREEN, BLUE };
enum Size { SMALL, MEDIUM, LARGE };

// RED and SMALL can conflict in a large program
  

With enum class, the enum values are accessed with their enum name, like Color::RED and Size::SMALL, making the code clearer and avoiding conflicts.

3. Better Control Over the Underlying Type

With a traditional enum, the underlying type is usually int. However, with enum class, you can specify a different underlying type, which gives you more control over memory usage and compatibility. You can explicitly set the underlying type of the enum class.

enum class Color : uint8_t { RED, GREEN, BLUE }; // Use uint8_t instead of int
  

4. Improved Readability

By using enum class, the values are associated with the enum type, which can improve code readability. You get better context about what the values represent.

enum class Color { RED, GREEN, BLUE };
Color color = Color::GREEN;

// The code is clear: we're working with Color values
  

Example with enum class:

#include <iostream>

enum class Color { RED, GREEN, BLUE };
enum class Size { SMALL, MEDIUM, LARGE };

int main() {
    Color c = Color::RED;
    Size s = Size::SMALL;

    // This will not compile, ensuring type safety
    // c = s;  // Error: cannot assign Size to Color

    // The enum values are scoped, so no risk of name collision
    std::cout << "Color: " << static_cast<int>(c) << ", Size: " << static_cast<int>(s) << std::endl;
    return 0;
}
  

In Summary:

For most modern C++ code, using enum class is recommended due to these advantages.


edit this page