Thread overview
[Doubt] Variadic arguments as reference (Possible?)
Jun 14, 2013
MattCoder
Jun 14, 2013
Regan Heath
Jun 14, 2013
MattCoder
Jun 14, 2013
bearophile
Jun 14, 2013
MattCodrr
June 14, 2013
Hi,

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.

http://dpaste.dzfl.pl/515edfb8

or

import std.stdio;
import std.conv;

void sum(double value, in double[] numbers ...){
	foreach(num; numbers)
		writeln(num+value);
}

void main(){
	auto a = 1, b = 2, c = 3;
	sum(10, a, b, c);
	
	writeln("a = "~to!string(a),
			"\nb = "~to!string(b),
			"\nc = "~to!string(c));
}

Thanks,

Matheus.
June 14, 2013
On Fri, 14 Jun 2013 13:55:29 +0100, MattCoder <mattcoder@gmail.com> wrote:
> I want to know if there is a way to pass variadic arguments as reference? DMD says no!

import std.stdio;
import std.conv;

void sum(double value, double*[] numbers ...){
	foreach(ref num; numbers)
		*num = *num + value;
}

void main(){
	double a = 1, b = 2, c = 3;
	sum(10, &a, &b, &c);
	
	writeln("a = "~to!string(a),
			"\nb = "~to!string(b),
			"\nc = "~to!string(c));
}

R
-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
June 14, 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
June 14, 2013
On Friday, 14 June 2013 at 13:20:26 UTC, Regan Heath wrote:
> import std.stdio;
> import std.conv;
>
> void sum(double value, double*[] numbers ...){
> 	foreach(ref num; numbers)
> 		*num = *num + value;
> }
>
> void main(){
> 	double a = 1, b = 2, c = 3;
> 	sum(10, &a, &b, &c);
> 	
> 	writeln("a = "~to!string(a),
> 			"\nb = "~to!string(b),
> 			"\nc = "~to!string(c));
> }
>
> R

Hi Regan,

My fault, I thought that those variadics only works with "in" after I assume wrong from (http://ddili.org/ders/d.en/parameter_flexibility.html), but your code works well.

Thanks,

Matheus.
June 14, 2013
On Friday, 14 June 2013 at 13:24:18 UTC, bearophile wrote:
> 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);
> }



Hi bearophile,

I know that I don't specified on my first post, but since I need
a generic type parameter on my real problem, your ref TArgs seems
to fit nicely.

Thanks.