Advertisement
cwchen

Varargs with template

Jan 6th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. template<typename T>
  7. T max(T num) {
  8.   return num;
  9. }
  10.  
  11. template<typename T>
  12. T max(T num1, T num2) {
  13.   return (num1 > num2) ? num1 : num2;
  14. }
  15.  
  16. template<typename T, typename... Args>
  17. T max(T num1, T num2, Args... args) {
  18.   T temp = max(num1, num2);
  19.   return max(temp, args...);
  20. }
  21.  
  22. int main() {
  23.   double m = max(3, 4, 5, 1, 2);
  24.   cout << m << endl;
  25.   return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement