Advertisement
zamotivator

Untitled

Jul 6th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<typename Owner>
  4. class Comparable;
  5.  
  6. template<typename Owner>
  7. bool operator<(Comparable<Owner> const& left, Comparable<Owner> const& right)
  8. {
  9.     return left.owner().data() < right.owner().data();
  10. }
  11.  
  12. template<typename Owner>
  13. bool operator>(Comparable<Owner> const& left, Comparable<Owner> const& right)
  14. {
  15.     return left.owner().data() > right.owner().data();
  16. }
  17.  
  18. template<typename Owner>
  19. bool operator<=(Comparable<Owner> const& left, Comparable<Owner> const& right)
  20. {
  21.     return left.owner().data() <= right.owner().data();
  22. }
  23.  
  24. template<typename Owner>
  25. std::ostream& operator<<(std::ostream& s, Comparable<Owner> const& c)
  26. {
  27.     s << c.owner().name() << "(" << c.owner().data() << ")";
  28.     return s;
  29. }
  30.  
  31. template<typename Owner>
  32. class Comparable
  33. {
  34. public:
  35.     Owner const& owner() const
  36.     {
  37.         return *static_cast<Owner const * const>(this);
  38.     }
  39. };
  40.  
  41.  
  42. class Timestamp : public Comparable<Timestamp>
  43. {
  44.   public:
  45.     unsigned int _timestamp;
  46.  
  47.     unsigned int const& data() const
  48.     {
  49.         return _timestamp;
  50.     }
  51.  
  52.     const char* name() const
  53.     {
  54.         return "Timestamp";
  55.     }
  56. };
  57.  
  58. class Interval : public Comparable<Interval>
  59. {
  60.   public:
  61.     unsigned int _interval;
  62.  
  63.     unsigned int const& data() const
  64.     {
  65.         return _interval;
  66.     }
  67.  
  68.     const char* name() const
  69.     {
  70.         return "Interval";
  71.     }
  72. };
  73.  
  74. /*
  75.  
  76. oleg (0) ~$ g++ test.cpp && ./a.out
  77. Timestamp(5)<Timestamp(6) is true
  78. Timestamp(5)>Timestamp(6) is false
  79. Timestamp(5)<=Timestamp(6) is true
  80.  
  81. */
  82.  
  83. int main(int, char*[])
  84. {
  85.     Timestamp lt, rt;
  86.     Interval li, ri;
  87.     lt._timestamp = 5;
  88.     rt._timestamp = 6;
  89.     li._interval = 7;
  90.     ri._interval = 8;
  91.  
  92.     std::cout << lt << "<" << rt << " is " << (lt < rt ? "true" : "false") << std::endl;
  93.     std::cout << lt << ">" << rt << " is " << (lt > rt ? "true" : "false") << std::endl;
  94.     std::cout << lt << "<=" << rt << " is " << (lt <= rt ? "true" : "false") << std::endl;
  95.  
  96.     /*
  97.       std::cout << lt << " < " << ri << " is incomparable " << (lt < ri ? "true" : "false") << std::endl;
  98.  
  99.       Error of compiler:
  100.  
  101. oleg (0) ~$ g++ test.cpp
  102. test.cpp: In function ‘int main(int, char**)’:
  103. test.cpp:87:68: error: no match for ‘operator<’ in ‘lt < ri’
  104. test.cpp:87:68: note: candidate is:
  105. test.cpp:7:6: note: template<class Owner> bool operator<(const Comparable<Owner>&, const Comparable<Owner>&)
  106. test.cpp:7:6: note:   template argument deduction/substitution failed:
  107. test.cpp:87:68: note:   deduced conflicting types for parameter ‘Owner’ (‘Timestamp’ and ‘Interval’)
  108.     */
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement