optional_relops.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2015 Andrzej Krzemienski.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // akrzemi1@gmail.com
  12. #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_RELOPS_AJK_03OCT2015_HPP
  13. #define BOOST_OPTIONAL_DETAIL_OPTIONAL_RELOPS_AJK_03OCT2015_HPP
  14. namespace boost {
  15. // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
  16. // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
  17. //
  18. // optional<T> vs optional<T> cases
  19. //
  20. template<class T>
  21. inline
  22. bool operator == ( optional<T> const& x, optional<T> const& y )
  23. { return bool(x) && bool(y) ? *x == *y : bool(x) == bool(y); }
  24. template<class T>
  25. inline
  26. bool operator < ( optional<T> const& x, optional<T> const& y )
  27. { return less_pointees(x,y); }
  28. template<class T>
  29. inline
  30. bool operator != ( optional<T> const& x, optional<T> const& y )
  31. { return !( x == y ) ; }
  32. template<class T>
  33. inline
  34. bool operator > ( optional<T> const& x, optional<T> const& y )
  35. { return y < x ; }
  36. template<class T>
  37. inline
  38. bool operator <= ( optional<T> const& x, optional<T> const& y )
  39. { return !( y < x ) ; }
  40. template<class T>
  41. inline
  42. bool operator >= ( optional<T> const& x, optional<T> const& y )
  43. { return !( x < y ) ; }
  44. //
  45. // optional<T> vs T cases
  46. //
  47. template<class T>
  48. inline
  49. bool operator == ( optional<T> const& x, T const& y )
  50. { return equal_pointees(x, optional<T>(y)); }
  51. template<class T>
  52. inline
  53. bool operator < ( optional<T> const& x, T const& y )
  54. { return less_pointees(x, optional<T>(y)); }
  55. template<class T>
  56. inline
  57. bool operator != ( optional<T> const& x, T const& y )
  58. { return !( x == y ) ; }
  59. template<class T>
  60. inline
  61. bool operator > ( optional<T> const& x, T const& y )
  62. { return y < x ; }
  63. template<class T>
  64. inline
  65. bool operator <= ( optional<T> const& x, T const& y )
  66. { return !( y < x ) ; }
  67. template<class T>
  68. inline
  69. bool operator >= ( optional<T> const& x, T const& y )
  70. { return !( x < y ) ; }
  71. //
  72. // T vs optional<T> cases
  73. //
  74. template<class T>
  75. inline
  76. bool operator == ( T const& x, optional<T> const& y )
  77. { return equal_pointees( optional<T>(x), y ); }
  78. template<class T>
  79. inline
  80. bool operator < ( T const& x, optional<T> const& y )
  81. { return less_pointees( optional<T>(x), y ); }
  82. template<class T>
  83. inline
  84. bool operator != ( T const& x, optional<T> const& y )
  85. { return !( x == y ) ; }
  86. template<class T>
  87. inline
  88. bool operator > ( T const& x, optional<T> const& y )
  89. { return y < x ; }
  90. template<class T>
  91. inline
  92. bool operator <= ( T const& x, optional<T> const& y )
  93. { return !( y < x ) ; }
  94. template<class T>
  95. inline
  96. bool operator >= ( T const& x, optional<T> const& y )
  97. { return !( x < y ) ; }
  98. //
  99. // optional<T> vs none cases
  100. //
  101. template<class T>
  102. inline
  103. bool operator == ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
  104. { return !x; }
  105. template<class T>
  106. inline
  107. bool operator < ( optional<T> const& x, none_t )
  108. { return less_pointees(x,optional<T>() ); }
  109. template<class T>
  110. inline
  111. bool operator != ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
  112. { return bool(x); }
  113. template<class T>
  114. inline
  115. bool operator > ( optional<T> const& x, none_t y )
  116. { return y < x ; }
  117. template<class T>
  118. inline
  119. bool operator <= ( optional<T> const& x, none_t y )
  120. { return !( y < x ) ; }
  121. template<class T>
  122. inline
  123. bool operator >= ( optional<T> const& x, none_t y )
  124. { return !( x < y ) ; }
  125. //
  126. // none vs optional<T> cases
  127. //
  128. template<class T>
  129. inline
  130. bool operator == ( none_t , optional<T> const& y ) BOOST_NOEXCEPT
  131. { return !y; }
  132. template<class T>
  133. inline
  134. bool operator < ( none_t , optional<T> const& y )
  135. { return less_pointees(optional<T>() ,y); }
  136. template<class T>
  137. inline
  138. bool operator != ( none_t, optional<T> const& y ) BOOST_NOEXCEPT
  139. { return bool(y); }
  140. template<class T>
  141. inline
  142. bool operator > ( none_t x, optional<T> const& y )
  143. { return y < x ; }
  144. template<class T>
  145. inline
  146. bool operator <= ( none_t x, optional<T> const& y )
  147. { return !( y < x ) ; }
  148. template<class T>
  149. inline
  150. bool operator >= ( none_t x, optional<T> const& y )
  151. { return !( x < y ) ; }
  152. } // namespace boost
  153. #endif // header guard