Enumerated types... what's the point?

Karl Heuer karl at haddock.ima.isc.com
Tue Mar 27 06:22:30 AEST 1990


In article <699 at sixhub.UUCP> davidsen at sixhub.UUCP (bill davidsen) writes:
>I was told that enums as I wanted them were "thinking like Pascal,"
>and "not in the spirit of C."

That's what they said about pointer-vs-integer collision detection, too.  An
idea is *not* automatically bad just because Pascal implemented it first!

ANSI decided to endorse the weak-enum model, but they did give a nod to the
strong-enum model via an acknowledement in the Common Warnings appendix.  I
presume there are a fair number of us who use enums only in the strong model,
so there ought to be a market for a compiler (or a lint option) that enforces
the stricter rules.  Unfortunately, even "gcc -Wall" doesn't seem to do so
yet, but I bet it could be added without too much difficulty.

In my opinion, the rules for strong-enum arithmetic should be analogous to
pointers: enum + int --> enum; enum - enum --> int; enum + enum --> error.
"++" and "--" are analogous to Pascal's "succ" and "pred" functions.  The one
thing you still can't do--and which was, I think, a major reason why the
Committee went for the weak-enum model--is to have an array subscripted by
enum, but this could be faked with a macro.

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list