Advertisement
avr39ripe

cppTemplateOverloadingLecture

Apr 21st, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //int max(int a, int b)
  4. //{
  5. //  return a > b ? a : b;
  6. //}
  7.  
  8. template <typename Type>
  9. Type max(Type a, Type b)
  10. {
  11.     return a > b ? a : b;
  12. }
  13.  
  14. //template <typename T1, typename T2>
  15. //T1 max(T1 a, T2 b)
  16. //{
  17. //  return a > b ? a : b;
  18. //}
  19.  
  20. template <typename T1, typename T2>
  21. auto max(T1 a, T2 b) -> decltype(a > b)
  22. {
  23.     return a > b ? a : b;
  24. }
  25.  
  26. //bool nice(int a) { return a < 6; }
  27. auto nice(int a) -> bool { return a > 6; }
  28. //int max(int, double)
  29. //double max(double, int)
  30.  
  31.  
  32. int main()
  33. {
  34.     //auto answer{ nice(8) + 'z' };
  35.     //auto x{2.5};
  36.  
  37.     //answer = 3.14;
  38.     //answer = 'q';
  39.  
  40.     //int x{ 0 };
  41.     //decltype(x++ + 6) val;
  42.  
  43.     ////val = 3.5;
  44.  
  45.     //auto flag{ 10 > 5 };
  46.  
  47.     //std::cout << answer << '\n';
  48.  
  49.     //std::cout << max(2, 4) << '\n';
  50.     //std::cout << max(4, 2) << '\n';
  51.  
  52.     //std::cout << max('a', 'q') << '\n';
  53.     //std::cout << max('q', 'a') << '\n';
  54.  
  55.     //std::cout << max(2.1, 2.25) << '\n';
  56.     //std::cout << max(2.25, 2.1) << '\n';
  57.     //std::cout << max<int>(115, 'q') << '\n';
  58.     //
  59.     //
  60.     //std::cout << max(115, 'q') << '\n';
  61.     //std::cout << max('q', 115) << '\n';
  62.  
  63.     std::cout << max(2, 2.25) << '\n';
  64.     std::cout << max(2.25, 2) << '\n';
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement