Enum declaration with subrange bounds violation?

Enum declaration with subrange bounds violation?

type
  {$MINENUMSIZE 2}
  TMyEnum2 = (
    Enum11 = 1,
    Enum12 = 2,
    Enum13 = 3,
    Enum1Mid1 = $7fff,  // No warning
    Enum1Mid2 = $8000,  // No warning
    Enum1Max  = $ffff); // No warning

  {$MINENUMSIZE 4}
  TMyEnum4 = (
    Enum21 = 1,
    Enum22 = 2,
    Enum23 = 3,
    Enum2Mid1 = $7fffffff,  // No warning
    Enum2Mid2 = $80000000,  // Warning
    Enum2Max  = $ffffffff); // Warning

The two last entries in the second type declaration above, gives a warning: W1012 Constant expression violates subrange bounds.

Any known QC issue or workaround for this?
Any reason that the enum can't use the most signficant bit in a 4 byte size enum?

Comments

  1. MaxInt ($7FFFFFFF) seems to be the limit, I'm guessing that the compiler uses Longint (ie. signed dword) for 4-byte enums. Funny that this gets rid of the warning (in XE2):

    type
     TMyEnum4 = (
        Enum21 = 1,
        Enum22 = 2,
        Enum23 = 3,
        Enum2Mid1 = MaxInt,               // No warning
        Enum2Mid2 = MaxInt + 1,           // No Warning
        Enum2Max  = MaxInt + MaxInt + 1); // No Warning

    However, although Ord(Enum2Max) returns $FFFFFFFF as expected, High(TMyEnum4) still returns $7FFFFFFF.

    ReplyDelete
  2. That workaround would need a Assert(SizeOf(MaxInt) = 4) somewhere :)

    ReplyDelete

Post a Comment