Generics: Error: Can't determine which overloaded function to call

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Generics: Error: Can't determine which overloaded function to call

LacaK
Hi *,

I have generics record, where one method computes distance between 2 points:

function T2DPoint.Distance(P: T2DPoint): Single;
begin
   Result := Sqrt(Sqr(x-P.x) + Sqr(y-P.y));
end;

But this does not compile as there are many versions of Sqr() in the
scope Sqr(longint), Sqr(QWord), Sqr(Extended), Sqr(int64)

Point coordinates (x,y) can be either integer or single (in my case)
when specializing, so decision which version of Sqr() to call should be
postponed to specializing phase, does not?

Is there other solution as:

function T2DPoint.Distance(P: T2DPoint): Single;
var dx,dy: T;
begin
   dx := x-P.x;
   dy := y-P.y;
   Result := Sqrt(dx*dx + dy*dy);
end;

(for integer operations Sqr() is implemented as "value*value", so there
is no difference (compared to above mentioned implementation) but for
floating point operations there is called internal function
"fpc_in_sqr_real" which is IMO implemented in smarter way that
"value*value" (I can not find where/how is implemented)?)

TIA

-Laco.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generics: Error: Can't determine which overloaded function to call

LacaK
Work-around which produces good assembler without writting/reading
intermediate variables:

class function T2DPoint.Sqr(value: T): T; static; inline;
begin
   Result := value*value;
end;

function T2DPoint.Distance(P: T2DPoint): Single; inline;
begin
   Result := Sqrt(Sqr(x-P.x) + Sqr(y-P.y));
end;


>
> Is there other solution as:
>
> function T2DPoint.Distance(P: T2DPoint): Single;
> var dx,dy: T;
> begin
>   dx := x-P.x;
>   dy := y-P.y;
>   Result := Sqrt(dx*dx + dy*dy);
> end;
>
> (for integer operations Sqr() is implemented as "value*value", so
> there is no difference (compared to above mentioned implementation)
> but for floating point operations there is called internal function
> "fpc_in_sqr_real" which is IMO implemented in smarter way that
> "value*value" (I can not find where/how is implemented)?)
>
> TIA
>
> -Laco.
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal