Einzelnen Beitrag anzeigen

WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Template for numbers only

  Alt 5. Feb 2015, 12:19
Delphi-Version: XE6
I have function:

Delphi-Quellcode:
function SimilarValue(const AValue: Float32; AValues: array of Float32; out AResult: Float32): Int32;
var
  I: Int32;
  Distance: Float80;
  SmallestDistance: Float80;
begin
  Result := -1;
  AResult := 0;

  SmallestDistance := 16777216;

  for I := Low(AValues) to High(AValues) do
  begin
    Distance := Sqr(AValue - AValues[I]);
    if Distance < SmallestDistance then
    begin
      Result := I;
      AResult := AValues[I];

      SmallestDistance := Distance
    end;
  end;
end;
I want to make it working for any numeric type, but won't to overloading. Is it possible to do it with templates?

Delphi-Quellcode:
type
  TSimilarValue<T> = class
    class function Get(const AValue: T; AValues: array of T; out AResult: T): Int32;
  end;

class function TSimilarValue<T>.Get(const AValue: T; AValues: array of T; out AResult: T): Int32;
  Mit Zitat antworten Zitat