Overload resolution involving old-style enums and integral types
[conv.integral]/1:
A prvalue of an unscoped enumeration type can be converted to a prvalue of an integer type.
The converse is not true. There's no implicit conversion from an integer type to an unscoped enum type:
It seems that you are confusing this with casting an integral value to an enum type: [expr.static.cast]/10
A value of integral or enumeration type can be explicitly converted to a complete enumeration type. The value is unchanged if the original value is within the range of the enumeration values ([dcl.enum]). Otherwise, the behavior is undefined. A value of floating-point type can also be explicitly converted to an enumeration type. The resulting value is the same as converting the original value to the underlying type of the enumeration ([conv.fpint]), and subsequently to the enumeration type.
(emphasis mine)
But this can only be done via an explicit cast:
E x1 = static_cast<E>(1) // yes
E x2 = E(1); // yes
E x3 = 1; // no
E x4(1); // no
I suggest you read this link.
As mentioned,
An enumeration such as
enum Color { red, white, blue };
is its own type. It is not of typeint
.
myEnum
is neither an int
nor an unsigned int
.
Plus, I suggest not to use myEnum
by directly casting an int
to a myEnum
(doing this: test(static_cast<myEnum>(0))
).
Indeed the compiler won't check for you if the value provided is a valid one, it can lead to unexpected behavior.