You may be familiar with enum values, but do you know about enum classes?
This great feature was introduced with C++11 to solve several problems with the regular enum declaration.
Now I will explain the enum class declaration and demonstrate its practical uses. Although the examples I provide are intended for the Arduino Uno, the concept will work with any platform and with all modern C++ compilers.
A Display Interface
Let’s assume we create modular firmware code and create an interface for the display of our device.
#pragma once
namespace Display {
enum Backlight {
Off,
On
};
enum StatusLed {
Off,
Red,
Yellow,
Blue,
};
void setBacklight(Backlight backlight);
void setStatusLed(StatusLed statusLed);
}
If you compile this example, it will cause a failure and display this error message:
Display.hpp:11:3: error: redeclaration of 'Off'
Off,
The regular enum declaration will put all of its enumerators into the same namespace as the declaration itself. The list of declared identifiers will look similar to this:
Display::Backlight [enum-name]
Display::Off [enumerator]
Display::On [enumerator]
Display::StatusLed [enum-name]
Display::Off [enumerator] <== conflict!
Display::Red [enumerator]
Display::Yellow [enumerator]
Display::Blue [enumerator]
Display::setBacklight [function]
Display::setStatusLed [function]
Because Off is already declared by the Backlight enum, its second declaration by StatusLed causes a name conflict.
Doubtful Solutions
Prefixes
A common “solution” used to avoid name conflicts is to put a prefix in front of each name:
enum Backlight {
BacklightOff,
BacklightOn
};
enum StatusLed {
StatusLedOff,
StatusLedRed,
StatusLedYellow,
StatusLedBlue,
};
Occasionally, you read code that uses an underscore to separate the prefix from the name.
#pragma once
namespace Display {
enum Backlight {
Backlight_Off,
Backlight_On
};
enum StatusLed {
StatusLed_Off,
StatusLed_Red,
StatusLed_Yellow,
StatusLed_Blue,
};
void setBacklight(Backlight backlight);
void setStatusLed(StatusLed statusLed);
}
This underscore is introduced because the author instinctively perceives a loss of readability.
While these solutions work perfectly fine, they introduce cumbersome, repetitive code, and repetitive code always indicates a design problem.
Artificial Namespaces
Another common solution is to put the enum declaration into another namespace:
#pragma once
namespace Display {
namespace Backlight {
enum Backlight {
Off,
On
};
}
namespace StatusLed {
enum StatusLed {
Off,
Red,
Yellow,
Blue,
};
}
void setBacklight(Backlight::Backlight backlight);
void setStatusLed(StatusLed::StatusLed statusLed);
}
The previous example uses a namespace declaration, which creates a very ugly duplication of the name in the interface. A more creative variant is to encapsulate the enum into its own class:
#pragma once
namespace Display {
class Backlight {
public:
enum Enum {
Off,
On
};
Backlight(Enum value) : _value(value) {}
// operators, getters, etc.
private:
Enum _value;
};
class StatusLed {
public:
enum Enum {
Off,
Red,
Yellow,
Blue,
};
StatusLed(Enum value) : _value(value) {}
// operators, getters, etc.
private:
Enum _value;
};
void setBacklight(Backlight backlight);
void setStatusLed(StatusLed statusLed);
}
Both of these solutions allow for clear and simple use of the interface, which is an improvement over the first ones.
#include "Display.hpp"
void setup() {
Display::setBacklight(Display::Backlight::On);
Display::setStatusLed(Display::StatusLed::Red);
}
void loop() {
}
Using an Enum Class
Enum classes work like a regular enum declaration, but put all enumerators into a new namespace:
#pragma once
namespace Display {
enum class Backlight {
Off,
On
};
enum class StatusLed {
Off,
Red,
Yellow,
Blue,
};
void setBacklight(Backlight backlight);
void setStatusLed(StatusLed statusLed);
}
Let’s now look at all identifiers declared in the previous example:
Display::Backlight [enum-name]
Display::Backlight::Off [enumerator]
Display::Backlight::On [enumerator]
Display::StatusLed [enum-name]
Display::StatusLed::Off [enumerator]
Display::StatusLed::Red [enumerator]
Display::StatusLed::Yellow [enumerator]
Display::StatusLed::Blue [enumerator]
Display::setBacklight [function]
Display::setStatusLed [function]
The declaration of the enum is simple and the two Off enumerators are separated in their own namespaces.
Syntax
Declaring an enum class is nearly identical to a regular enum declaration. The only difference is the class keyword after enum:
enum class [enum name] {
[enumerator 1],
[enumerator 2],
[enumerator 3],
...
};
As is true for regular enum declarations, you can set an explicit base type and provide an initialiser for each enumerator:
enum class Speed : uint8_t {
Slow = 0x10u,
Medium = 0x40u,
Fast = 0xa5u,
};
Usage
Enum classes are used in the exact same way as regular enum types. The only difference is the additional namespace you must put in front of the enumerator:
#include "Display.hpp"
void setup() {
Display::setBacklight(Display::Backlight::On);
Display::setStatusLed(Display::StatusLed::Red);
}
void loop() {
Display::setStatusLed(Display::StatusLed::Blue);
delay(200);
Display::setStatusLed(Display::StatusLed::Yellow);
delay(200);
Display::setStatusLed(Display::StatusLed::Red);
delay(200);
}
This can lead to repetitive code with long identifier names, which is less than ideal. You can easily solve this problem using local using declarations:
#include "Display.hpp"
void setup() {
using namespace Display;
setBacklight(Backlight::On);
setStatusLed(StatusLed::Red);
}
void loop() {
using namespace Display;
setStatusLed(StatusLed::Blue);
delay(200);
setStatusLed(StatusLed::Yellow);
delay(200);
setStatusLed(StatusLed::Red);
delay(200);
}
Here, the using namespace declaration is only working with an actual namespace, but not with a class.
Let’s assume we created a class-based Display interface as shown in the following example:
#pragma once
class Display
{
public:
enum class Backlight {
Off,
On
};
enum class StatusLed {
Off,
Red,
Yellow,
Blue,
};
void setBacklight(Backlight backlight);
void setStatusLed(StatusLed statusLed);
};
The using namespace will not work with the Display class. Instead, we can declare new names in our namespace for the enum types:
#include "Display.hpp"
using Backlight = Display::Backlight;
using StatusLed = Display::StatusLed;
Display gDisplay;
void setup() {
gDisplay.setBacklight(Backlight::On);
gDisplay.setStatusLed(StatusLed::Red);
}
void loop() {
gDisplay.setStatusLed(StatusLed::Blue);
delay(200);
gDisplay.setStatusLed(StatusLed::Yellow);
delay(200);
gDisplay.setStatusLed(StatusLed::Red);
delay(200);
}
When Should I Use Enum Classes?
You should use an enum class if the additional namespace will improve the readability of your code.
- The enum is used on its own in an interface, as demonstrated with the in an interface, as demonstrated with the
Display interface. interface. - The enumerator names may be confusing out of context, like , like
Off and and On.. - If you prefix each enumerator name, like , like
Backlight_Off and and Backlight_On..
If you already introduced a namespace, and the enumerator names are unique and make sense, you should use a regular enum declaration.
When deciding, always consider how your interface is used. Your goal must be to make this code clear and readable.
Conclusion
Using enum classes will keep the names of enumerators simple and prevent name conflicts. The namespace in front of each enumerator associates the name with the enum type, increasing the readability of the code.
If you have questions, missed any information, or simply wish to provide feedback, simply add a comment below or ask a question on Twitter!