traits: how to split parametrized type into basic type and parameters

Martin Kinkelin noone at spam.com
Mon Feb 14 04:09:45 PST 2011


Hi Philippe,

thank you very much! I added your isVector!T template as well as the two aliases.
opCast!NewType is implemented like this:

V opCast(V)() if (isVector!V)
// parameters {d2,T2} of V are checked when instantiating V: d2 >= 1 && isNumeric!T2
{
    V r;
    foreach (i; 0 .. (d < V.dim ? d : V.dim))
        r._data[i] = cast(V.Type) _data[i];
    return r;
}

thereby allowing to cast to arbitrary component types as well as to arbitrary
dimensions. Works like a charm:

auto f3 = float3(1,2,3);
assert((cast(double2) f3) == double2(1,2));
assert((cast(int3) f3) == int3(1,2,3));
assert((cast(int4) f3) == int4(1,2,3,0));


More information about the Digitalmars-d-learn mailing list