common_type.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*******************************************************************************
  2. * boost/type_traits/detail/common_type.hpp
  3. *
  4. * Copyright 2010, Jeffrey Hellrung.
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * struct boost::common_type<T,U>
  9. *
  10. * common_type<T,U>::type is the type of the expression
  11. * b() ? x() : y()
  12. * where b() returns a bool, x() has return type T, and y() has return type U.
  13. * See
  14. * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm#common_type
  15. *
  16. * Note that this evaluates to void if one or both of T and U is void.
  17. ******************************************************************************/
  18. #ifndef BOOST_EX_TYPE_TRAITS_EXT_DETAIL_COMMON_TYPE_HPP
  19. #define BOOST_EX_TYPE_TRAITS_EXT_DETAIL_COMMON_TYPE_HPP
  20. #include <cstddef>
  21. #include <boost/mpl/assert.hpp>
  22. #include <boost/mpl/at.hpp>
  23. #include <boost/mpl/begin_end.hpp>
  24. #include <boost/mpl/contains.hpp>
  25. #include <boost/mpl/copy.hpp>
  26. #include <boost/mpl/deref.hpp>
  27. #include <boost/mpl/eval_if.hpp>
  28. #include <boost/mpl/if.hpp>
  29. #include <boost/mpl/inserter.hpp>
  30. #include <boost/mpl/next.hpp>
  31. #include <boost/mpl/or.hpp>
  32. #include <boost/mpl/placeholders.hpp>
  33. #include <boost/mpl/push_back.hpp>
  34. #include <boost/mpl/size.hpp>
  35. #include <boost/mpl/vector/vector0.hpp>
  36. #include <boost/mpl/vector/vector10.hpp>
  37. #include <boost/type_traits/integral_constant.hpp>
  38. #include <boost/type_traits/is_enum.hpp>
  39. #include <boost/type_traits/is_integral.hpp>
  40. #include <boost/type_traits/make_signed.hpp>
  41. #include <boost/type_traits/make_unsigned.hpp>
  42. #include <boost/type_traits/remove_cv.hpp>
  43. #include <boost/type_traits/remove_reference.hpp>
  44. #include <boost/type_traits/declval.hpp>
  45. namespace boost_ex
  46. {
  47. namespace detail_type_traits_common_type
  48. {
  49. /*******************************************************************************
  50. * struct propagate_cv< From, To >
  51. *
  52. * This metafunction propagates cv-qualifiers on type From to type To.
  53. ******************************************************************************/
  54. template< class From, class To >
  55. struct propagate_cv
  56. { typedef To type; };
  57. template< class From, class To >
  58. struct propagate_cv< const From, To >
  59. { typedef To const type; };
  60. template< class From, class To >
  61. struct propagate_cv< volatile From, To >
  62. { typedef To volatile type; };
  63. template< class From, class To >
  64. struct propagate_cv< const volatile From, To >
  65. { typedef To const volatile type; };
  66. /*******************************************************************************
  67. * struct is_signable_integral<T>
  68. *
  69. * This metafunction determines if T is an integral type which can be made
  70. * signed or unsigned.
  71. ******************************************************************************/
  72. template< class T >
  73. struct is_signable_integral
  74. : mpl::or_< is_integral<T>, is_enum<T> >
  75. { };
  76. template<>
  77. struct is_signable_integral< bool >
  78. : false_type
  79. { };
  80. /*******************************************************************************
  81. * struct sizeof_t<N>
  82. * typedef ... yes_type
  83. * typedef ... no_type
  84. *
  85. * These types are integral players in the use of the "sizeof trick", i.e., we
  86. * can distinguish overload selection by inspecting the size of the return type
  87. * of the overload.
  88. ******************************************************************************/
  89. template< std::size_t N > struct sizeof_t { char _dummy[N]; };
  90. typedef sizeof_t<1> yes_type;
  91. typedef sizeof_t<2> no_type;
  92. BOOST_MPL_ASSERT_RELATION( sizeof( yes_type ), ==, 1 );
  93. BOOST_MPL_ASSERT_RELATION( sizeof( no_type ), ==, 2 );
  94. /*******************************************************************************
  95. * rvalue_test(T&) -> no_type
  96. * rvalue_test(...) -> yes_type
  97. *
  98. * These overloads are used to determine the rvalue-ness of an expression.
  99. ******************************************************************************/
  100. template< class T > no_type rvalue_test(T&);
  101. yes_type rvalue_test(...);
  102. /*******************************************************************************
  103. * struct conversion_test_overloads< Sequence >
  104. *
  105. * This struct has multiple overloads of the static member function apply, each
  106. * one taking a single parameter of a type within the Boost.MPL sequence
  107. * Sequence. Each such apply overload has a return type with sizeof equal to
  108. * one plus the index of the parameter type within Sequence. Thus, we can
  109. * deduce the type T of an expression as long as we can generate a finite set of
  110. * candidate types containing T via these apply overloads and the "sizeof
  111. * trick".
  112. ******************************************************************************/
  113. template< class First, class Last, std::size_t Index >
  114. struct conversion_test_overloads_iterate
  115. : conversion_test_overloads_iterate<
  116. typename mpl::next< First >::type, Last, Index + 1
  117. >
  118. {
  119. using conversion_test_overloads_iterate<
  120. typename mpl::next< First >::type, Last, Index + 1
  121. >::apply;
  122. static sizeof_t< Index + 1 >
  123. apply(typename mpl::deref< First >::type);
  124. };
  125. template< class Last, std::size_t Index >
  126. struct conversion_test_overloads_iterate< Last, Last, Index >
  127. { static sizeof_t< Index + 1 > apply(...); };
  128. template< class Sequence >
  129. struct conversion_test_overloads
  130. : conversion_test_overloads_iterate<
  131. typename mpl::begin< Sequence >::type,
  132. typename mpl::end< Sequence >::type,
  133. 0
  134. >
  135. { };
  136. /*******************************************************************************
  137. * struct select< Sequence, Index >
  138. *
  139. * select is synonymous with mpl::at_c unless Index equals the size of the
  140. * Boost.MPL Sequence, in which case this evaluates to void.
  141. ******************************************************************************/
  142. template<
  143. class Sequence, int Index,
  144. int N = mpl::size< Sequence >::value
  145. >
  146. struct select
  147. : mpl::at_c< Sequence, Index >
  148. { };
  149. template< class Sequence, int N >
  150. struct select< Sequence, N, N >
  151. { typedef void type; };
  152. /*******************************************************************************
  153. * class deduce_common_type< T, U, NominalCandidates >
  154. * struct nominal_candidates<T,U>
  155. * struct common_type_dispatch_on_rvalueness<T,U>
  156. * struct common_type_impl<T,U>
  157. *
  158. * These classes and structs implement the logic behind common_type, which goes
  159. * roughly as follows. Let C be the type of the conditional expression
  160. * declval< bool >() ? declval<T>() : declval<U>()
  161. * if C is an rvalue, then:
  162. * let T' and U' be T and U stripped of reference- and cv-qualifiers
  163. * if T' and U' are pointer types, say, T' = V* and U' = W*, then:
  164. * define the set of NominalCandidates to be
  165. * { V*, W*, V'*, W'* }
  166. * where V' is V with whatever cv-qualifiers are on W, and W' is W
  167. * with whatever cv-qualifiers are on V
  168. * else T' and U' are both "signable integral types" (integral and enum
  169. * types excepting bool), then:
  170. * define the set of NominalCandidates to be
  171. * { unsigned(T'), unsigned(U'), signed(T'), signed(U') }
  172. * where unsigned(X) is make_unsigned<X>::type and signed(X) is
  173. * make_signed<X>::type
  174. * else
  175. * define the set of NominalCandidates to be
  176. * { T', U' }
  177. * else
  178. * let V and W be T and U stripped of reference-qualifiers
  179. * define the set of NominalCandidates to be
  180. * { V&, W&, V'&, W'& }
  181. * where V' is V with whatever cv-qualifiers are on W, and W' is W with
  182. * whatever cv-qualifiers are on V
  183. * define the set of Candidates to be equal to the set of NominalCandidates with
  184. * duplicates removed, and use this set of Candidates to determine C using the
  185. * conversion_test_overloads struct
  186. ******************************************************************************/
  187. template< class T, class U, class NominalCandidates >
  188. class deduce_common_type
  189. {
  190. typedef typename mpl::copy<
  191. NominalCandidates,
  192. mpl::inserter<
  193. mpl::vector0<>,
  194. mpl::if_<
  195. mpl::contains< mpl::_1, mpl::_2 >,
  196. mpl::_1,
  197. mpl::push_back< mpl::_1, mpl::_2 >
  198. >
  199. >
  200. >::type candidate_types;
  201. static const int best_candidate_index =
  202. sizeof( conversion_test_overloads< candidate_types >::apply(
  203. declval< bool >() ? declval<T>() : declval<U>()
  204. ) ) - 1;
  205. public:
  206. typedef typename select< candidate_types, best_candidate_index >::type type;
  207. };
  208. template<
  209. class T, class U,
  210. class V = typename remove_cv< typename remove_reference<T>::type >::type,
  211. class W = typename remove_cv< typename remove_reference<U>::type >::type,
  212. bool = is_signable_integral<V>::value && is_signable_integral<W>::value
  213. >
  214. struct nominal_candidates;
  215. template< class T, class U, class V, class W >
  216. struct nominal_candidates< T, U, V, W, false >
  217. { typedef mpl::vector2<V,W> type; };
  218. template< class T, class U, class V, class W >
  219. struct nominal_candidates< T, U, V, W, true >
  220. {
  221. typedef mpl::vector4<
  222. typename make_unsigned<V>::type,
  223. typename make_unsigned<W>::type,
  224. typename make_signed<V>::type,
  225. typename make_signed<W>::type
  226. > type;
  227. };
  228. template< class T, class U, class V, class W >
  229. struct nominal_candidates< T, U, V*, W*, false >
  230. {
  231. typedef mpl::vector4<
  232. V*, W*,
  233. typename propagate_cv<W,V>::type *,
  234. typename propagate_cv<V,W>::type *
  235. > type;
  236. };
  237. template<
  238. class T, class U,
  239. bool = sizeof( ::boost::detail_type_traits_common_type::rvalue_test(
  240. declval< bool >() ? declval<T>() : declval<U>()
  241. ) ) == sizeof( yes_type )
  242. >
  243. struct common_type_dispatch_on_rvalueness;
  244. template< class T, class U >
  245. struct common_type_dispatch_on_rvalueness< T, U, true >
  246. : deduce_common_type< T, U, typename nominal_candidates<T,U>::type >
  247. { };
  248. template< class T, class U >
  249. struct common_type_dispatch_on_rvalueness< T, U, false >
  250. {
  251. private:
  252. typedef typename remove_reference<T>::type unrefed_T_type;
  253. typedef typename remove_reference<U>::type unrefed_U_type;
  254. public:
  255. typedef typename deduce_common_type<
  256. T, U,
  257. mpl::vector4<
  258. unrefed_T_type &,
  259. unrefed_U_type &,
  260. typename propagate_cv< unrefed_U_type, unrefed_T_type >::type &,
  261. typename propagate_cv< unrefed_T_type, unrefed_U_type >::type &
  262. >
  263. >::type type;
  264. };
  265. template< class T, class U >
  266. struct common_type_impl
  267. : common_type_dispatch_on_rvalueness<T,U>
  268. { };
  269. template< class T > struct common_type_impl< T, void > { typedef void type; };
  270. template< class T > struct common_type_impl< void, T > { typedef void type; };
  271. template<> struct common_type_impl< void, void > { typedef void type; };
  272. template< > struct common_type_impl< char, short> { typedef int type; };
  273. template< > struct common_type_impl< short, char> { typedef int type; };
  274. template< > struct common_type_impl< unsigned char, short> { typedef int type; };
  275. template< > struct common_type_impl< short, unsigned char> { typedef int type; };
  276. template< > struct common_type_impl< unsigned char, unsigned short> { typedef int type; };
  277. template< > struct common_type_impl< unsigned short, unsigned char> { typedef int type; };
  278. template< > struct common_type_impl< char, unsigned short> { typedef int type; };
  279. template< > struct common_type_impl< unsigned short, char> { typedef int type; };
  280. } // namespace detail_type_traits_common_type
  281. } // namespace boost_ex
  282. #endif // BOOST_EX_TYPE_TRAITS_EXT_DETAIL_COMMON_TYPE_HPP