Modifiable typesafe variadic arguments

Daniel Keep daniel.keep.lists at gmail.com
Thu Jun 15 04:12:55 PDT 2006



Deewiant wrote:
> Trying to do something like this doesn't work:
> 
> void foo(int[] arr...) {
> 	for (int i = 0; i < arr.length; ++i)
> 		arr[i] = 3;
> }
> 
> foo(x, y, z);
> 
> The variables passed retain their original values. Defining arr as inout or out
> doesn't work, since that would be modifying the array reference, and as such the
> compiler doesn't allow it.
> 
> Is there any way of doing it other than using pointers, like in the following?
> 
> void foo(int*[] arr...) {
> 	for (int i = 0; i < arr.length; ++i)
> 		*arr[i] = 3;
> }
> 
> foo(&x, &y, &z);
> 
> My guess is there isn't, since std.stream.InputStream.readf, for instance, uses
> pointers for this as well. Any ideas?

Not that I can find after a quick squiz at the docs.  Honestly, I think
that when you ever pass something to a function that it can change,
there should be some explicit notice of this (like having to pass the
argument by address).

Well, ok, you could always play the "write a template for every possible
argument length and use THAT to pass the arguments by address to the
*real* function", but I think that's probably unnecessary :)

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d-learn mailing list