Here's the code of my function:
#include <iostream>
#include <type_traits>
#include <algorithm>
template <typename Head, typename ... Args>
std::common_type_t<Head, Args...> mx(Head n, Args ... args)
{
if (sizeof ... (args) == 0)
return n;
else
return std::max(n, mx(args ...));
}
int main()
{
std::cout << mx(3, 4, 5);
}
I got compile errors:
main.cpp: In instantiation of 'std::common_type_t<Head, Args ...> mx(Head, Args ...) [with Head = int; Args = {}; std::common_type_t<Head, Args ...> = int]': main.cpp:11:24:
recursively required from 'std::common_type_t<Head, Args ...> mx(Head, Args ...) [with Head = int; Args = {int}; std::common_type_t<Head, Args ...> = int]' main.cpp:11:24: required from 'std::common_type_t<Head, Args ...> mx(Head, Args ...) [with Head = int; Args = {int, int}; std::common_type_t<Head, Args ...> = int]' main.cpp:16:25: required from here main.cpp:11:24: error: no matching function for call to 'mx()' 11 | return std::max(n, mx(args ...)); | ~~^~~~~~~~~~ main.cpp:6:35: note: candidate: 'template<class Head, class ... Args> std::common_type_t<Head, Args ...> mx(Head, Args ...)' 6 | std::common_type_t<Head, Args...> mx(Head n, Args ... args) | ^~ main.cpp:6:35: note: template argument deduction/substitution failed: main.cpp:11:24: note: candidate expects at least 1 argument, 0 provided 11 | return std::max(n, mx(args ...)); | ~~^~~~~~~~~~
Of course I can write this more properly, like this:
template <typename Head>
std::common_type_t<Head> mx(Head n)
{
return n;
}
template <typename Head, typename ... Args>
std::common_type_t<Head, Args...> mx(Head n, Args ... args)
{
return std::max(n, mx(args ...));
}
But still, I don't understand why my first option doesn't work. Judging by errors, it somehow tries to call recursive version of function even if there's no arguments in parameter pack. But that does not make any sense to me since I considered this case. What's the problem and can I fix it?
Even if
if (sizeof ... (args) == 0)
the entire function must be well-formed C++.
return std::max(n, mx(args ...));
This still must be valid C++, even if won't get executed. If, outside of template context, you have an if (1)
, the else
part must still be valid C++, you can't just throw randomly-generated gibberish in there, and this is the same thing. And when sizeof...(args)
is 0 the function call becomes mx()
and that, of course, has no valid overload.
What you want to do, instead, is use if constexpr
instead of your garden-variety if
.
if constexpr (sizeof ... (args) == 0)