I have written class for measure function run time. and its works fine. However when I start to use it in my project to measure speed of classes method, it breaks with the following error:
error: invalid use of non-static member function
this is my measure function:
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;
}
how can i pass class method to my function, or how can i write function that measure class method speed?
The simplest solution is to just wrap the call to your member function in a lambda and pass that as the function argument to
Time::timer:Alternatively, you can call the member function directly if you add an overload for
Time::timer: