I am trying to create a generic wrapper function that takes a function as an argument to a template and takes the same arguments as this function as arguments. For example:
template <typename F, F func> /* return type of F */ wrapper(Ts... Args /* not sure how to get Ts*/) { // do stuff auto ret = F(std::forward<Ts>(args)...); // do some other stuff return ret; }
The solution must be bound to a function pointer with the same type as func so that I can pass it to C api. In other words, the solution should be a function, not a functional object. Most importantly, I need to be able to do the work in a wrapper function .
If the inline comments are not clear, I would like to do something like the following:
struct c_api_interface { int (*func_a)(int, int); int (*func_b)(char, char, char); }; int foo(int a, int b) { return a + b; } int bar(char a, char b, char c) { return a + b * c; } c_api_interface my_interface; my_interface.func_a = wrapper<foo>; my_interface.func_b = wrapper<bar>;
I searched for relevant posts and found them, but none of them are quite what I am trying to do. Most of these messages relate to function objects. Is that what I'm trying to do is even possible?
Function passed as template argument
Functional shell using (functional object) class (variational)
How does wrapping a function pointer and function object work in common code?
How to get function pointer argument types in a variational pattern class?
Common functor for functions with any argument list
C ++ Functors - and their use
In response to the first 2 answers, I edited the question so that it is clear that I need to be able to do the work in a wrapper function (i.e. change some global state before and after calling the wrapped function)