Enums

Until now, every value had a type that C++ gives you, like int, string or bool. In this section, you start making your own types. The simplest kind is an enum (short for "enumeration", a list of named values).

Think of a traffic light. It can only be red, yellow or green. You could store the color as an int, with 0 for red, 1 for yellow and 2 for green. But then a line like light = 2; tells the reader nothing, and nothing stops the code from setting the light to 7. An enum gives each allowed value a name:

#include <iostream>
using namespace std;

enum class Light { Red, Yellow, Green };

int main() {
    Light now = Light::Red;
    if (now == Light::Red) {
        cout << "Stop" << "\n";
    }

    now = Light::Green;
    if (now == Light::Green) {
        cout << "Go" << "\n";
    }
    return 0;
}

Output:

Stop
Go

enum class Light { Red, Yellow, Green }; creates a new type called Light. A Light variable can hold only one of the three values listed in the braces. You write each value with the type name in front, like Light::Red. You can compare two values with == and !=.

Write the enum above main, so every function in the program can use the new type. Note the semicolon after the closing brace.

Why enum class

C++ also has an older form without the word class: enum Light { Red, Yellow, Green };. Its values are written without Light::, so their names belong to the whole program. Two plain enums cannot then use the same name:

#include <iostream>
using namespace std;

enum Light { Red, Yellow, Green };
enum Team { Red, Blue };

int main() {
    return 0;
}

Output:

main.cpp:5:13: error: ‘Red’ conflicts with a previous declaration

Line 5 tries to create a second Red, but the name is already taken by line 4. With enum class, Light::Red and Team::Red are two different names, so this problem cannot happen. Use enum class in new code.

An enum in a switch

A switch fits an enum well: one case for each value.

#include <iostream>
using namespace std;

enum class Light { Red, Yellow, Green };

void showAction(Light light) {
    switch (light) {
        case Light::Red:
            cout << "Stop" << "\n";
            break;
        case Light::Yellow:
            cout << "Wait" << "\n";
            break;
        case Light::Green:
            cout << "Go" << "\n";
            break;
    }
}

int main() {
    showAction(Light::Yellow);
    showAction(Light::Green);
    return 0;
}

Output:

Wait
Go

Printing an enum

cout does not know how to print a value of an enum class. This program does not compile:

#include <iostream>
using namespace std;

enum class Light { Red, Yellow, Green };

int main() {
    Light now = Light::Green;
    cout << now << "\n";
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:8:10: error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘Light’)

The real message goes on for many more lines, but the first error line is the one to read. std::ostream is the type of cout, so the message means that cout has no << for a Light.

To show a word, use a switch like showAction above. To see the number behind a value, convert it with static_cast<int>, which you know from the Type Conversion lesson:

#include <iostream>
using namespace std;

enum class Light { Red, Yellow, Green };

int main() {
    Light now = Light::Green;
    cout << static_cast<int>(now) << "\n";
    cout << static_cast<int>(Light::Red) << "\n";
    return 0;
}

Output:

2
0

C++ numbers the values from 0, in the order you list them. Red is 0, Yellow is 1 and Green is 2.

In the exercise, finish a traffic light that goes from Red to Green to Yellow and back to Red, and prints the name of each light.