Using scoped enumerations
Enumeration is a basic type in C++ that defines a collection of values, always of an integral underlying type. Their named values, which are constant, are called enumerators. Enumerations declared with the keyword enum are called unscoped enumerations, while enumerations declared with enum class or enum struct are called scoped enumerations. The latter ones were introduced in C++11 and are intended to solve several problems with unscoped enumerations, which are explained in this recipe.
How to do it...
When working with enumerations, you should:
- Prefer to use scoped enumerations instead of unscoped ones
- Declare scoped enumerations using
enum classorenum struct:enum class Status { Unknown, Created, Connected }; Status s = Status::Created;The
enum classandenum structdeclarations are equivalent, and throughout this recipe and the rest of this book, we will useenum class.
Because...