Reusing variable argument list

K

Kufa

Hi,

I might post a stupid question, but i prefer give it a shot, as i tried
several things without any success.

In a function called with a varying number of arguments of varying
types, could it be possible to pass all those arguments to another
function that takes a varying number of arguments (smart use of
va_list?).

A small example:

struct A
{
virtual void doStuff( int a, ... ) { /* do stuff here */ }
};
struct ClassB : public A
{
virtual void doStuff( int a, ... ) { A::doStuff( a /*, ... */ ); } //
<-- what do do here?
};

I first though of two things: assembly, that i'd love to avoid as i
need cross-platform support, and some template stuff, but i couldn t
get it working.

So are those kind of calls possible, and if so how?

Thanks,

David
 
I

Ivan Vecerina

: In a function called with a varying number of arguments of varying
: types, could it be possible to pass all those arguments to another
: function that takes a varying number of arguments (smart use of
: va_list?).

What you do is pass a va_list parameter to the following function.
Look at the way vprintf is declared.

When you actually need to go through the list of parameters multiple
times, this may work by just copying the va_list instance.
If your platform supports va_copy (introduced in C99), using it
will provide a formally corect solution.

hth -Ivan
 
K

Kufa

Ivan said:
What you do is pass a va_list parameter to the following function.
Look at the way vprintf is declared.

Thanks i lot. I actually thought about passing va_list as a parameter,
but wasn't sure if nested calls to va_arg could break something on a
platform:

void B::doStuff( int a, va_list args )
{
/* Someone decides to do something on the first parameter */
ProperType obj = va_arg(args);
/* do stuff */
A::doStuff( a, args );
}
When you actually need to go through the list of parameters multiple
times, this may work by just copying the va_list instance.
If your platform supports va_copy (introduced in C99), using it
will provide a formally corect solution.

I'll indeed use va_copy when available, but is it safe to assume the
va_arg wont break anything when va_copy is not available? Or should i
look in all my va_list implementations?

Thanks again for your answer.
 
I

Ivan Vecerina

:
: Ivan Vecerina wrote:
: > What you do is pass a va_list parameter to the following function.
: > Look at the way vprintf is declared.
:
: Thanks i lot. I actually thought about passing va_list as a parameter,
: but wasn't sure if nested calls to va_arg could break something on a
: platform:
:
: void B::doStuff( int a, va_list args )
: {
: /* Someone decides to do something on the first parameter */
: ProperType obj = va_arg(args);

You don't call va_arg again here, you re-use the copy of the
va_list that was initialized by the caller.

: /* do stuff */
: A::doStuff( a, args );
: }
:
: > When you actually need to go through the list of parameters multiple
: > times, this may work by just copying the va_list instance.
: > If your platform supports va_copy (introduced in C99), using it
: > will provide a formally corect solution.
:
: I'll indeed use va_copy when available, but is it safe to assume the
: va_arg wont break anything when va_copy is not available? Or should i
: look in all my va_list implementations?

Copying a va_list "just works" on the 2-3 compilers I have been using.
You'll have to try...

: Thanks again for your answer.

You are most welcome!
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top