[fpc-devel] constant evaluation in generics params

Martin Frb lazarus at mfriebe.de
Wed May 4 04:25:09 CEST 2022


In the below code specialization takes a boolean constant.
The compiler (kind of) allows expressions that can be evaluated at 
compile time.

Of course
     specialize TGen<  5>1  >;
does not work. The ">" can't be distinguished from the end of the param 
list.

Not sure why
     specialize TGen<  5=1  >;
does not work.
It does work though, if the expression is in brackets.

And talking about brackets
     specialize TGen<  (5>1)  >;
does not, but could work. while in () the ">" can't be the end of the 
param list.



program Project1;
{$mode objfpc}{$H+}
type
   generic TGen<const CC: boolean> = class end;

   TBar1 = specialize TGen<true>;
   TBar2 = specialize TGen<(1=1)>;
   TBar3 = specialize TGen<1=1>; // err
   TBar4 = specialize TGen<(1>1)>; // err
   TBar5 = specialize TGen<(1<1)>; // err



More information about the fpc-devel mailing list