fork download
  1. namespace interval
  2. {
  3.  
  4. namespace detail
  5. {
  6.  
  7. template < typename T >
  8. struct result_
  9. {
  10. protected:
  11. const bool value;
  12. const T &item_;
  13.  
  14. result_( bool p, const T &itm )
  15. : value( p ), item_( itm ) {}
  16.  
  17. explicit
  18. operator bool( void ) const
  19. { return this->value; }
  20. };
  21.  
  22. template < typename T >
  23. struct is_lt_or_leq
  24. : private result_< T >
  25. {
  26. template < typename U >
  27. bool
  28. friend operator>( is_lt_or_leq< U > &&, const U & );
  29. template < typename U >
  30. bool
  31. friend operator>=( is_lt_or_leq< U > &&, const U & );
  32.  
  33. is_lt_or_leq( bool p, const T &itm )
  34. : result_< T >( p, itm ) {}
  35.  
  36. using result_< T >::operator bool;
  37. };
  38.  
  39. template < typename T >
  40. struct is_gt_or_geq
  41. : private result_< T >
  42. {
  43. template < typename U >
  44. bool
  45. friend operator<( is_gt_or_geq< U > &&, const U & );
  46. template < typename U >
  47. bool
  48. friend operator<=( is_gt_or_geq< U > &&, const U & );
  49.  
  50. is_gt_or_geq( bool p, const T &itm )
  51. : result_< T >( p, itm ) {}
  52.  
  53. using result_< T >::operator bool;
  54. };
  55.  
  56. template < typename T >
  57. struct interval_item
  58. {
  59. template < typename U >
  60. bool
  61. friend operator>( interval_item< U > &&, const U & );
  62. template < typename U >
  63. is_lt_or_leq< U >
  64. friend operator>( const U &, interval_item< U > && );
  65.  
  66. template < typename U >
  67. bool
  68. friend operator>=( interval_item< U > &&, const U & );
  69. template < typename U >
  70. is_lt_or_leq< U >
  71. friend operator>=( const U &, interval_item< U > && );
  72.  
  73. template < typename U >
  74. bool
  75. friend operator<( interval_item< U > &&, const U & );
  76. template < typename U >
  77. is_gt_or_geq< U >
  78. friend operator<( const U &, interval_item< U > && );
  79.  
  80. template < typename U >
  81. bool
  82. friend operator<=( interval_item< U > &&, const U & );
  83. template < typename U >
  84. is_gt_or_geq< U >
  85. friend operator<=( const U &, interval_item< U > && );
  86.  
  87. private:
  88. const T &item_;
  89.  
  90. public:
  91. interval_item( const T &itm )
  92. : item_( itm ) {}
  93.  
  94. interval_item( const interval_item & );
  95. interval_item( interval_item && );
  96.  
  97. interval_item &
  98. operator=( const interval_item & );
  99. interval_item &
  100. operator=( interval_item && );
  101. };
  102.  
  103. template < typename T >
  104. interval_item< T >
  105. item( const T &itm )
  106. { return { itm }; }
  107.  
  108. template < typename T >
  109. bool
  110. operator>( interval_item< T > &&item, const T &x )
  111. { return item.item_ > x; }
  112.  
  113. template < typename T >
  114. is_lt_or_leq< T >
  115. operator>( const T &x, interval_item< T > &&item )
  116. { return { x > item.item_, item.item_ }; }
  117.  
  118. template < typename T >
  119. bool
  120. operator>( is_lt_or_leq< T > &&prev, const T &x )
  121. { return prev.value && prev.item_ > x; }
  122.  
  123.  
  124. template < typename T >
  125. bool
  126. operator>=( interval_item< T > &&item, const T &x )
  127. { return item.item_ >= x; }
  128.  
  129. template < typename T >
  130. is_lt_or_leq< T >
  131. operator>=( const T &x, interval_item< T > &&item )
  132. { return { x >= item.item_, item.item_ }; }
  133.  
  134. template < typename T >
  135. bool
  136. operator>=( is_lt_or_leq< T > &&prev, const T &x )
  137. { return prev.value && prev.item_ >= x; }
  138.  
  139.  
  140. template < typename T >
  141. bool
  142. operator<( interval_item< T > &&item, const T &x )
  143. { return item.item_ < x; }
  144.  
  145. template < typename T >
  146. is_gt_or_geq< T >
  147. operator<( const T &x, interval_item< T > &&item )
  148. { return { x < item.item_, item.item_ }; }
  149.  
  150. template < typename T >
  151. bool
  152. operator<( is_gt_or_geq< T > &&prev, const T &x )
  153. { return prev.value && prev.item_ < x; }
  154.  
  155.  
  156. template < typename T >
  157. bool
  158. operator<=( interval_item< T > &&item, const T &x )
  159. { return item.item_ <= x; }
  160.  
  161. template < typename T >
  162. is_gt_or_geq< T >
  163. operator<=( const T &x, interval_item< T > &&item )
  164. { return { x <= item.item_, item.item_ }; }
  165.  
  166. template < typename T >
  167. bool
  168. operator<=( is_gt_or_geq< T > &&prev, const T &x )
  169. { return prev.value && prev.item_ <= x; }
  170.  
  171. } // namespace detail
  172.  
  173. using ::interval::detail::item;
  174.  
  175. } // namespace interval
  176.  
  177. #include <iostream>
  178. using namespace std;
  179.  
  180. int
  181. main( void )
  182. {
  183. cout << boolalpha
  184. << static_cast< bool >( -1 < interval::item( 0 ) < 1 )
  185. << endl;
  186. }
  187.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
true