Passing Arguments on in Variadic Functions
jmh530 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 17 14:36:33 PDT 2015
On Thursday, 17 September 2015 at 21:27:31 UTC, jmh530 wrote:
>
There's probably a way to clean this up better, but this is what
I was talking about.
import std.algorithm : sum;
import std.stdio : writeln;
import std.traits : isNumeric;
auto test(T)(T x, bool sample=true)
{
auto sum_x = sum(x);
if (sample)
return sum_x;
else
return sum_x / 2;
}
auto test(T, V ...)(T x, V y)
{
static if (y.length)
{
static if (y.length == 2 &&
is(typeof(y[0]) == bool) &&
isNumeric!(typeof(y[1])))
{
auto sum_x = sum(x, y[1]);
if (y[0])
return sum_x;
else
return sum_x / 2;
}
}
}
void main()
{
int[] x = [10, 5, 15, 20, 30];
writeln(test(x, true));
writeln(test(x, false));
float seed = 1;
writeln(test(x, true, seed));
writeln(test(x, false, seed));
}
More information about the Digitalmars-d-learn
mailing list