Number of enums

Dave P. Schaumann dave at cs.arizona.edu
Fri Jan 25 01:32:22 AEST 1991


In article <279DC67C.6ABD at tct.uucp> chip at tct.uucp (Chip Salzenberg) writes:
>According to dave at cs.arizona.edu (Dave P. Schaumann):
>>[suggests the new operator 'numberof' would be A Good Thing]
>
>Hardly.  Given:
>
>    typedef enum {
>	CORGE = -100, QUUX = -50,
>	LARRY = 1, MOE = 2, CURLY = 4, JOE = 9,
>	EX_PARROT = 32767
>    } PLUGH;
>
>Of what use would numberof(PLUGH) be?

Indeed.  What use would it be?  I intended that the numberof operator would be
useful for non-initialized enums only.  If the programmer used the numberof
operator on PLUGH, the result shouldn't necessarily be useful.

Let me give a more precise definition of what I think 'numberof' should do,
to put to rest some questions I have been getting.

DEFINE numberof(enum typename) to return the value of the last name in typename
       incremented by 1.  This would be a compile-time operator like 'sizeof'.

Thus, the operaton is well-defined over all possible enum definitions, and
gives a useful value for default-valued enums.

Frankly, I don't really care what happens in the case of an initialized-enum
type.  The only thing that I would really want is for it to be well defined,
and easy to implement.

Probably the most serious objection I recieved to this operator was "does C
really need another operator?"  The only defense I can offer for having
numberof() instead of using a special final enum name is that the enum is
syntactically and (IMO) conceptually cleaner.  It is true that it offers no
new functionality to the language, but I would argue that it is easier to
understand "int a[numberof(enum foo)]" at a glance than it is to understand
"int b[NUMBER_OF_FOO]".  In the first case, all you need to understand is
the operation of 'numberof'.  In the second case, you also to understand the
programmer's style and naming conventions.


>Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>

Dave Schaumann		|  And then -- what then?  Then, future...
dave at cs.arizona.edu	|  		-Weather Report



More information about the Comp.lang.c mailing list