I try to create a general wrapper function which takes any function as argument and also their parameters. Just something like the 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 wrapper function itself works. It calls the given function object (std::function
, functor or just a "normal" function) with the given arguments. But i also like to return its return value.
This should work with the removed return
-statement, but unfortunately i dont know how to declare the wrapper functions return type.
I tried many things (e.g. with decltype
), but nothing worked. My question is now, how i get the following code running?
#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 for the ???
.
Thank you for any ideas
Regards Kevin
->
. Thank you for this. It worked:)! – Inefficiencyfunc(std::forward<Args>(args)...)
– Doc