Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function with parameter pack with sizeof … (args) == 0 as base case doesn't compile

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 …));
| ~~^~~~~~~~~~

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading