1
我实现一个简单的曲线函数的返回类型:获得一个回调函数
template <typename T>
typename T::return_type ProfilerRun(T&& func, const std::string& routine_name = "unknown")
{
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::steady_clock;
using std::cerr;
using std::endl;
#ifdef SELF_PROFILING
steady_clock::time_point t_begin = steady_clock::now();
#endif
func();
#ifdef SELF_PROFILING
steady_clock::time_point t_end = steady_clock::now();
cerr << "Function " << routine_name << " duration: " <<
duration_cast<microseconds>(t_end - t_begin).count() <<
" microseconds." << endl;
#endif
}
这与std::bind(&Class::function, class_object, param1, param2, ...)
完美的作品,但它不会与原始函数指针工作,因为他们没有T::result_type
财产。我也想过
auto ProfilerRun(T&& func, const std::string&) -> decltype(T()))
但在这种情况下decltype
将调用T
的构造函数时,函数对象被传递。有没有可能的策略来解决这个问题?
'decltype(func())'会做,顺便说一句。 –
@Fernades好点,我没有注意到这一点。 – xis