April 25th, 2023

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.
Topics
Other

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

7 comments

Discussion is closed. Login to edit/delete existing comments.

  • Neil Rashbrook

    What’s wrong with having 256 values in an enum? Still fits in a byte, right?

    • Dave Gzorple

      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.

      • Sigge Mannen

        Isn’t enum just dumb integer constants? Or am i thinking ol’ C

      • Me Gusta

        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...

        Read more
      • Tom Lint

        “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

  • Reinhard Weiß

    “255 members in this enum ought to be enough for anybody” 🙂

  • Antonio Rodríguez

    Tomorrow in The Daily WTF: “Let’s give an applause to our guest star, Raymond Chen!”