[fpc-devel] Templates / Generics

Florian Klaempfl florian at freepascal.org
Fri Nov 4 10:46:51 CET 2005


Peter Vreman wrote:

>>Mattias Gaertner wrote:
>>
>>
>>>This page looks only like the start of a proposal. Neither complete nor
>>>official.
>>>Why do you think, that D2006 will have generics?
>>>
>>>
>>>
>>
>>D2006 <> D11=D2007
> 
> 
> How will Delphi handle the following case with overloads and different types:
> 
> 
> 
> unit test;
> 
> interface
> 
> type
>   List<T> = class
>     data : T;
>     constructor(aData: T);
>     procedure dump;
>   end;
> 
> implementation
> 
> procedure show(i:integer);overload;
> begin
> end;
> 
> procedure show(s:string);overload;
> begin
> end;
> 
> constructor List<T>(aData: T);
> begin
>   Data := aData;
> end;
> 
> procedure List<T>.Dump;
> begin
>   Show(Data);
> end;
> 
> end.

It will be eaten, but as soon as you instanziate with non-integer/non-string you
get an error, see also C++:

#include <string>

using namespace std;

void show(int i)
{
}

void show(string s)
{
}

template<typename T> class test
{
  public:
    T data;
    void p()
    {
      show(data);
    }
};

test<int> mytest1;
test<int *> mytest2;

int main()
{
  mytest1.p();
  mytest2.p();
}




More information about the fpc-devel mailing list