I am trying to create a general wrapper function that takes any function as an argument, as well as their parameters. Just something like a std::thread constructor.
My current code is:
#include <iostream> using namespace std; template<typename FUNCTION, typename... ARGS> void wrapper(FUNCTION&& func, ARGS&&... args) { cout << "WRAPPER: BEFORE" << endl; auto res = func(args...); cout << "WRAPPER: AFTER" << endl; //return res; } int dummy(int a, int b) { cout << a << '+' << b << '=' << (a + b) << endl; return a + b; } int main(void) { dummy(3, 4); wrapper(dummy, 3, 4); }
The wrapping function itself works. It calls the given function object ( std::function , a functor, or simply a βnormalβ function) with the given arguments. But I also like to return the return value.
This should work with remote return -statement, but unfortunately I don't know how to declare the return type of wrapper functions.
I tried a lot of things (e.g. with decltype ) but nothing worked. Now my question is: how can I get the following code?
#include <iostream> template<typename FUNCTION, typename... ARGS> ??? wrapper(FUNCTION&& func, ARGS&&... args) { cout << "WRAPPER: BEFORE" << endl; auto res = func(args...); cout << "WRAPPER: AFTER" << endl; return res; } int dummy(int a, int b) { cout << a << '+' << b << '=' << (a + b) << endl; return a + b; } int main(void) { dummy(3, 4); cout << "WRAPPERS RES IS: " << wrapper(dummy, 3, 4) << endl; }
I think the code should work except ??? .
Thanks for any ideas.
Relations Kevin
source share