I want to write a function template run that calls a passed callable which takes no parameters and prints its return value if it is not void. Here is my attempt:
template<typename FUNC>
void run(FUNC &&f)
{
if constexpr (std::is_void_v<decltype(f())>)
f();
else
std::cout << f() << std::endl;
}
Now here is the problem: I would like run to accept functions that do take parameters which all have a default value, i.e.:
int foo()
{ return 0; }
int bar(int a = 1, int b = 2)
{ return a + b; }
template<typename FUNC>
void run(FUNC &&f)
{
if constexpr (std::is_void_v<decltype(f())>)
f();
else
std::cout << f() << std::endl;
}
int main()
{
run(foo);
run(bar); // compiler error, too few arguments to f()
}
This does not work yet since default parameters are not part of a functions signature and so I can't call f() inside run. How can I fix this?