It’s surprising that in C++, an enum class is default-initialized to 0, even if no enumerator is explicitly assigned the value 0. In the code below, this behavior causes the default-constructed enum class A to have an invalid value, leading the program to return -1.

enum class A { a = 100, b = 200};

auto main()->int
{
    auto a = A{};
    switch (a)
    {
     case A::a: return 100;
     case A::b: return 200;
    }
    return -1;
}

https://godbolt.org/z/5Te14fbnc

Next - Previous