Strongly typed Enums in C++ 11

C-Plus-Plus

In this article, we are going to learn about Strongly typed Enums in C++ 11.The strongly typed enum feature of C++ 11. There are continuous improvements in C++ 17 and C++20 to this topic.

What is Enum in C++


Enumerations or Enums in C++ is a convenient way to define integer constants(enumerators) with names. Before C++ 11 there were four issues with enums in C++. Let us understand the issues and then jump to the C++11 improvements to overcome these issues.

Enum issues were befor C++ 11

  • The enumerators implicitly convert to an int value.
  • The data type of the values in enumeration can not be specified.
  • The forward declaration can not be done.
  • enumerators were unscoped, you can not have two enumerations with the same name.
// this code won't compile!
enum fruits {apple, banana, orange}; 
enum Juice {grapes, pineapple, orange};

Strongly typed Enums in C++ 11


With the introduction of C++ 11, enums got some new features and now enums became Strongly typed enumerations or scoped and unscoped enumerations. The strongly-typed enumerations in C++ 11 have some new features associated with them. Below are some of the new features:

Let us understand the new features now:

Syntax changes:

The strongly-typed enumerations will be declared by using class or struct keywords.

enum class fruits{
  apple= 1,
  banana=10,
  grapes=100,
  orange= 1000
};

enum struct fruits{
  apple= 1,
  banana=10,
  grapes=100,
  orange= 1000
};

Features of stronged type Enum in C++ 11


1. The enumerators don’t implicitly convert to int

Before C++11, the enumerators get converted to int implicitly. But now if you want to convert it to int then
you will need to do it explicitly. Let us see this with an example below:

C++ Program Example

static_cast(fruits::apple)
static_cast(fruits::banana)
static_cast(fruits::grapes)
static_cast(fruits::orange)

2. Explicit Data types

The data type of enumerators is by default-int. But you can specify a data type with its declaration. Also, you can use enumerators for forwarding declarations now. let us see this with examples below:

C++ Program Example

enum struct Signal: bool{
  Off,     // 0
  ON     // 1
};

enum class fruits: char{
  apple= 1,
  banana, // 2
  grapes // 3
};

// Forward declaration
enum class fruits;

void MakeJuice (fruits m);
 
// later on:
enum class fruits { apple, banana, grapes };

3. Enhanced Scoping

Strongly typed enums, enum classes, have enhanced scoping. The enumerators can only be accessed in the scope of the enumeration. Let us understand this with an example: Each enum value is scoped within the name of the enum class. So, to access the enum values, you must use the enum class name:

C++ Program Example

fruits Fruit = fruits::apple;
if ( Signal::Off == 0 )
{
    // off
}

Summary

In this post, we have learned what is Strongly typed Enums in C++ 11 with an example, what are limitations of enum before strongly typing enum and what are new features of Strongly typed Enums in C++ 11.