C ++ function call statement with function as template argument

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)

+7
c ++ templates variadic-functions variadic-templates
source share
5 answers
 #include <utility> #include <iostream> 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; } template<typename Fn, Fn fn, typename... Args> typename std::result_of<Fn(Args...)>::type wrapper(Args... args) { std::cout << "and ....it a wrap "; return fn(std::forward<Args>(args)...); } #define WRAPIT(FUNC) wrapper<decltype(&FUNC), &FUNC> int main() { c_api_interface my_interface; my_interface.func_a = WRAPIT(foo); my_interface.func_b = WRAPIT(bar); std:: cout << my_interface.func_a(1,1) << std::endl; std:: cout << my_interface.func_b('a','b', 1) << std::endl; return 0; } 

see http://rextester.com/ZZD18334

+3
source share
 template<class F, F f> struct wrapper_impl; template<class R, class... Args, R(*f)(Args...)> struct wrapper_impl<R(*)(Args...), f> { static R wrap(Args... args) { // stuff return f(args...); } }; template<class F, F f> constexpr auto wrapper = wrapper_impl<F, f>::wrap; 

Use as wrapper<decltype(&foo), foo> .

+7
source share

you probably need something like

 template <typename F> class Wrapper { public: Wrapper(F *func) : function(func) {} operator F* () { return function; } F *function; }; 

What you can use as void (*funcPtr)(int) = Wrapper<void(int)>(&someFunction);

0
source share

I think this will be a short way to do what you want:

 template <typename F> F* wrapper(F* pFunc) { return pFunc; } 

and use it as follows:

 my_interface.func_a = wrapper(foo); my_interface.func_a(1, 3); 
0
source share

you can try something like this (Ugly, but it works)

 #include <iostream> #include <functional> struct wrapper_ctx { wrapper_ctx () { std::cout << "Before" << std::endl; } ~wrapper_ctx () { std::cout << "after" << std::endl; } }; template <typename F, typename... Args> auto executor (F&& f, Args&&... args) -> typename std::result_of<F(Args...)>::type { wrapper_ctx ctx; return std::forward<F>(f)( std::forward<Args>(args)...); } template <typename F> class wrapper_helper; template<typename Ret, typename... Args> class wrapper_helper <std::function<Ret(Args...)>> { std::function<Ret(Args...)> m_f; public: wrapper_helper( std::function<Ret(Args...)> f ) : m_f(f) {} Ret operator()(Args... args) const { return executor (m_f, args...); } }; template <typename T> wrapper_helper<T> wrapper (T f) { return wrapper_helper <T>(f); } int sum(int x, int y) { return x + y; } int main (int argc, char* argv []) { std::function<int(int, int)> f = sum; auto w = wrapper (f); std::cout << "Executing the wrapper" << std::endl; int z = w(3, 4); std::cout << "z = " << z << std::endl; } 
0
source share

All Articles