[fpc-devel] Min/Max ordinal value of enum?

Martin Frb lazarus at mfriebe.de
Sun Nov 17 13:11:11 CET 2024


What is the range that is valid for an enum (if enums are given ord 
values with "=")?

With 3.3.1

type TFoo =(a=0, b=$Ffffffff);
project1.lpr(3,29) Error: Enumeration symbols can only have values in 
the range of -2^31 to 2^31-1

So the compiler clearly thinks it is a signed integer (longint).


The documentation is not specific about it.
https://www.freepascal.org/docs-html/prog/progsu157.html

It doesn't exactly say what values the enum-val can have. It just 
specifies how many there can be.
0..255 means apparently an enum can have between 0 and 255 values.

It does not explicitly say that those values are the values between 0 
and 255.

It specifies the storage requirements by using unsigned examples 
(byte/word).
Again, not explicitly saying that the stored value must have an unsigned 
ordinal value.
But, some suggestive element in it....

-----------------
Further 3.3.1

program Project1;
{$R-}
type TFoo =(a, b);
begin
writeln(ord(TFoo(1)));
writeln(ord(TFoo($ffffffff)));

writes
1
-1

--------------
In that light my earlier mail "packed enum too small?" below...

Should that be reported as bug?

On 13/11/2024 16:50, Martin Frb via fpc-devel wrote:
> The below gives a size of 1 byte => meaning that both values are the 
> same $FF
>
>
> program Project1;
> {$PackEnum 1}
> type
>   Tfoo = (f1=-1, f2=255);
> var
>   a,b: TFoo;
> begin
>   writeln(SizeOf(TFoo));
>   a:= f1;
>   b:= f2;
>
>
>   b:= f2;
> end.
>
>
> Changing it to:
>   Tfoo = (f1=-129, f2=255);
>
> gives a size of 2 bytes.
>
>
> It seems the range that returns 1 byte goes from -127 to 255 => which 
> is clearly more than fits into one byte?


More information about the fpc-devel mailing list