I'm trying to write a wrapper for a C-style variadic function like printf, which adds some extra arguments, but I'm having trouble with it:
void printf(char* fmt, ...); // the thing I'm trying to wrap
void wrapper(char* fmt, ...)
{
printf(fmt, extra_arg1, extra_arg2, /* the variadic arguments */);
}
but what do I write for /* the variadic arguments */
?
Even if the function I'm trying to wrap has a version that takes a va_list
, I can't do it:
void vprintf(char* fmt, va_list args);
void wrapper(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, extra_arg1, extra_arg2, args);
va_end(args);
}
extra_arg1
, extra_arg2
and args
don't magically turn into a va_list
that vprintf
expects.
I know I could write a macro and use __VA_ARGS__
:
void printf(char* fmt, ...); // the thing I'm trying to wrap
#define wrapper(fmt, ...) printf(fmt, extra_arg1, extra_arg2, __VA_ARGS__)
but I'm trying to avoid that and write the wrapper as a function. Is there a way to do that?
(By the way, I can't use C++11 variadic templates either.)