Conversation
Edited yesterday

Hello Mastodon!

Since this is my first post, I thought I'd share some incredibly niche C++ trivia / pedantry:

For an enum whose enumerators all have the value 0, C++ asks us to imagine a hypothetical integer type with minimal width that can represent 0 (https://eel.is/c++draft/dcl.enum#8.sentence-2). This means we must consider the case where the width is 0. For an unsigned integer type, this gives a range of representable values of [0, 0], and that's the type we pick. But before we can determine that that's minimal, we must also consider a signed integer type with a width of 0, for which we get a range of representable values of [-½, -½]! (https://eel.is/c++draft/basic.fundamental#1.sentence-5) Conveniently that range does not include 0, so we discover that we must use an unsigned integer type to determine the range of values of the enumeration. (We also rule out an unsigned integer type of negative width as that would have a range of values 0 to -½ (inclusive) or smaller, which I think we can reasonably conclude is an empty range despite the parenthetical.)

In any case: if you ever wondered whether a zero-bit signed integer type in C++ can represent only the value 0 or only the value -1, now you know: no, it can represent only the value -½. Truly a marvelous compromise.

Follow me for more brilliant insights like this one :)

1
6
0
@zygoloid This hits surprisingly close to home as I try to create a compatibility layer for decompilers: since they work with partial information by definition they regularly introduce stuff that has a name but has no/0 length (the rest is an exercise for the user). Now how do you explain the *other* decompiler what that zero-length thing means? :)
0
0
0