[Doubt] Variadic arguments as reference (Possible?)

bearophile bearophileHUGS at lycos.com
Fri Jun 14 06:24:17 PDT 2013


MattCoder:

> I want to know if there is a way to pass variadic arguments as 
> reference? DMD says no!
>
> I wrote the snippet code below, but what I want to do in fact 
> is assign the sum back to respective variables to use somewhere 
> else.

One way to do it, but beware of template bloat:

import std.stdio;

void sum(TArgs...)(double value, ref TArgs numbers) {
     foreach (ref num; numbers) // Note: this is a static foreach.
         num += value;
}

void main() {
	auto a = 1, b = 2, c = 3;

     writeln(a, " ", b, " ", c);
	sum(10, a, b, c);
	writeln(a, " ", b, " ", c);
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list