Basically, given
enum class Color : int { R, G, B };
Color c;
is there any way for c
to end up holding something other than R
, G
, B
? Or, in other words, an underlying int
other than 0
, 1
, or 2
?
I know that the declaration of c
above leaves it uninitialize, and that
Color c{};
would initialize it to R
/0
.
However, I've discovered that if the definition of the enumeration were
enum calss Color : int { R = 1, G, B };
then the {}
-initialized c
would have value 0
, which does not correspond to any of R
/G
/B
(I've verified that c == Color::enumerator
returns false
for any enumerator
chosen from R
, G
, B
; and static_cast<int>(c)
is 0).
Now this, looks a bit edgy to me: if 0
does not underlay any of the enumerators, then why does {}
-initialization give that value to the underlaying int
?