First, I want to say, according to cppreference.com, it is somewhat impossible to value-initialize an enum.
According to http://en.cppreference.com/w/cpp/language/value_initialization, value-initializing an enum actually performs zero-initialization. It then follows that, according to http://en.cppreference.com/w/cpp/language/zero_initialization, the effect of zero-initializing an enum is:
If
Tis a scalar type, the object's initial value is the integral constant zero implicitly converted toT.
However, an integral constant zero is not implicitly convertible to an enum. Ultimately, an enum cannot be value-initialized. This sounds weird, and value-initializing an enum does work on VC, GCC, and clang. So, what does the standard say about this?
Second, according to http://en.cppreference.com/w/cpp/language/static_cast:
Integer, floating-point, or enumeration type can be converted to any complete enumeration type (the result is unspecified (until C++17) undefined behavior (since C++17) if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
So, does this imply that value-initializing an enum (if it works at all) may actually lead to undefined behavior if the target enum does not have an enumerator equal to 0?
1: This can be undestood like so:
This is basic protection from undefined behavior!
2: Yes and Yes :)
static_cast is dangerous by definition, it shoud only be used to support serrialization or old c interfaces!