The following program compiles successfully.
template<typename T>
T sum(T x) {
return x;
}
template<typename T, typename... Args>
T sum(T x, Args... args) {
return x + sum(args...);
}
int main() {
sum(1, 2, 3, 4, 5);
}
However, when I switch the order in which the template functions are written, it no longer compiles:
template<typename T, typename... Args>
T sum(T x, Args... args) {
return x + sum(args...);
}
template<typename T>
T sum(T x) {
return x;
}
int main() {
sum(1, 2, 3, 4, 5);
}
The compiler error I get is:
error C2780: 'T sum(T,Args...)': expects 2 arguments - 0 provided, message : see declaration of 'sum'
Why does this happen? Aren't both functions already defined prior to being called in main()? Why does the order in which they're written in matter?