There is a function:
void some_function(int id,...);
question: is there a way to wrap this function? It means to get something like this:
void wrapped_some_function(int id,...)
{
//do smth
some_function(id,...);
}
There is a function:
void some_function(int id,...);
question: is there a way to wrap this function? It means to get something like this:
void wrapped_some_function(int id,...)
{
//do smth
some_function(id,...);
}
With gcc, __builtin_apply_args
and __builtin_apply
, documented here, can do it.
For standard C, there's no way (what other answers suggest can work, but it isn't 100% what you ask for).
But if there's a variation of some_function
that gets va_list
(like vprintf
is a variation of printf
), you can use it.
Since this is also tagged C++: C++11 ftw.
#include <utility> // forward
template<class... Args>
void wrapped_some_function(int id, Args&&... args)
{
//do smth
some_function(id, std::forward<Args>(args)...);
}
If your compiler supports variadic macros maybe you could use that?
#define wrapped_some_function(id, ...) \
do { \
/* Do something here... */ \
some_function(id, __VA_ARGS__); \
} while (0)
I assume your wrapper function doesn't "know" the actual number of arguments passed (and their types) at compile-time, otherwise you could just retrieve them and call the wrapped function.
Then this is solvable only by using inline assembler (hence - platform-dependent solution). This also depends on how exactly do you want to wrap your function, do you need to reserve the stack for your wrapper (i.e. you don't need local function variables) and etc.
In general case there is no way to do it, and because there is no way the creator of some_function should have provided similar function vsome_function
that does not take ..., but a va_list
. Function with variable argument list must have its counter part, a function with va_list
. Consider pairs of functions (printf, vprintf), (sprintf, vsprintf), (CString::Format, CString::FormatV).