fork download
  1. template <typename T>
  2. T sum(const T& v) {
  3. return v;
  4. }
  5.  
  6. template <typename T1, typename... Ts>
  7. auto sum(const T1& v1, const Ts&... rest) -> decltype( v1 + sum(rest...) ) {
  8. return v1 + sum(rest... );
  9. }
  10.  
  11. #include <iostream>
  12. using std::cout;
  13.  
  14. int main() {
  15. cout << sum(1,2,3,4,5);
  16. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
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
stdout
Standard output is empty