has_binary_operator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // (C) Copyright 2009-2011 Frederic Bron, Robert Stewart, Steven Watanabe & Roman Perepelitsa.
  2. //
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt).
  6. //
  7. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  8. #include <boost/config.hpp>
  9. #include <boost/type_traits/detail/config.hpp>
  10. // cannot include this header without getting warnings of the kind:
  11. // gcc:
  12. // warning: value computed is not used
  13. // warning: comparison between signed and unsigned integer expressions
  14. // msvc:
  15. // warning C4018: '<' : signed/unsigned mismatch
  16. // warning C4244: '+=' : conversion from 'double' to 'char', possible loss of data
  17. // warning C4547: '*' : operator before comma has no effect; expected operator with side-effect
  18. // warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
  19. // warning C4804: '<' : unsafe use of type 'bool' in operation
  20. // warning C4805: '==' : unsafe mix of type 'bool' and type 'char' in operation
  21. // cannot find another implementation -> declared as system header to suppress these warnings.
  22. #if defined(__GNUC__)
  23. # pragma GCC system_header
  24. #elif defined(BOOST_MSVC)
  25. # pragma warning ( push )
  26. # pragma warning ( disable : 4018 4244 4547 4800 4804 4805 4913 4133)
  27. # if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
  28. # pragma warning ( disable : 6334)
  29. # endif
  30. #endif
  31. #if defined(BOOST_TT_HAS_ACCURATE_BINARY_OPERATOR_DETECTION)
  32. #include <boost/type_traits/integral_constant.hpp>
  33. #include <boost/type_traits/make_void.hpp>
  34. #include <boost/type_traits/is_convertible.hpp>
  35. #include <boost/type_traits/is_void.hpp>
  36. #include <boost/type_traits/add_reference.hpp>
  37. #include <utility>
  38. namespace boost
  39. {
  40. namespace binary_op_detail {
  41. struct dont_care;
  42. template <class T, class U, class Ret, class = void>
  43. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _ret_imp) : public boost::false_type {};
  44. template <class T, class U, class Ret>
  45. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _ret_imp)<T, U, Ret, typename boost::make_void<decltype(std::declval<typename add_reference<T>::type>() BOOST_TT_TRAIT_OP std::declval<typename add_reference<U>::type>())>::type>
  46. : public boost::integral_constant<bool, ::boost::is_convertible<decltype(std::declval<typename add_reference<T>::type>() BOOST_TT_TRAIT_OP std::declval<typename add_reference<U>::type>()), Ret>::value> {};
  47. template <class T, class U, class = void >
  48. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _void_imp) : public boost::false_type {};
  49. template <class T, class U>
  50. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _void_imp)<T, U, typename boost::make_void<decltype(std::declval<typename add_reference<T>::type>() BOOST_TT_TRAIT_OP std::declval<typename add_reference<U>::type>())>::type>
  51. : public boost::integral_constant<bool, ::boost::is_void<decltype(std::declval<typename add_reference<T>::type>() BOOST_TT_TRAIT_OP std::declval<typename add_reference<U>::type>())>::value> {};
  52. template <class T, class U, class = void>
  53. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _dc_imp) : public boost::false_type {};
  54. template <class T, class U>
  55. struct BOOST_JOIN(BOOST_TT_TRAIT_NAME, _dc_imp)<T, U, typename boost::make_void<decltype(std::declval<typename add_reference<T>::type>() BOOST_TT_TRAIT_OP std::declval<typename add_reference<U>::type>())>::type>
  56. : public boost::true_type {};
  57. }
  58. template <class T, class U = T, class Ret = boost::binary_op_detail::dont_care>
  59. struct BOOST_TT_TRAIT_NAME : public boost::binary_op_detail:: BOOST_JOIN(BOOST_TT_TRAIT_NAME, _ret_imp) <T, U, Ret> {};
  60. template <class T, class U>
  61. struct BOOST_TT_TRAIT_NAME<T, U, void> : public boost::binary_op_detail:: BOOST_JOIN(BOOST_TT_TRAIT_NAME, _void_imp) <T, U> {};
  62. template <class T, class U>
  63. struct BOOST_TT_TRAIT_NAME<T, U, boost::binary_op_detail::dont_care> : public boost::binary_op_detail:: BOOST_JOIN(BOOST_TT_TRAIT_NAME, _dc_imp) <T, U> {};
  64. }
  65. #else
  66. #include <boost/type_traits/detail/yes_no_type.hpp>
  67. #include <boost/type_traits/integral_constant.hpp>
  68. #include <boost/type_traits/is_base_of.hpp>
  69. #include <boost/type_traits/is_const.hpp>
  70. #include <boost/type_traits/is_convertible.hpp>
  71. #include <boost/type_traits/is_fundamental.hpp>
  72. #include <boost/type_traits/is_integral.hpp>
  73. #include <boost/type_traits/is_pointer.hpp>
  74. #include <boost/type_traits/is_same.hpp>
  75. #include <boost/type_traits/is_void.hpp>
  76. #include <boost/type_traits/remove_cv.hpp>
  77. #include <boost/type_traits/remove_pointer.hpp>
  78. #include <boost/type_traits/remove_reference.hpp>
  79. #include <boost/type_traits/detail/is_likely_lambda.hpp>
  80. namespace boost {
  81. namespace detail {
  82. // This namespace ensures that argument-dependent name lookup does not mess things up.
  83. namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {
  84. // 1. a function to have an instance of type T without requiring T to be default
  85. // constructible
  86. template <typename T> T &make();
  87. // 2. we provide our operator definition for types that do not have one already
  88. // a type returned from operator BOOST_TT_TRAIT_OP when no such operator is
  89. // found in the type's own namespace (our own operator is used) so that we have
  90. // a means to know that our operator was used
  91. struct no_operator { };
  92. // this class allows implicit conversions and makes the following operator
  93. // definition less-preferred than any other such operators that might be found
  94. // via argument-dependent name lookup
  95. struct any { template <class T> any(T const&); };
  96. // when operator BOOST_TT_TRAIT_OP is not available, this one is used
  97. no_operator operator BOOST_TT_TRAIT_OP (const any&, const any&);
  98. // 3. checks if the operator returns void or not
  99. // conditions: Lhs!=void and Rhs!=void
  100. // we first redefine "operator," so that we have no compilation error if
  101. // operator BOOST_TT_TRAIT_OP returns void and we can use the return type of
  102. // (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) to deduce if
  103. // operator BOOST_TT_TRAIT_OP returns void or not:
  104. // - operator BOOST_TT_TRAIT_OP returns void -> (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns returns_void_t
  105. // - operator BOOST_TT_TRAIT_OP returns !=void -> (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns int
  106. struct returns_void_t { };
  107. template <typename T> int operator,(const T&, returns_void_t);
  108. template <typename T> int operator,(const volatile T&, returns_void_t);
  109. // this intermediate trait has member value of type bool:
  110. // - value==true -> operator BOOST_TT_TRAIT_OP returns void
  111. // - value==false -> operator BOOST_TT_TRAIT_OP does not return void
  112. template < typename Lhs, typename Rhs >
  113. struct operator_returns_void {
  114. // overloads of function returns_void make the difference
  115. // yes_type and no_type have different size by construction
  116. static ::boost::type_traits::yes_type returns_void(returns_void_t);
  117. static ::boost::type_traits::no_type returns_void(int);
  118. BOOST_STATIC_CONSTANT(bool, value = (sizeof(::boost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>(),returns_void_t())))));
  119. };
  120. // 4. checks if the return type is Ret or Ret==dont_care
  121. // conditions: Lhs!=void and Rhs!=void
  122. struct dont_care { };
  123. template < typename Lhs, typename Rhs, typename Ret, bool Returns_void >
  124. struct operator_returns_Ret;
  125. template < typename Lhs, typename Rhs >
  126. struct operator_returns_Ret < Lhs, Rhs, dont_care, true > {
  127. BOOST_STATIC_CONSTANT(bool, value = true);
  128. };
  129. template < typename Lhs, typename Rhs >
  130. struct operator_returns_Ret < Lhs, Rhs, dont_care, false > {
  131. BOOST_STATIC_CONSTANT(bool, value = true);
  132. };
  133. template < typename Lhs, typename Rhs >
  134. struct operator_returns_Ret < Lhs, Rhs, void, true > {
  135. BOOST_STATIC_CONSTANT(bool, value = true);
  136. };
  137. template < typename Lhs, typename Rhs >
  138. struct operator_returns_Ret < Lhs, Rhs, void, false > {
  139. BOOST_STATIC_CONSTANT(bool, value = false);
  140. };
  141. template < typename Lhs, typename Rhs, typename Ret >
  142. struct operator_returns_Ret < Lhs, Rhs, Ret, true > {
  143. BOOST_STATIC_CONSTANT(bool, value = false);
  144. };
  145. // otherwise checks if it is convertible to Ret using the sizeof trick
  146. // based on overload resolution
  147. // condition: Ret!=void and Ret!=dont_care and the operator does not return void
  148. template < typename Lhs, typename Rhs, typename Ret >
  149. struct operator_returns_Ret < Lhs, Rhs, Ret, false > {
  150. static ::boost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret
  151. static ::boost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise
  152. BOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>()))==sizeof(::boost::type_traits::yes_type)));
  153. };
  154. // 5. checks for operator existence
  155. // condition: Lhs!=void and Rhs!=void
  156. // checks if our definition of operator BOOST_TT_TRAIT_OP is used or an other
  157. // existing one;
  158. // this is done with redefinition of "operator," that returns no_operator or has_operator
  159. struct has_operator { };
  160. no_operator operator,(no_operator, has_operator);
  161. template < typename Lhs, typename Rhs >
  162. struct operator_exists {
  163. static ::boost::type_traits::yes_type s_check(has_operator); // this version is preferred when operator exists
  164. static ::boost::type_traits::no_type s_check(no_operator); // this version is used otherwise
  165. BOOST_STATIC_CONSTANT(bool, value = (sizeof(s_check(((make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>()),make<has_operator>())))==sizeof(::boost::type_traits::yes_type)));
  166. };
  167. // 6. main trait: to avoid any compilation error, this class behaves
  168. // differently when operator BOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the
  169. // standard.
  170. // Forbidden_if is a bool that is:
  171. // - true when the operator BOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the standard
  172. // (would yield compilation error if used)
  173. // - false otherwise
  174. template < typename Lhs, typename Rhs, typename Ret, bool Forbidden_if >
  175. struct trait_impl1;
  176. template < typename Lhs, typename Rhs, typename Ret >
  177. struct trait_impl1 < Lhs, Rhs, Ret, true > {
  178. BOOST_STATIC_CONSTANT(bool, value = false);
  179. };
  180. template < typename Lhs, typename Rhs, typename Ret >
  181. struct trait_impl1 < Lhs, Rhs, Ret, false > {
  182. BOOST_STATIC_CONSTANT(bool,
  183. value = (operator_exists < Lhs, Rhs >::value && operator_returns_Ret < Lhs, Rhs, Ret, operator_returns_void < Lhs, Rhs >::value >::value));
  184. };
  185. // some specializations needs to be declared for the special void case
  186. template < typename Rhs, typename Ret >
  187. struct trait_impl1 < void, Rhs, Ret, false > {
  188. BOOST_STATIC_CONSTANT(bool, value = false);
  189. };
  190. template < typename Lhs, typename Ret >
  191. struct trait_impl1 < Lhs, void, Ret, false > {
  192. BOOST_STATIC_CONSTANT(bool, value = false);
  193. };
  194. template < typename Ret >
  195. struct trait_impl1 < void, void, Ret, false > {
  196. BOOST_STATIC_CONSTANT(bool, value = false);
  197. };
  198. // defines some typedef for convenience
  199. template < typename Lhs, typename Rhs, typename Ret >
  200. struct trait_impl {
  201. typedef typename ::boost::remove_reference<Lhs>::type Lhs_noref;
  202. typedef typename ::boost::remove_reference<Rhs>::type Rhs_noref;
  203. typedef typename ::boost::remove_cv<Lhs_noref>::type Lhs_nocv;
  204. typedef typename ::boost::remove_cv<Rhs_noref>::type Rhs_nocv;
  205. typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Lhs_noref>::type >::type >::type Lhs_noptr;
  206. typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr;
  207. BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Rhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value));
  208. };
  209. } // namespace impl
  210. } // namespace detail
  211. // this is the accessible definition of the trait to end user
  212. template <class Lhs, class Rhs=Lhs, class Ret=::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care>
  213. struct BOOST_TT_TRAIT_NAME : public integral_constant<bool, (::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME, _impl)::trait_impl < Lhs, Rhs, Ret >::value)>{};
  214. } // namespace boost
  215. #endif
  216. #if defined(BOOST_MSVC)
  217. # pragma warning ( pop )
  218. #endif