C++11: enum classes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
enum class Colour
{
    Red,
    Green,
    Blue
};
//
int main()
{

    // lvalue of type Colour
    auto var = Colour::Red;
    //
    //  cannot assign it to an int -- compilation failure
    int val = var;
    //
    //  type-inferencing works correctly
    decltype(var) greenColor = Colour::Green;
    //
    //  casting will work
    int val2 = static_cast(var);
    //
    //  what is Red? undeclared identifier. nice
    //  compilation failure
    auto val3 = Red;
    //
    //  can't compare enum-class and int
    //  we haven't defined operator==(Colour,int)
    auto areEqual = Colour::Red == 0;
    //
}