I am trying to create a forwarding wrapper function which times the call of a function in c++14. There are 2
types I need to deal with,
one is timing a function which doesn't return a value, and
other not returning
There might be operations which I need to do before and after the call which I am skipping here, so I cannot just do return func()
, which deals with both void
and non-void
types.
Here is the wrapper for the void
function.
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) -> typename enable_if<is_same<decltype(func(args...)), void>::value>::type
{
std::cout << "timing void function" << std::endl;
std::forward<T>(func)(std::forward<U>(args)...);
std::cout << "timing over" << std::endl;
}
Wrapper for the non-void
function
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) -> typename enable_if < !is_same<decltype(func(args...)), void), decltype(func(args...)) > ::value > ::type
{
std::cout << "timing returning function" << std::endl;
auto val = std::forward<T>(func)(std::forward<U>(args)...);
std::cout << "timing over" << std::endl;
return val;
}
int main()
{
time_function(foo, 2);
int i = time_function(&foo_return, 1); //this generates an error
//std::cout<<i<<std::endl;
}
foo
is a function which returns void
, and foo_return
returns an integer.
The error that is generated is
<source > :28 : 129 : error : template argument 1 is invalid
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^
<source>:28 : 55 : error : expected nested - name - specifier before 'enable_if'
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^ ~~~~~~~~
<source>:28 : 129 : error : template argument 1 is invalid
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^
<source>:28 : 129 : error : template argument 1 is invalid
<source> : 28 : 129 : error : template argument 1 is invalid
<source> : 28 : 55 : error : expected initializer before 'enable_if'
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^ ~~~~~~~~
<source>: In function 'int main()' :
<source> : 42 : 41 : error : no matching function for call to 'time_function(int (*)(int), int)'
int i = time_function(&foo_return, 1); //error -
^
<source>:20 : 6 : note : candidate : template<class T, class ... U> typename std::enable_if<std::is_same<decltype (func(time_function::args ...)), void>::value>::type time_function(T&&, U && ...)
auto time_function(T && func, U && ...args) -> typename enable_if<is_same<decltype(func(args...)), void>::value>::type
^ ~~~~~~~~~~~~
<source> : 20 : 6 : note : template argument deduction / substitution failed :
<source> : In substitution of 'template<class T, class ... U> typename std::enable_if<std::is_same<decltype (func(time_function::args ...)), void>::value>::type time_function(T&&, U&& ...) [with T = int (*)(int); U = {int}]' :
<source> : 42 : 41 : required from here
<source> : 20 : 6 : error : no type named 'type' in 'struct std::enable_if<false, void>'
To my knowledge, the wrapper is correct, what is wrong? I am checking if the return type of the function is void
using is_same
, and if so, declaring the return type I wish using enable_if
.