enum, bit fields, cpp...

turner at ucbesvax.UUCP turner at ucbesvax.UUCP
Wed Feb 1 19:27:00 AEST 1984


Perhaps there should also be a "bitsizeof" in addition to "sizeof".
Defining number of bits per byte (#define NBBY in some places) is
not good enough for machines whose byte does not evenly divide the bit-
length all scalar types.

Thus, with enum types cleaned up the way they ought to be (hope, pray)
one could say "bitsizeof (enum foo)" for various ultra-hacky purposes.
Even, I would hope,

    typedef enum { north, south, east, west } direction;

    struct {
	...
	bool flag: bitsizeof (bool);	/* (dumb, since always == 1) */
	direction dir: bitsizeof (direction);	/* pack next to flag */
	...

Hmmm, this seems fraught with terrors for compiler-writers, somehow.  Are
enums always int-sized (or better, rounded to short or char as appropriate)
when used alone?  In which case, are their bitsizeof's always that of the
primitive scalars they fit into?  Or should bitsizeof always mean "minimum
number of bits required to represent this scalar type?  That would at least
be consistent.  Having enums be always packed when they are members of
structures is also possible, but possibly confusing as well.

Or are there other solutions to this whole mess?
---
Michael Turner (ucbvax!ucbesvax.turner)



More information about the Comp.lang.c mailing list