has_prefix_operator.hpp 11 KB

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