top of page
90s theme grid background
Writer's pictureGunashree RS

Enumeration in Cpp: Understanding Scoped and Based Enums

Introduction

In C++, enumerations (enums) provide a way to define a set of named constants, enhancing code readability, safety, and maintainability. Enums have been a part of C++ since its inception, allowing developers to pack related constants into an efficient user-defined type without the overhead of more complex constructs like classes or structs. However, while enums are powerful, traditional C++ enums have limitations that have irked developers for decades.


With C++11, enums received significant updates in the form of scoped enums and based enums. These enhancements improved scoping rules, allowed custom underlying types, and enforced stronger typing, addressing long-standing issues with traditional enums. In this guide, we'll dive deep into enumeration in CPP, exploring both the classic enum types and the powerful new features introduced in C++11. By the end of this article, you'll have a comprehensive understanding of how to use enums to improve the quality and safety of your code.


C++


What is Enumeration in Cpp?

In C++, an enumeration is a user-defined type that consists of a set of named integral constants known as enumerators. These constants represent a specific value and provide a more readable alternative to using raw integers or other primitive types to represent options, states, or categories.

For example, the following enum defines different colors:

cpp

enum Color {
    Red,
    Green,
    Blue
};

Here, Red, Green, and Blue are enumerators, and by default, they are implicitly assigned integer values starting from 0. Thus, Red is 0, Green is 1, and Blue is 2. You can use these enumerators in your program as symbolic constants:

cpp

Color myColor = Green;

Enums in C++ are versatile but also limited in traditional C++. For instance:

  • Enumerator names are visible in the global scope.

  • You cannot define the underlying data type for enums in C++03.

  • Enums implicitly convert to integers, which can lead to errors if misused.



The Drawbacks of Traditional Enums in C++

Despite their utility, traditional enums in Cpp03 have several drawbacks that have frustrated developers:


1. Scope Issues

In traditional enums, all enumerator names are introduced into the enclosing scope, which means they can easily conflict with other variables or constants. Consider the following example:

cpp

enum Color {
    Red,
    Green,
    Blue
};

enum Fruit {
    Apple,
    Green, // This conflicts with the Color Green!
    Banana
};

In the example above, both Color and Fruit define an enumerator Green, leading to a conflict because they share the same scope.


2. Lack of Type Safety

Traditional enums implicitly convert to int, which can cause unintended consequences when misused:

cpp

enum Color {
    Red,
    Green,
    Blue
};

int c = Green;  // Implicitly converts to int (c = 1)

This implicit conversion can lead to bugs or unexpected behavior when using enums with integers interchangeably.


3. No Control Over Underlying Type

In traditional C++, the underlying type of an enum is implementation-defined, and there's no standard way to explicitly specify the type. This can be problematic when platform-independent code is required, such as when transmitting data across a network where byte sizes matter.


C++11 Enhancements: Scoped and Based Enums

To address these limitations, C++11 introduced two major enhancements to enums:

  • Scoped Enums: Enums with their own scope, avoiding name clashes.

  • Based Enums: Enums with an explicitly defined underlying type.

Let’s explore these enhancements in detail.



1. Scoped Enums: Solving the Scoping Problem

In C++11, scoped enums provide a solution to the scoping problem by confining enumerators to the scope of the enum type itself. Scoped enums are declared using the enum class or enum struct syntax. With scoped enums, enumerators are not visible in the enclosing scope, and you must use the qualified name to refer to them.


Here’s an example of a scoped enum:

cpp

enum class Color {
    Red,
    Green,
    Blue
};

enum class Fruit {
    Apple,
    Orange,
    Banana
};

To access an enumerator from a scoped enum, you must qualify it with the enum type name:

cpp

Color myColor = Color::Green;
Fruit myFruit = Fruit::Apple;

This eliminates the possibility of naming conflicts, as seen in traditional enums.


Benefits of Scoped Enums

  • No Name Clashes: Scoped enums keep enumerators within the enum type, so they do not conflict with other variables or constants in the global scope.

  • Type Safety: Scoped enums are strongly typed, meaning implicit conversions to int or other types are not allowed.


For instance:

cpp

int c = Color::Green;  // Error: no implicit conversion to int
int c = static_cast<int>(Color::Green);  // OK: explicit cast

2. Based Enums: Controlling the Underlying Type

In C++11, you can now specify the underlying type of an enum explicitly using based enums. This allows you to control the size and memory layout of your enums, which can be particularly useful for optimizing storage or ensuring platform-independent behavior.


Here’s how you define an enum with a custom underlying type:

cpp

enum class ErrorCode : uint8_t {
    Success = 0,
    Warning = 1,
    Error = 2
};

In the example above, ErrorCode uses uint8_t (an 8-bit unsigned integer) as its underlying type. This ensures that each enumerator occupies exactly one byte of memory, which is useful when memory efficiency is critical, such as in embedded systems.


Why Use Based Enums?

  • Memory Efficiency: By selecting smaller types like uint8_t or char, you can reduce memory consumption, which is critical in memory-constrained environments like embedded systems.

  • Platform Independence: Based enums ensure consistency across platforms by explicitly specifying the size of the enum type, avoiding the variability introduced by compiler-specific implementations.


Here’s another example:

cpp

enum class FileMode : unsigned int {
    Read = 1,
    Write = 2,
    ReadWrite = 3
};

In this case, FileMode uses unsigned int as the underlying type, allowing for a range of values that fit within an unsigned integer.


Combining Scoped and Based Enums

One of the most powerful aspects of C++11 enums is the ability to combine both scoped and based enums. This allows you to benefit from strong typing, scope control, and memory efficiency in a single enum declaration:

cpp

enum class Status : uint16_t {
    OK = 200,
    NotFound = 404,
    InternalError = 500
};

Here, Status is both scoped and based, using uint16_t (a 16-bit unsigned integer) as the underlying type. The scope ensures no name clashes and the based type ensures memory-efficient storage.


Backward Compatibility with Traditional Enums

One of the great things about C++11’s enum enhancements is that they are fully backward-compatible with traditional enums. If you have legacy code using old-style enums, they will continue to work as expected. You can mix traditional enums with C++11 scoped and based enums without any issues.


For example:

cpp

enum Direction {
    Up,
    Down
};

int dir = Down;  // Still works with traditional enums in C++11

Why You Should Use Scoped and Based Enums

Here are some compelling reasons why you should consider refactoring your code to use C++11 scoped and based enums:

  • Stronger Type Safety: Avoid unintended conversions between enums and integers, reducing bugs.

  • Better Name Management: Scoped enums eliminate name clashes, especially in large projects with multiple modules.

  • Memory Control: Based enums allow you to explicitly control the size of your enums, improving memory efficiency.

  • Future-Proofing: By using modern C++ features, you ensure that your code is robust, maintainable, and scalable.




FAQs on Enumeration in C++


1. What is an enumeration in C++?

The enumeration in C++ is a user-defined data type that consists of a set of named constants, known as enumerators, which are often used to represent states, modes, or options in a program.


2. What is the difference between scoped and unscoped enums?

Scoped enums (introduced in C++11) keep enumerators within their scope, preventing naming conflicts, and are strongly typed. Unscoped enums are the traditional C++ enums, which are visible in the global scope and implicitly convert to integers.


3. What are based enums in C++11?

Based enums allow you to explicitly define the underlying type of an enum, such as char, int, or uint16_t, giving you control over the size and memory layout of the enumerators.


4. Can you combine scoped and based enums?

Yes, you can combine both features in C++11 by specifying both the scope and the underlying type of an enum, providing strong typing and memory control.


5. Are traditional enums still valid in C++11 and later?

Yes, traditional enums are fully backward-compatible and work the same way in C++11 and later versions.



Conclusion

Enumeration in C++ has evolved significantly with C++11, offering developers greater control over scope, type safety, and memory usage. By using scoped enums to avoid name clashes and ensure type safety, and based enums to control the underlying type, you can write more robust, maintainable, and efficient code.


Traditional enums still serve their purpose and remain fully supported, but C++11’s scoped and based enums are worth incorporating into your code for better performance and reduced errors. Whether you are working on a small project or a large, complex system, refactoring your code to use modern enums can dramatically improve code quality and developer productivity.



Key Takeaways

  1. Scoped enums prevent name clashes by confining enumerators to their scope.

  2. Based enums allow you to control the underlying type for memory efficiency and platform independence.

  3. Strong typing in scoped enums prevents unintended conversions to integers.

  4. Backward compatibility ensures traditional enums can still be used in C++11 and later.

  5. Combining scoped and based enums provides both name management and memory control, improving code quality.



Article Sources


Comments


bottom of page