fork download
  1. #include <iostream>
  2.  
  3. template<typename C, typename T>
  4. struct caster;
  5.  
  6. template<typename C>
  7. struct caster<C, long>
  8. {
  9. static long cast(const C& c) { return c.as_long(); }
  10. };
  11.  
  12. template<typename C>
  13. struct caster<C, double>
  14. {
  15. static double cast(const C& c) { return c.as_double(); }
  16. };
  17.  
  18. template<typename C, typename T>
  19. struct ops_base
  20. {
  21. friend bool operator==(const ops_base& l, const ops_base& r)
  22. {
  23. const C& cl = static_cast<const C&>(l);
  24. const C& cr = static_cast<const C&>(r);
  25. return (caster<C, T>::cast(cl) == caster<C, T>::cast(cr));
  26. }
  27.  
  28. friend bool operator==(const ops_base& l, T r)
  29. {
  30. const C& cl = static_cast<const C&>(l);
  31. return (caster<C, T>::cast(cl) == r);
  32. }
  33.  
  34. friend bool operator==(T l, const ops_base& r)
  35. {
  36. const C& cr = static_cast<const C&>(r);
  37. return (l == caster<C, T>::cast(cr));
  38. }
  39.  
  40. friend bool operator!=(const ops_base& l, const ops_base& r)
  41. { return !(l == r); }
  42.  
  43. friend bool operator!=(const ops_base& l, T r)
  44. { return !(l == r); }
  45.  
  46. friend bool operator!=(T l, const ops_base& r)
  47. { return !(l == r); }
  48.  
  49. friend bool operator<(const ops_base& l, const ops_base& r)
  50. {
  51. const C& cl = static_cast<const C&>(l);
  52. const C& cr = static_cast<const C&>(r);
  53. return (caster<C, T>::cast(cl) < caster<C, T>::cast(cr));
  54. }
  55.  
  56. friend bool operator<(const ops_base& l, T r)
  57. {
  58. const C& cl = static_cast<const C&>(l);
  59. return (caster<C, T>::cast(cl) < r);
  60. }
  61.  
  62. friend bool operator<(T l, const ops_base& r)
  63. {
  64. const C& cr = static_cast<const C&>(r);
  65. return (l < caster<C, T>::cast(cr));
  66. }
  67.  
  68. friend bool operator>(const ops_base& l, const ops_base& r)
  69. {
  70. const C& cl = static_cast<const C&>(l);
  71. const C& cr = static_cast<const C&>(r);
  72. return (caster<C, T>::cast(cl) > caster<C, T>::cast(cr));
  73. }
  74.  
  75. friend bool operator>(const ops_base& l, T r)
  76. {
  77. const C& cl = static_cast<const C&>(l);
  78. return (caster<C, T>::cast(cl) > r);
  79. }
  80.  
  81. friend bool operator>(T l, const ops_base& r)
  82. {
  83. const C& cr = static_cast<const C&>(r);
  84. return (l > caster<C, T>::cast(cr));
  85. }
  86.  
  87. friend bool operator<=(const ops_base& l, const ops_base& r)
  88. { return !(l > r); }
  89.  
  90. friend bool operator<=(const ops_base& l, T r)
  91. { return !(l > r); }
  92.  
  93. friend bool operator<=(T l, const ops_base& r)
  94. { return !(l > r); }
  95.  
  96. friend bool operator>=(const ops_base& l, const ops_base& r)
  97. { return !(l < r); }
  98.  
  99. friend bool operator>=(const ops_base& l, T r)
  100. { return !(l < r); }
  101.  
  102. friend bool operator>=(T l, const ops_base& r)
  103. { return !(l < r); }
  104. };
  105.  
  106. template<typename C, typename T>
  107. struct io_base
  108. {
  109. friend std::ostream& operator<<(std::ostream& out, const io_base& self)
  110. {
  111. const C& c = static_cast<const C&>(self);
  112. return out << caster<C, T>::cast(c);
  113. }
  114. };
  115.  
  116. struct Long : ops_base<Long, long>, io_base<Long, long>
  117. {
  118. Long(long val) : value_(val) { }
  119.  
  120. long as_long() const { return value_; }
  121.  
  122. private:
  123. long value_;
  124. };
  125.  
  126. struct Float : ops_base<Float, double>, io_base<Float, double>
  127. {
  128. Float(double val) : value_(val) { }
  129.  
  130. double as_double() const { return value_; }
  131.  
  132. private:
  133. double value_;
  134. };
  135.  
  136. int main(int argc, char** argv)
  137. {
  138. {
  139. Long l { 5 };
  140. Long r { 6 };
  141.  
  142. std::cout << "[Long, Long]" << std::endl;
  143. std::cout << l << " == " << r << " : " << (l == r) << std::endl;
  144. std::cout << l << " != " << r << " : " << (l != r) << std::endl;
  145. std::cout << l << " < " << r << " : " << (l < r) << std::endl;
  146. std::cout << l << " > " << r << " : " << (l > r) << std::endl;
  147. std::cout << l << " <= " << r << " : " << (l <= r) << std::endl;
  148. std::cout << l << " >= " << r << " : " << (l >= r) << std::endl;
  149. }
  150. {
  151. Long l { 5 };
  152. long r { 6 };
  153.  
  154. std::cout << "[Long, long]" << std::endl;
  155. std::cout << l << " == " << r << " : " << (l == r) << std::endl;
  156. std::cout << l << " != " << r << " : " << (l != r) << std::endl;
  157. std::cout << l << " < " << r << " : " << (l < r) << std::endl;
  158. std::cout << l << " > " << r << " : " << (l > r) << std::endl;
  159. std::cout << l << " <= " << r << " : " << (l <= r) << std::endl;
  160. std::cout << l << " >= " << r << " : " << (l >= r) << std::endl;
  161. }
  162.  
  163. {
  164. Float l { 5 };
  165. Float r { 6 };
  166.  
  167. std::cout << "[Float, Float]" << std::endl;
  168. std::cout << l << " == " << r << " : " << (l == r) << std::endl;
  169. std::cout << l << " != " << r << " : " << (l != r) << std::endl;
  170. std::cout << l << " < " << r << " : " << (l < r) << std::endl;
  171. std::cout << l << " > " << r << " : " << (l > r) << std::endl;
  172. std::cout << l << " <= " << r << " : " << (l <= r) << std::endl;
  173. std::cout << l << " >= " << r << " : " << (l >= r) << std::endl;
  174. }
  175. {
  176. Float l { 5 };
  177. float r { 6 };
  178.  
  179. std::cout << "[Float, float]" << std::endl;
  180. std::cout << l << " == " << r << " : " << (l == r) << std::endl;
  181. std::cout << l << " != " << r << " : " << (l != r) << std::endl;
  182. std::cout << l << " < " << r << " : " << (l < r) << std::endl;
  183. std::cout << l << " > " << r << " : " << (l > r) << std::endl;
  184. std::cout << l << " <= " << r << " : " << (l <= r) << std::endl;
  185. std::cout << l << " >= " << r << " : " << (l >= r) << std::endl;
  186. }
  187. }
  188.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
[Long, Long]
5 == 6 : 0
5 != 6 : 1
5 < 6 : 1
5 > 6 : 0
5 <= 6 : 1
5 >= 6 : 0
[Long, long]
5 == 6 : 0
5 != 6 : 1
5 < 6 : 1
5 > 6 : 0
5 <= 6 : 1
5 >= 6 : 0
[Float, Float]
5 == 6 : 0
5 != 6 : 1
5 < 6 : 1
5 > 6 : 0
5 <= 6 : 1
5 >= 6 : 0
[Float, float]
5 == 6 : 0
5 != 6 : 1
5 < 6 : 1
5 > 6 : 0
5 <= 6 : 1
5 >= 6 : 0