www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variadic funtions

reply Tomas Lindquist Olsen <tomas famolsen.dk> writes:
Hi all.
Does anyone know any tricks for passing all arguments of a variadic function
to another?

void foo(...)
{
        // do something
        bar(...);
}

void bar(...)
{
        writefln(...);
}

Thanx
Feb 12 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Tomas Lindquist Olsen" <tomas famolsen.dk> wrote in message 
news:eqr8q3$1a4a$1 digitalmars.com...
 Hi all.
 Does anyone know any tricks for passing all arguments of a variadic 
 function
 to another?

 void foo(...)
 {
        // do something
        bar(...);
 }

 void bar(...)
 {
        writefln(...);
 }

 Thanx

Not with "normal" functions. Usually the convention is to declare "..." and "TypeInfo[] arguments, va_list argptr" versions of your function so that other variadic funcs can call it, like void foo(...) { vfoo(_arguments, _argptr); } void vfoo(TypeInfo[] arguments, va_list argptr) { vbar(arguments, argptr); } void bar(...) { vbar(_arguments, _argptr); } void vbar(TypeInfo[] arguments, va_list argptr) { // The only way to do this.. dout.writefx(arguments, argptr, true); } With variadic templates, it becomes much more straightforward, although your output code can become bloated: void foo(T...)(T args) { bar(args); } void bar(T...)(T args) { writefln(args); } You can also look at std.boxer, though I haven't really seen that many people use it for varargs..
Feb 12 2007
parent reply Tomas Lindquist Olsen <tomas famolsen.dk> writes:
Jarrett Billingsley wrote:

 Not with "normal" functions.  Usually the convention is to declare "..."
 and "TypeInfo[] arguments, va_list argptr" versions of your function so
 that other variadic funcs can call it
 

Guess that is what I was trying to avoid.
 void foo(...)
 {
     vfoo(_arguments, _argptr);
 }
 
 void vfoo(TypeInfo[] arguments, va_list argptr)
 {
     vbar(arguments, argptr);
 }
 
 void bar(...)
 {
     vbar(_arguments, _argptr);
 }
 
 void vbar(TypeInfo[] arguments, va_list argptr)
 {
     // The only way to do this..
     dout.writefx(arguments, argptr, true);
 }
 

Thanx for this I wasn't aware of OutStream.writefx . It will work for me just fine.
 With variadic templates, it becomes much more straightforward, although
 your output code can become bloated

Yeah I don't want to go this way. Though it's cool that a tuple can be passed as variadic args. Thanx very much for your reply :)
Feb 12 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Tomas Lindquist Olsen" <tomas famolsen.dk> wrote in message 
news:eqr9n4$1a4a$2 digitalmars.com...
 Guess that is what I was trying to avoid.

You would think that this kind of stuff could be automated. I mean, if I'm in a variadic function, and I call another variadic function with '...' in its parameter list, shouldn't that be obvious what I mean? Can't the compiler just go "oh, well I guess I can just forward the _arguments and _argptr params to that variadic function"? It seems like something that would be relatively easy to implement. I don't know, maybe it has something to do with the code generation.
 Yeah I don't want to go this way. Though it's cool that a tuple can be
 passed as variadic args.

I love tuples so much.
Feb 12 2007
parent Tomas Lindquist Olsen <tomas famolsen.dk> writes:
Jarrett Billingsley wrote:

 
 You would think that this kind of stuff could be automated.  I mean, if
 I'm in a variadic function, and I call another variadic function with
 '...' in
 its parameter list, shouldn't that be obvious what I mean?  Can't the
 compiler just go "oh, well I guess I can just forward the _arguments and
 _argptr params to that variadic function"?  It seems like something that
 would be relatively easy to implement.  I don't know, maybe it has
 something to do with the code generation.
 

Yeah... It does seem like a "missing" feature. Though the ABI really doesn't give you a clue. (or I just dont get what "_arguments", "hidden" and "this" mean...)
Feb 12 2007