A code comment noting the steps to take when a 256th enum field is added
I left this comment for myself and future maintainers of some code I had written:
// If the number of members of this enum exceeds 255, then // // (1) update blah blah blah, and // (2) shame on you.
Tomorrow in The Daily WTF: “Let’s give an applause to our guest star, Raymond Chen!”
“255 members in this enum ought to be enough for anybody” 🙂
What’s wrong with having 256 values in an enum? Still fits in a byte, right?
Some compilers, e.g. armcc, use variable-width enums. When you blow past 255 values the size of your enum goes from 8 to 16 bits, breaking binary compatibility with any API that was built for the <= 255-value enum.
Isn’t enum just dumb integer constants? Or am i thinking ol’ C
The underlying type of an unscoped enum is implementation defined.
But there are two big issues here, first, we don’t know what really happens in this case since it was about a witty comment that Raymond put in his code. There could be other issues like alignment issues in a structure to consider. Secondly, Dave Gzorple missed the subtle joke/nitpick that 0-255 is actually 256 values, so it shouldn’t really hit 16 bits until the 257th value in the enum.
“The underlying type of an unscoped enum is implementation defined.”
This is why you’ll find enum values, aptly named FORCE_DWORD, in, for example, DirectX headers, to force the enum size to 32 bits