Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to wrap function having va_args arguments?

Tags:

c

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,...);
}
like image 299
2r2w Avatar asked Feb 22 '12 11:02

2r2w


2 Answers

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.

like image 196
ugoren Avatar answered Nov 10 '22 01:11

ugoren


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)...);
}
like image 22
Xeo Avatar answered Nov 10 '22 01:11

Xeo