Some problems about enumerations.

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.
 
K

Kai-Uwe Bux

Wayne said:
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?

You could not prevent this anyway:

enum e1 { 0 };
e1 const a = 0;
e1 const b = 0;
e1 const c = 0;

If a programmer has a use for multiple names refering to the same constant
value, so be it. Why should the language interfere?

As for a special use case, some people make it a habit to always include
min_value, and max_value markers in their enums:

enum my_enum { min_value = 0, a = 0, b = 1, c = 2, max_value = 2 };

This way, templates can retrieve the boundaries of the enumerated values.
However, the names min_value and max_value are probably unrelated to the
problem domain, whence you need alternative proper names for those values,
too.

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?

enum flags { read = 1, write = 2 };

file = open_file ( read | write );

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)?

Provided, long can hold it, yes.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,041
Members
48,769
Latest member
Clifft

Latest Threads

Top