lundi 9 octobre 2017

Ambiguous call to variadic template function with no parameters?

When running this:

template <typename T>
struct CodeByType
{
    static const int32_t Value = 7;
};

template <>
struct CodeByType<int>
{
    static const int32_t Value = 1;
};

template <typename Arg, typename... Args>
int32_t Sum()
{
    // The compiler complains on this line
    return Sum<Arg>() + Sum<Args...>();
}

template <typename Arg>
int32_t Sum()
{
    return CodeByType<Arg>::Value;
}

int main()
{
    auto sum = Sum<int, char, double>();
}

I'm getting:

Error C2668 'Sum': ambiguous call to overloaded function

Can someone please explain why and how to overcome it?

This looks awfully similar to the below code, which does compile, so I suppose it has something to do with Sum not accepting any actual parameters.

template <typename T>
T adder(T first) {
    return first;
}

template<typename T, typename... Args>
T adder(T first, Args... rest) {
    return first + adder(rest...);
}

int main()
{
    auto sum = adder(1, 7);
}

Aucun commentaire:

Enregistrer un commentaire