【发布时间】:2021-03-31 18:55:22
【问题描述】:
我已经编写了测量函数运行时间的类。它的工作正常。但是,当我开始在我的项目中使用它来测量类方法的速度时,它会因以下错误而中断:
错误:非静态成员函数的使用无效
这是我的测量函数:
template<typename F, typename... Args>
decltype(auto) Time::timer(F function, Args&&... args){
auto start = std::chrono::steady_clock::now();
auto ret = function(std::forward<Args>(args)...);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
return ret;
}
如何将类方法传递给我的函数,或者如何编写测量类方法速度的函数?
【问题讨论】:
-
将您的成员函数调用包装在 lambda 中
-
当我开始在我的项目中使用它来测量类方法的速度时,它会中断它是如何中断的?
-
drescherjm,错误:非静态成员函数的使用无效
标签: c++ templates methods perfect-forwarding time-measurement