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