Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To invoke a variadic function with unamed arguments of another variadic function

I have two variadic function as foo(format, ...) and bar(format, ...). I want to implement function foo so that it can invoke bar with the same list of arguments it has. That is,

foo(format...)
{
 ...
 bar(format, ...);
}

For instance, invoking foo("(ii)", 1, 2) will invoke bar with same arguments bar("(ii)", 1, 2). How should this foo function be implemented?

PS: function bar is from a legacy library which I cant change its interface.

like image 438
Richard Avatar asked Mar 21 '11 20:03

Richard


1 Answers

Can't be done, as long as all you have is a bunch if functions with ... arguments.

You have to plan ahead for things like that and implement each variadic fuinction in two-staged fashion

void vfoo(format, va_list *args) {
  /* Process `*args` */
}

void foo(format, ...) {
  va_list args;
  va_start(args, format);
  vfoo(format, &args);
  va_end(args);
}

Once you have each of your variadic functions implemented through a pair of va_list * function and ... function, you can delegate the calls using the va_list * versions of the functions

void vfoo(format, va_list *args) {
  ...
  vbar(format, args);
  ...
}
like image 192
AnT Avatar answered Oct 02 '22 22:10

AnT