3 variant questions

Saaa empty at needmail.com
Tue May 12 10:31:59 PDT 2009


> Dear Saaa, these varargs suck badly and you shouldn't use them. It's so
> simple to introduce portability errors or heisenbugs, and it's
heisenbugs :)
> incredibly hard to get it right. You're better off with alternatives.
>
> Alternative 1: Typesafe Variadic Functions
> Useful if the variadic arguments should have only one type. And they're 
> very easy to use. It's documented somewhere on 
> http://www.digitalmars.com/d/1.0/function.html

It doesn't say much about setting/changing the passed arguments.

How do these differ?

int test1( int i ...) { return i;}
int test2( int i ) { return i;}
test1(3);  // returns 3
test2(3);  // returns 3

>
> Alternative 2: Tuples
> http://www.digitalmars.com/d/1.0/template.html#TemplateTupleParameter
> The variadic arguments can have any types, but such functions can't really 
> be virtual methods or delegates.

Well, I should learn about them templates anyways :)
Templates create the necessary appropriately typed instances of a function
 at compile-time.. seems more usefull as my get function should work with
any type.

>
>>> Why is this extra step necessary and why won't simple casting not work?
>>
>> You should never directly work with _argptr.  It's not guaranteed to be
>> a simple pointer.  For example, I believe that GCC will sometimes use
>> registers (God only knows WHY).
>
> I think it's because GCC tries to keep the usual calling convention, where 
> some arguments are passed as registers. Which is utterly pointless and 
> stupid (ok, maybe it was easier to implement in gdc), and turns using 
> varargs into hell.
>
> It would be so simple if _argptr was just an array of pointers to the 
> actual arguments. It'd be so simple it makes me mad. 




More information about the Digitalmars-d-learn mailing list