HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Why is enum class considered safer to use than plain enum?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
enumwhyclassconsideredthanuseplainsafer

Problem

I heard a few people recommending to use enum classes in C++ because of their type safety.

But what does that really mean?

Solution

C++ has two kinds of enum:

  • enum classes



  • Plain enums



Here are a couple of examples on how to declare them:

enum class Color { red, green, blue }; // enum class
 enum Animal { dog, cat, bird, human }; // plain enum


What is the difference between the two?

-
enum classes - enumerator names are local to the enum and their values do not implicitly convert to other types (like another enum or int)

-
Plain enums - where enumerator names are in the same scope as the enum and their
values implicitly convert to integers and other types

Example:

enum Color { red, green, blue };                    // plain enum 
enum Card { red_card, green_card, yellow_card };    // another plain enum 
enum class Animal { dog, deer, cat, bird, human };  // enum class
enum class Mammal { kangaroo, deer, human };        // another enum class

void fun() {

        //-----------------------------------------------
    // examples of bad use of plain enum (not safe)
    Color color = Color::red;
    Card card = Card::green_card;

    int num = color;    // no error (bad)

    if (color == Card::red_card) // no error (not safe, bad)
        cout << "bad" << endl;

    if (card == Color::green)   // no error (not safe, bad)
        cout << "bad" << endl;
        //-----------------------------------------------

        //-----------------------------------------------
    // examples of good use of enum classes (safe)
    Animal a = Animal::deer;
    Mammal m = Mammal::deer;

    int num2 = a;   // error (good)
    if (m == a)         // error (good)
        cout << "bad" << endl;

    if (a == Mammal::deer) // error (good)
        cout << "bad" << endl;
        //-----------------------------------------------

}


Conclusion:

enum classes should be preferred because they cause fewer surprises that could potentially lead to bugs.

Code Snippets

enum class Color { red, green, blue }; // enum class
 enum Animal { dog, cat, bird, human }; // plain enum
enum Color { red, green, blue };                    // plain enum 
enum Card { red_card, green_card, yellow_card };    // another plain enum 
enum class Animal { dog, deer, cat, bird, human };  // enum class
enum class Mammal { kangaroo, deer, human };        // another enum class

void fun() {

        //-----------------------------------------------
    // examples of bad use of plain enum (not safe)
    Color color = Color::red;
    Card card = Card::green_card;

    int num = color;    // no error (bad)

    if (color == Card::red_card) // no error (not safe, bad)
        cout << "bad" << endl;

    if (card == Color::green)   // no error (not safe, bad)
        cout << "bad" << endl;
        //-----------------------------------------------

        //-----------------------------------------------
    // examples of good use of enum classes (safe)
    Animal a = Animal::deer;
    Mammal m = Mammal::deer;

    int num2 = a;   // error (good)
    if (m == a)         // error (good)
        cout << "bad" << endl;

    if (a == Mammal::deer) // error (good)
        cout << "bad" << endl;
        //-----------------------------------------------

}

Context

Stack Overflow Q#18335861, score: 875

Revisions (0)

No revisions yet.