views:

483

answers:

3

I would like to wrap the xmlrpc "call" function (which takes a variable number of parameters) with another function (also taking a variable number of parameters). I would like to simply forward the variable number of parameters I get passed in to my wrapper function to the xmlrpc "call" function. I know how to use va_start and va_arg, but I really don't care about the values that are passed in, I simply want to forward them along to the wrapped function. Is this possible?

the spec for the function i would like to wrap is

call(const char* url, const char* function, const char* paramSpec, void* result, ...);

my wrapper takes care of the first three parameters and the by reference result, it just needs to forward its extra parameters over to the call function

+2  A: 

Yes, if you have control over the wrapped function, change from "..." to va_list. Look at vprintf as an example.

Not sure if you can wrap (...) with another (...)

Lou Franco
You know what languages need? They need a construct for expanding arrays (or va_args in this case) into parameters, so that we don't have to write a separate function for handling an array input every time this issue comes up...
Mark
C++0x will get varidic templates, which is basically this. You handle input like that recursively.
rlbond
+5  A: 

Unfortunately, there is no way to provide perfect forwarding of a function that takes a variable number of arguments via the ... syntax. This is why it is a best practice to implement all public variable argument list functions in terms of functions taking a va_list and provide both interfaces to any client code. This is why there is printf and vprintf, sprintf and vsprintf, etc. in the standard library.

If there isn't a vcall or similar taking a va_list then there is no easy way to do what you want. The only possibility is to interpret the other parameters to work out what must be in the variable argument list passed to your function based on the supplied fixed parameters, to pull all the arguments from the variable argument list into separate variables and make one of a number of possibly explicit calls to call based on the number and type of arguments extracted. It is not possible to do this with complete generality.

Charles Bailey
A: 

This may be possible with evil hacks on some platforms. On x86, arguments are pushed onto the stack from right to left, so if you were to remove the return address from the stack, push your additional arguments, then replace it, you could (in theory) do what you want. The hard part is when you then want to do something with the return value from within the wrapper...

bdonlan