Passing Arguments on in Variadic Functions

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 14 13:13:02 PDT 2015


On Monday 14 September 2015 21:59, jmh530 wrote:

> This approach gives the correct result, but dmd won't deduce the 
> type of the template. So for instance, the second to the last 
> line of the unit test requires explicitly stating the types. I 
> may as well use the alternate version that doesn't use the 
> variadic function (which is simple for this trivial example, but 
> maybe not more generally).

You can use a variadic template instead:

----
import std.algorithm : sum;

auto test(R, E ...)(R r, E e)
{
    return sum(r, e);
}

unittest
{
    int[] x = [10, 5, 15, 20, 30];
    assert(test(x) == 80);
    assert(test(x, 0f) == 80f);
    assert(test(x, 0f) == 80f);
}
----


More information about the Digitalmars-d-learn mailing list