optional_relops.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2015, 2024 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_pointees() in generic code instead,
  17. // to obtain the same semantic for pointers.
  18. //
  19. // optional<T> vs optional<T> cases
  20. //
  21. template<class T>
  22. inline
  23. bool operator == ( optional<T> const& x, optional<T> const& y )
  24. { return bool(x) && bool(y) ? *x == *y : bool(x) == bool(y); }
  25. template<class T>
  26. inline
  27. bool operator < ( optional<T> const& x, optional<T> const& y )
  28. { return !y ? false : (!x ? true : (*x) < (*y)); }
  29. template<class T>
  30. inline
  31. bool operator != ( optional<T> const& x, optional<T> const& y )
  32. { return !( x == y ) ; }
  33. template<class T>
  34. inline
  35. bool operator > ( optional<T> const& x, optional<T> const& y )
  36. { return y < x ; }
  37. template<class T>
  38. inline
  39. bool operator <= ( optional<T> const& x, optional<T> const& y )
  40. { return !( y < x ) ; }
  41. template<class T>
  42. inline
  43. bool operator >= ( optional<T> const& x, optional<T> const& y )
  44. { return !( x < y ) ; }
  45. //
  46. // optional<T> vs T cases
  47. //
  48. template<class T>
  49. inline
  50. bool operator == ( optional<T> const& x, T const& y )
  51. { return x && (*x == y); }
  52. template<class T>
  53. inline
  54. bool operator < ( optional<T> const& x, T const& y )
  55. { return (!x) || (*x < y); }
  56. template<class T>
  57. inline
  58. bool operator != ( optional<T> const& x, T const& y )
  59. { return !( x == y ) ; }
  60. template<class T>
  61. inline
  62. bool operator > ( optional<T> const& x, T const& y )
  63. { return y < x ; }
  64. template<class T>
  65. inline
  66. bool operator <= ( optional<T> const& x, T const& y )
  67. { return !( y < x ) ; }
  68. template<class T>
  69. inline
  70. bool operator >= ( optional<T> const& x, T const& y )
  71. { return !( x < y ) ; }
  72. //
  73. // T vs optional<T> cases
  74. //
  75. template<class T>
  76. inline
  77. bool operator == ( T const& x, optional<T> const& y )
  78. { return y && (x == *y); }
  79. template<class T>
  80. inline
  81. bool operator < ( T const& x, optional<T> const& y )
  82. { return y && (x < *y); }
  83. template<class T>
  84. inline
  85. bool operator != ( T const& x, optional<T> const& y )
  86. { return !( x == y ) ; }
  87. template<class T>
  88. inline
  89. bool operator > ( T const& x, optional<T> const& y )
  90. { return y < x ; }
  91. template<class T>
  92. inline
  93. bool operator <= ( T const& x, optional<T> const& y )
  94. { return !( y < x ) ; }
  95. template<class T>
  96. inline
  97. bool operator >= ( T const& x, optional<T> const& y )
  98. { return !( x < y ) ; }
  99. //
  100. // optional<T> vs none cases
  101. //
  102. template<class T>
  103. inline
  104. bool operator == ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
  105. { return !x; }
  106. template<class T>
  107. inline
  108. bool operator < ( optional<T> const&, none_t )
  109. { return false; }
  110. template<class T>
  111. inline
  112. bool operator != ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
  113. { return bool(x); }
  114. template<class T>
  115. inline
  116. bool operator > ( optional<T> const& x, none_t y )
  117. { return y < x ; }
  118. template<class T>
  119. inline
  120. bool operator <= ( optional<T> const& x, none_t y )
  121. { return !( y < x ) ; }
  122. template<class T>
  123. inline
  124. bool operator >= ( optional<T> const& x, none_t y )
  125. { return !( x < y ) ; }
  126. //
  127. // none vs optional<T> cases
  128. //
  129. template<class T>
  130. inline
  131. bool operator == ( none_t , optional<T> const& y ) BOOST_NOEXCEPT
  132. { return !y; }
  133. template<class T>
  134. inline
  135. bool operator < ( none_t , optional<T> const& y )
  136. { return bool(y); }
  137. template<class T>
  138. inline
  139. bool operator != ( none_t, optional<T> const& y ) BOOST_NOEXCEPT
  140. { return bool(y); }
  141. template<class T>
  142. inline
  143. bool operator > ( none_t x, optional<T> const& y )
  144. { return y < x ; }
  145. template<class T>
  146. inline
  147. bool operator <= ( none_t x, optional<T> const& y )
  148. { return !( y < x ) ; }
  149. template<class T>
  150. inline
  151. bool operator >= ( none_t x, optional<T> const& y )
  152. { return !( x < y ) ; }
  153. } // namespace boost
  154. #endif // header guard