www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Doubt] Variadic arguments as reference (Possible?)

reply "MattCoder" <mattcoder gmail.com> writes:
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.
Jun 14 2013
next sibling parent reply "Regan Heath" <regan netmail.co.nz> writes:
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/
Jun 14 2013
parent "MattCoder" <mattcoder gmail.com> writes:
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.
Jun 14 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jun 14 2013
parent "MattCodrr" <mattcoder gmail.com> writes:
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.
Jun 14 2013