fork download
  1. #include <type_traits>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. template <typename T, typename = void>
  6. struct Comparator
  7. {
  8. int operator()(const T& a, const T& b) const
  9. {
  10. std::cout << "catch all\n";
  11. if (a < b)
  12. {
  13. return -1;
  14. }
  15. if (a > b)
  16. {
  17. return 1;
  18. }
  19. return 0;
  20. }
  21. };
  22.  
  23. template <typename T>
  24. struct Comparator<T, std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value>>
  25. {
  26. int operator()(const T &a, const T &b) const
  27. {
  28. std::cout << "Signed arithmetic type\n";
  29. return a - b;
  30. }
  31. };
  32.  
  33. template<typename T>
  34. int comparator(const T& a, const T& b)
  35. {
  36. return Comparator<T>()(a, b);
  37. }
  38.  
  39. int main()
  40. {
  41. comparator(42, 0);
  42. comparator('c', 'g');
  43. comparator(42u, 0u);
  44. comparator(42LL, 0LL);
  45. comparator(std::string("b"), std::string("a"));
  46. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Signed arithmetic type
Signed arithmetic type
catch all
Signed arithmetic type
catch all