Given the following C++11 code
enum class Foods {
Apple,
Pear,
Banana
};
will
std::cout << (unsigned int)Foods::Apple << std::endl;
output 0?
This is true for MSVC 2012 and gcc 4.7.0 however I’m more interested in what the C++11 standard specifies we may rely on. That is, can portable code depend on enumerations defaulting to 0 for the value of the first item?
Yes, they are. It’s in §7.2/2:
This section covers both
enum classdeclarations and regularenumdeclarations