tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Do all -current compilers support C23 typed enum syntax?
> However, it looks like sometime in the last couple years Apple has also deci$
I think this is an EXTREMELY bad idea, at least for C.
The whole point of such a default: case, especially when its body is a
panic, abort, or the like, is to catch cases where something has gotten
corrupted. Declaring the enum closed (to use the terminology of the
cited #defines, assuming closed means "no other values ever") does not
affect that. Even if the compiler generates checking code to prevent
assigning an invalid value to an object or member of such an enum type,
nothing prevents things such as wild pointer stores, or reading
corrupted data from a file or network connection or the like, from
creating invalid values, and such defaults: cases are extremely
valuable firewalls to catch such things as early as feasible rather
than letting the damage cascade.
I routinely add "default: abort(); break;" or the like to my switches,
and they have caught corruption of various sorts far too often for me
to give them up, whether by not writing them or by a compiler
arrogating to itself the decision to delete them.
The only case where such an "optimization" is actually justified in my
opinion is where every possible value of the underlying storage unit
*is* taken by the enum, such as a 256-member enum having no explicit
values declared with unsigned char as its underlying type on a machine
with 8-bit unsigned chars. While such cases doubtless exist, I
consider them rare enough that I would still describe such a
transformation of the code as broken, possibly unless specifically
requested by pragma, non-default compiler flag, or the like.
/~\ The ASCII Mouse
\ / Ribbon Campaign
X Against HTML mouse%rodents-montreal.org@localhost
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
Home |
Main Index |
Thread Index |
Old Index