fork(1) download
  1. /*
  2. Write and test a variadic template function sum_values() that accepts an arbitrarily
  3. long list of arguments with numeric values (they can be a mixture of types) and
  4. returns the sum as a long double value.
  5. */
  6.  
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. template<typename T>
  11. long double sum_values(T sum) {
  12. return sum;
  13. }
  14. template<typename T, typename TT, typename... TTT>
  15. long double sum_values(T a, TT b, TTT... args) {
  16. return sum_values(a + b, args...);
  17. }
  18.  
  19. int main() {
  20. cout << sum_values(5) << " == 5? " << endl;
  21. cout << sum_values(5, 6) << " == 11? " << endl;
  22. cout << sum_values(5, 6, 7) << " == 18? " << endl;
  23. cout << sum_values(5, 6, 7, 8) << " == 26? " << endl;
  24. cout << sum_values(5, 2.3, 2.63, 'a') << " == 106.93? " << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
5 == 5? 
11 == 11? 
18 == 18? 
26 == 26? 
106.93 == 106.93?