has_postfix_operator.hpp 9.8 KB

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