C++ variadic template function ERROR C2672 'no matching overloaded function found'

85 Views Asked by At

I am currently trying to familiarize myself with variadic templates in c++, but I've kinda run into a wall of some sort, where every recursive implementation gives me error C2672. Even examples I've looked up in videos online and that I've seen work do notfor me for some reason.

#include <iostream>

template<typename T, typename... Args>
T sum(T t, Args... args)
{
    return t + sum( args...);
}

template<typename T>
T sum(T t1, T t2)
{
    return t1 + t2;
}

int main()
{
    std::cout << sum(1, 2, 3, 4, 5) << std::endl;
}

Severity Code Description Project File Line Suppression State Message could be 'T sum(T,Args...)' Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 4

Error C2672 'sum': no matching overloaded function found Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message 'T sum(T,Args...)': expects 2 arguments - 0 provided Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message see reference to function template instantiation 'T sum<int,>(T)' being compiled Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message see reference to function template instantiation 'T sum<int,int>(T,int)' being compiled Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message see reference to function template instantiation 'T sum<int,int,int>(T,int,int)' being compiled Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message see reference to function template instantiation 'T sum<int,int,int,int>(T,int,int,int)' being compiled Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 6

Message see reference to function template instantiation 'T sum<int,int,int,int,int>(T,int,int,int,int)' being compiled Exercise Variadics D:*\Desktop\CPP Dev*\Exercise Variadics\Exercise Variadics\Main.cpp 17

I've made sure that I am using C++20. I tried the same code on online compilers with the same result.

1

There are 1 best solutions below

4
On

The compiler never considers the second overload sum(T, T) because that overload isn't visible at the point of sum(args...) call.

return t + sum( args...);

That line asks the compiler to invoke some function with unqualified name sum. Hence the compiler has to perform a name lookup to find which function to call here.

Non-ADL lookup will consider only those declarations of sum() which are visible at that point of call. The only declaration of sum() visible to the compiler at that point is that recursive one. But that declaration requires at least one argument. So once the compiler gets down to zero arguments: t + sum(), that declaration no longer matches the call, which is exactly what those error messages are telling:

'sum': no matching overloaded function found see reference to function template instantiation 'T sum<int,>(T)' being compiled

So basically you should put that terminating overload up front:

#include <iostream>

template <typename T>
T sum(T t)
{
    return t;
}

template <typename T, typename... Args>
T sum(T t, Args... args)
{
    return t + sum(args...);
}

int main()
{
    std::cout << sum(1, 2, 3, 4, 5) << std::endl;
}