Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-09

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

2 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Reply by email

LacaK
to 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;
Show quoted text 21 lines
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.

Reply by email