Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a variadic template recursive function?

I'm trying to write a variadic template constexpr function which calculates sum of the template parameters given. Here's my code:

template<int First, int... Rest>
constexpr int f()
{
    return First + f<Rest...>();
}

template<int First>
constexpr int f()
{
    return First;
}

int main()
{
    f<1, 2, 3>();
    return 0;
}

Unfortunately, it does not compile reporting an error message error C2668: 'f': ambiguous call to overloaded function while trying to resolve f<3,>() call.

I also tried to change my recursion base case to accept 0 template arguments instead of 1:

template<>
constexpr int f()
{
    return 0;
}

But this code also does not compile (message error C2912: explicit specialization 'int f(void)' is not a specialization of a function template).

I could extract first and second template arguments to make this compile and work, like this:

template<int First, int Second, int... Rest>
constexpr int f()
{
    return First + f<Second, Rest...>();
}

But this does not seem to be the best option. So, the question is: how to write this calculation in an elegant way?

UP: I also tried to write this as a single function:

template<int First, int... Rest>
constexpr int f()
{
    return sizeof...(Rest) == 0 ? First : (First + f<Rest...>());
}

And this also does not work: error C2672: 'f': no matching overloaded function found.

like image 844
alexeykuzmin0 Avatar asked Aug 04 '16 19:08

alexeykuzmin0


2 Answers

template<int First, int... Rest>
constexpr int f()
{
    return First + f<Rest...>();
}

template<int First>
constexpr int f()
{
    return First;
}

int main()
{
    f<1, 2, 3>();
    return 0;
}

You get this error:

error C2668: 'f': ambiguous call to overloaded function while trying to resolve f<3,>() call.

This is because a variadic parameter pack can be given 0 arguments, so f<3> could work with template<int First, int... Rest> by "expanding" to template<3, >. However, you also have the specialization of template<int First>, so the compiler does not know which one to choose.

Explicitly stating the first and second template arguments is a completely valid and good solution to this problem.


When you try to change the base case to:

template <>
constexpr int f()
{
    return 0;
}

You have a problem because functions cannot be specialized in that way. Classes and structs can be, but not functions.


Solution #1: C++17 fold expression with constexpr

template <typename... Is>
constexpr int sum(Is... values) {
    return (0 + ... + values);
}

Solution #2: Use a constexpr function

constexpr int sum() {
    return 0;
}

template <typename I, typename... Is>
constexpr int sum(I first, Is... rest) {
    return first + sum(rest...);
}

Solution #3: Use Template Metaprogramming

template <int... Is>
struct sum;

template <>
struct sum<>
    : std::integral_constant<int, 0>
{};

template <int First, int... Rest>
struct sum<First, Rest...>
    : std::integral_constant<int,
        First + sum_impl<Rest...>::value>
{};
like image 21
Justin Avatar answered Sep 23 '22 06:09

Justin


Your base case was wrong. You need a case for the empty list, but as the compiler suggests, your second try was not a valid template specialization. One way to define a valid instantiation for zero arguments is to create an overload that accepts an empty list

template<class none = void>
constexpr int f()
{
    return 0;
}
template<int First, int... Rest>
constexpr int f()
{
    return First + f<Rest...>();
}
int main()
{
    f<1, 2, 3>();
    return 0;
}

EDIT: for completeness sake also my first answer, that @alexeykuzmin0 fixed by adding the conditional:

template<int First=0, int... Rest>
constexpr int f()
{
    return sizeof...(Rest)==0 ? First : First + f<Rest...>();
}
like image 103
example Avatar answered Sep 20 '22 06:09

example