Passing a generic struct as parameter

Simen Kjaeraas simen.kjaras at gmail.com
Fri Jul 1 08:19:36 PDT 2011


On Fri, 01 Jul 2011 08:58:32 +0200, Zardoz <luis.panadero at gmail.com> wrote:

> Well, the problem is that I must do the cast always, see :
>
> // alias Matrix!(real,4) Mat4r;
> // alias Vector!(float, 3) Vec3f;
> // alias Vector!(real, 4) Vec4r;
> // In Mat4r, VCol it's aliased to Vector!(real, 4)
>
> auto tcol = Mat4r.IDENTITY;
>
> auto v = cast(tcol.VCol) Vec3f(1, 2 ,3 );
> auto v2 = Vec4r(-1, -2 ,-3 ,-4);
>
> tcol[1] = v2; // Do a compiler error
> tcol[2] = v;

So more likely, this is what you want:

void opIndexAssign(U)(Vector!(U,dim) v, size_t j) {
     static if (is(U == T)) {
         col[j] = v;
     } else {
         col[j] = cast(VCol)v;
     }
}


-- 
   Simen


More information about the Digitalmars-d-learn mailing list