Re: enum safety
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Mon, 11 Feb 2008 07:07:05 +1300
Keith Thompson wrote:
Ian Collins <ian-news@xxxxxxxxxxx> writes:Or in C++, where they are first class types. A C enum is little
Army1987 wrote:
Ian Collins wrote:That illustrates the problem perfectly, you declare declare a valid
Sard wrote:Not necessarily. For example, you can have an object which can contain
Page 39 K&R2 saysThis is why (in my opinion) enums are horribly broken in C.
'Although variables of enum types may be declared, compilers need not
check that what you store in such a variable is a valid value for the
enumeration'
a number up to 10 as numbers, and 11, 12 and 13 with special meanings. E.g.
enum rank { ACE = 1, JACK = 11, QUEEN = 12, KING = 13 };
enum rank foo = 7;
range of values for rank and then legally assign some other arbitrary
value to it. Useless.
It seems perfectly useful to me. An object of type enum range can
sensibly hold any value from 1 to 13. The values 1, 11, 12, and 13
happen to have names associated with them; the others are merely
numbers. The language guarantees that this will work (in fact, it
guarantees that values in the range 0..127 are valid).
A C enumerated type doesn't act like an enumerated type in, say,
Pascal or Ada, but it's not supposed to. It merely provides a set of
names for specific values, and an integral type that can hold all
those values.
different from a typedef and a set of #defines. It's the lack of type
safety I don't like. Maybe it as was a bad experience with come code
that tried to use enems as types, but ended up abusing them (assigning
wrong values) that made me feel this way.
--
Ian Collins.
.
- Follow-Ups:
- Re: enum safety
- From: Keith Thompson
- Re: enum safety
- References:
- enum safety
- From: Sard
- Re: enum safety
- From: Ian Collins
- Re: enum safety
- From: Army1987
- Re: enum safety
- From: Ian Collins
- Re: enum safety
- From: Keith Thompson
- enum safety
- Prev by Date: Re: Who likes short shorts?
- Next by Date: Re: other than pragma
- Previous by thread: Re: enum safety
- Next by thread: Re: enum safety
- Index(es):
Relevant Pages
|