I am trying to write a generic function dispatch mechanism where I do some additional work before calling the actual function (e.g. time the function's execution). The following code works, except for functions of the type void f(....) since we are declaring ret.
#define execute(fn, ...) exec_helper(#fn, fn, ##__VA_ARGS__)
#define execute0(fn) exec_helper(#fn, fn)
template <typename TASK, typename... ARGS>
auto exec_helper(const char *fn_name, TASK&& task, ARGS&&... args)
{
//std::function<std::result_of_t<TASK(ARGS...)> ()> func
// = std::bind(std::forward<TASK>(task),
// std::forward<ARGS>(args)...);
#ifdef TIME_FUNC
auto start = std::chrono::steady_clock::now();
#endif
auto ret = task(std::forward<ARGS>(args)...);
#ifdef TIME_FUNC
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
auto time = std::chrono::duration<double, std::milli>(diff).count();
std::cout << "\n" << fn_name << "\t = "
<< std::setprecision(3) << time << " ms\n";
#endif
return ret;
}
Is there a way to make it work for these type of functions as well? Perhaps using some template tricks?
Aucun commentaire:
Enregistrer un commentaire