The definition of templates in D

Derek ddparnell at bigpond.com
Sun Mar 18 03:29:38 PDT 2012


On Sun, 18 Mar 2012 19:16:02 +1100, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> On 3/18/12, Derek <ddparnell at bigpond.com> wrote:
>> What would be useful is ...
>>   bar!(a, b, c); // is equivalent to
>>   bar!(int, int, int).bar(a, b, c);
>
> You mean like this?
>
> template bar(T...)
> {
>     void bar() { writeln(T); }
> }
>
> void main()
> {
>     int a = 1, b = 2, c = 3;
>     bar!(a, b, c);
> }

Almost, but more like this ...

template add(X,Y,Z)
{
    X add(Y a, Z b)
    {
        return cast(X) (cast(X)a + cast(X)b);
    }
}

void main()
{
      double s;
      int   t;
      ulong u;

      s = 1.23;
      t = 123;
      u = 456;

     t = add!(u,s);

     writefln( "%s %s %s", s,t, u );
}



This currently errors with ...

   "Error: template instance add!(u,s) add!(u,s) does not match template  
declaration add(X,Y,Z)"

-- 
Derek Parnell
Melbourne, Australia


More information about the Digitalmars-d mailing list