Vector operations doesn't convert to a common type?

Philippe Sigaud philippe.sigaud at gmail.com
Sun Apr 17 04:53:58 PDT 2011


On Sat, Apr 16, 2011 at 13:37, simendsjo <simen.endsjo at pandavre.com> wrote:
> I tried using a template mixin to avoid having to type this all over, but I
> cannot get it to work.. I think I have something wrong with my alias usage,
> but that's just speculating :)

Try this:


auto arrayOp(string op, A, B, int N, int n = 0)(A[N] a, B[N] b)
{
    static if (n < N - 1)
        mixin("return [a[n] " ~ op ~ " b[n]] ~ arrayOp!(op,A,B,N,n+1)(a, b);");
    else
        mixin("return [a[n] " ~ op ~ " b[n]];");
}

void main()
{
    int[3]   a = [1,2,4];
    float[3] b = [1,2,4];
    auto c = arrayOp!"+"(a,b);
    writeln(c);
}

I had to resort to the N/n kludge, because of A[N] <-> A[] incompatibilities.

Note that this could easily be extend to accept any string function
(ala std.algorithm) and any number of arrays.

Philippe


More information about the Digitalmars-d-learn mailing list