template <typename T>
T sum(const T& v) {
return v;
}
template <typename T1, typename... Ts>
auto sum(const T1& v1, const Ts&... rest) -> decltype( v1 + sum(rest...) ) {
return v1 + sum(rest... );
}
#include <iostream>
using std::cout;
int main() {
cout << sum(1,2,3,4,5);
}
dGVtcGxhdGUgPHR5cGVuYW1lIFQ+ClQgc3VtKGNvbnN0IFQmIHYpIHsKcmV0dXJuIHY7Cn0KCnRlbXBsYXRlIDx0eXBlbmFtZSBUMSwgdHlwZW5hbWUuLi4gVHM+CmF1dG8gc3VtKGNvbnN0IFQxJiB2MSwgY29uc3QgVHMmLi4uIHJlc3QpIC0+IGRlY2x0eXBlKCB2MSArIHN1bShyZXN0Li4uKSApIHsKcmV0dXJuIHYxICsgc3VtKHJlc3QuLi4gKTsKfQoKI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBzdGQ6OmNvdXQ7CgppbnQgbWFpbigpIHsKICAgIGNvdXQgPDwgc3VtKDEsMiwzLDQsNSk7ICAgIAp9
prog.cpp: In function ‘int main()’:
prog.cpp:15:26: error: no matching function for call to ‘sum(int, int, int, int, int)’
prog.cpp:15:26: note: candidates are:
prog.cpp:2:3: note: template<class T> T sum(const T&)
prog.cpp:2:3: note: template argument deduction/substitution failed:
prog.cpp:15:26: note: candidate expects 1 argument, 5 provided
prog.cpp:7:6: note: template<class T1, class ... Ts> decltype ((v1 + sum(sum::rest ...))) sum(const T1&, const Ts& ...)
prog.cpp:7:6: note: template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class T1, class ... Ts> decltype ((v1 + sum(rest ...))) sum(const T1&, const Ts& ...) [with T1 = int; Ts = {int, int, int, int}]’:
prog.cpp:15:26: required from here
prog.cpp:7:6: error: no matching function for call to ‘sum(const int&, const int&, const int&, const int&)’
prog.cpp:7:6: note: candidate is:
prog.cpp:2:3: note: template<class T> T sum(const T&)
prog.cpp:2:3: note: template argument deduction/substitution failed:
prog.cpp:7:6: note: candidate expects 1 argument, 4 provided