İbrahim
İbrahim

Reputation: 1021

Delphi Generics: E2037: Declaration of 'XXX' differs from previous declaration

I want to convert my C++ code to Delphi code. But I get this error from Delphi compiler: Declaration of 'callFunction<T>' differs from previous declaration.
My C++ code:

class Example
{
public:
  template<typename T>
  static void callFunction(const T value);
};

template<>
void Example::callFunction<int>(const int value)
{
  cout << "Integer = " << value << endl;
}

template<>
void Example::callFunction<double>(const double value)
{
  cout << "Double = " << value << endl;
}

template<>
void Example::callFunction<char*>(char* const value)
{
  cout << "Char* = " << value << endl;
}

int main()
{
  Example::callFunction<int>(17);
  Example::callFunction<double>(3.8);
  Example::callFunction<char*>("Hello");

  return 0;
}

This code is running succesfully. But my Object Pascal code is not running.
My Delphi code:

type
  Example = class
  public
    class procedure callFunction<T>(const val: T);
  end;

{ Example }

class procedure Example.callFunction<Integer>(const val: Integer);
begin
  Writeln('Integer');
end;

class procedure Example.callFunction<Double>(const val: Double);
begin
  Writeln('Double');
end;

class procedure Example.callFunction<PChar>(const val: PChar);
begin
  Writeln('PChar');
end;

begin
  Example.callFunction<Integer>(17);
  Example.callFunction<Double>(3.8);
  Example.callFunction<PChar>('Hello');

  Readln;
end.

How can I convert my C++ code to Delphi code? What is reason of the error? Can I convert my code to Delphi like this? Thanks.

Upvotes: 1

Views: 984

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595319

As @Dsm explained, Delphi Generics do not support specialization like C++ Templates do. However, all is not lost. You can do the following with Generics (though it kind of defeats the purpose of using Generics):

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  Example = class
  public
    class procedure callFunction<T>(const value: T);
  end;

class procedure Example.callFunction<T>(const value: T);
begin
  if TypeInfo(T) = TypeInfo(Integer) then
    WriteLn('Integer = ', PInteger(@value)^)
  else
  if TypeInfo(T) = TypeInfo(Double) then
    WriteLn('Double = ', PDouble(@value)^)
  else
  if TypeInfo(T) = TypeInfo(String) then
    WriteLn('String = ', PString(@value)^);
end;

begin
  Example.callFunction<Integer>(17);
  Example.callFunction<Double>(3.8);
  Example.callFunction<String>('Hello');
  ReadLn;
end.

Integer = 17
Double = 3.80000000000000E+0000
String = Hello

Upvotes: 2

Dsm
Dsm

Reputation: 6013

I think that you misunderstand generics. The whole point about generics is that you don't explicitly use the type within the class definition so lines like

class procedure Example.callFunction<Integer>(const val: Integer);

are not legal. Instead, in this case you would not use generics, but overloaded functions, like this.

type
  Example = class
  public
    class procedure callFunction(const val: integer); overload;
    class procedure callFunction(const val: double); overload; 
    class procedure callFunction(const val: string); overload;
  end;

{ Example }

class procedure Example.callFunction(const val: Integer);
begin
  Writeln('Integer');
end;

class procedure Example.callFunction(const val: Double);
begin
  Writeln('Double');
end;

class procedure Example.callFunction(const val: string);
begin
  Writeln('string');
end;

begin
  Example.callFunction(17);
  Example.callFunction(3.8);
  Example.callFunction('Hello');

  Readln;
end.

Note that I have used string rather than PChar, since this is more likely what you need.

Upvotes: 5

Related Questions