I'm trying to write a function that measures the time of execution of other functions.
It should have the same return type as the measured function.
The problem is that i'm getting a compiler error Variable has incomplete type 'void'
when the return type is void
.
Is there a workaround to solve this problem?
Help would be greatly appreciated, thanks!
#include <iostream>
#include <chrono>
template<class Func, typename... Parameters>
auto getTime(Func const &func, Parameters &&... args) {
auto begin = std::chrono::system_clock::now();
auto ret = func(std::forward<Parameters>(args)...);
auto end = std::chrono::system_clock::now();
std::cout << "The execution took " << std::chrono::duration<float>(end - begin).count() << " seconds.";
return ret;
}
int a() { return 0; }
void b() {}
int main()
{
getTime(a);
getTime(b);
return 0;
}
void
.void
means the fn has no return type. If you're writing something that needs a return value then a fn with no return value won't work. – Sturdivantreturn func(...);
+ RAII. – Mellisavoid
means no value or nothing - how would you assign anything to "nothing"?? – Highwaymanreturn void();
is a thing. And forvoid foo();
you canreturn foo();
for a return type ofvoid
. – Frydman