W
Wayne Shu
Today when I read TC++PL(se) section 4.8 Enumerations, I get some
problems.
1. In an enumeration, why the value of some enumerators can be the
same, for instance:
enum e1{a = 0, b = 0, c = 0};
we can't distinguish a, b and c by value, when they are used in
switch statement, the compiler will complain that they have the same
value. why not the standard forbid it, does it have some special
situation that have the same value is useful?
2. In Stroustrup's TC++PL(se) section 4.8, he has written that "The
notion of a range of values for an enumeration differs from the
enumeration notion in the Pascal family of languages. However, bit-
manipulation examples that require values outside the set of
enumerators to be well-defined have a long history in C and C++."
Can you give me a bit-manipulation example he mentioned here?
3. Also in TC++PL(se) section 4.8, he has written that "The sizeof an
enumeration is the sizeof some integral type that can hold its range
and not larger than sizeof (int), unless an enumerator cannot be
represented as an int or as an unsigned int".
If there is an enmerator which can't be represented as an int or as
an unsinged int , the sizeof the enumeration will be sizeof(long)?
Thanks for explaining it.
Regards.
problems.
1. In an enumeration, why the value of some enumerators can be the
same, for instance:
enum e1{a = 0, b = 0, c = 0};
we can't distinguish a, b and c by value, when they are used in
switch statement, the compiler will complain that they have the same
value. why not the standard forbid it, does it have some special
situation that have the same value is useful?
2. In Stroustrup's TC++PL(se) section 4.8, he has written that "The
notion of a range of values for an enumeration differs from the
enumeration notion in the Pascal family of languages. However, bit-
manipulation examples that require values outside the set of
enumerators to be well-defined have a long history in C and C++."
Can you give me a bit-manipulation example he mentioned here?
3. Also in TC++PL(se) section 4.8, he has written that "The sizeof an
enumeration is the sizeof some integral type that can hold its range
and not larger than sizeof (int), unless an enumerator cannot be
represented as an int or as an unsigned int".
If there is an enmerator which can't be represented as an int or as
an unsinged int , the sizeof the enumeration will be sizeof(long)?
Thanks for explaining it.
Regards.