variadic function: passing args

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jul 4 02:27:56 PDT 2006


icee wrote:
> is there a way to pass the ... args from one variadic function to another
> variadic function?
> 
> consider such case:
> void vf1(int a, ...) {
> 
> vf2(...);
> }
> void vf2(...) {
> }
> 
> can vf2 take _arguments and _argptr from vf1?
> 
> 

Simply way to do it, is to write vf2 twice, once as a variadic wrapper, and once taking a 
vararg pair.  In other words:

# import std .stdarg ;
#
# void vf2 (...) { vf2(_arguments, _argptr); }
#
# void vf2 (TypeInfo[] _arguments, va_list _argptr) {
#   /* real work */
# }

Sad to say, this is one time when I do start to miss a preprocessor just a little.  It'd 
be somewhat nice to be able to do:

########## module altvararg ;
# import std .stdarg ;
#
# #define ALT_VARIADIC(FNAME) \
#   void FNAME (...) { FNAME(_arguments, _argptr; } \
#   void FNAME (TypeInfo[] _arguments, va_list _argptr)
#

########## module foo ;
# import altvararg ;
#
# ALT_VARIADIC(vf2) {
#   /* real work */
# }

Maybe if we /did/ have a way it would be better.  Maybe something like:
# vf2(_arguments ... _argptr);

Where the '...' in this case has become an operator meaning to pass these varargs to the 
callee as such.  Not sure if it'd be the best syntax, but its the simplest thing that 
comes to mind.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list