common_type_test.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // common_type_test.cpp ----------------------------------------------------//
  2. // Copyright 2010 Beman Dawes
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. #ifdef TEST_STD
  6. # include <type_traits>
  7. #else
  8. # include <boost/type_traits/common_type.hpp>
  9. #endif
  10. #include "test.hpp"
  11. #include "check_type.hpp"
  12. #include <iostream>
  13. #ifdef BOOST_INTEL
  14. #pragma warning(disable: 304 383)
  15. #endif
  16. struct C1 {};
  17. struct C2 {};
  18. struct C3 : C2 {};
  19. struct C1C2 {
  20. C1C2() {}
  21. C1C2(C1 const&) {}
  22. C1C2(C2 const&) {}
  23. C1C2& operator=(C1C2 const&) {
  24. return *this;
  25. }
  26. };
  27. template <typename C, typename A>
  28. void proc2(typename boost::common_type<A, C>::type const& ) {}
  29. template <typename C, typename A, typename B>
  30. void proc3(typename boost::common_type<C, A, B>::type const& ) {}
  31. template <typename C, typename A>
  32. void assignation_2() {
  33. typedef typename boost::common_type<A, C>::type AC;
  34. A a;
  35. C c;
  36. AC ac;
  37. ac=a;
  38. ac=c;
  39. proc2<C, A>(a);
  40. proc2<C, A>(c);
  41. }
  42. template <typename C, typename A, typename B>
  43. void assignation_3() {
  44. typedef typename boost::common_type<C, A, B>::type ABC;
  45. A a;
  46. B b;
  47. C c;
  48. ABC abc;
  49. abc=a;
  50. abc=b;
  51. abc=c;
  52. proc3<C, A, B>(a);
  53. proc3<C, A, B>(b);
  54. proc3<C, A, B>(c);
  55. }
  56. C1C2 c1c2;
  57. C1 c1;
  58. int f(C1C2 ) { return 1;}
  59. int f(C1 ) { return 2;}
  60. template <typename OSTREAM>
  61. OSTREAM& operator<<(OSTREAM& os, C1 const&) {return os;}
  62. C1C2& declval_C1C2() {return c1c2;}
  63. C1& declval_C1(){return c1;}
  64. bool declval_bool(){return true;}
  65. TT_TEST_BEGIN(common_type)
  66. {
  67. assignation_2<C1C2, C1>();
  68. typedef tt::common_type<C1C2&, C1&>::type T1;
  69. BOOST_CHECK_TYPE(T1, C1C2);
  70. typedef tt::common_type<C3*, C2*>::type T2;
  71. BOOST_CHECK_TYPE(T2, C2*);
  72. typedef tt::common_type<int*, int const*>::type T3;
  73. BOOST_CHECK_TYPE(T3, int const*);
  74. #if defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_COMMON_TYPE_DONT_USE_TYPEOF)
  75. // fails if BOOST_COMMON_TYPE_DONT_USE_TYPEOF:
  76. typedef tt::common_type<int volatile*, int const*>::type T4;
  77. BOOST_CHECK_TYPE(T4, int const volatile*);
  78. #endif
  79. typedef tt::common_type<int*, int volatile*>::type T5;
  80. BOOST_CHECK_TYPE(T5, int volatile*);
  81. assignation_2<C1, C1C2>();
  82. assignation_2<C1C2, C2>();
  83. assignation_2<C2, C1C2>();
  84. assignation_3<C1, C1C2, C2>();
  85. assignation_3<C1C2, C1, C2>();
  86. assignation_3<C2, C1C2, C1>();
  87. assignation_3<C1C2, C2, C1>();
  88. //assignation_3<C1, C2, C1C2>(); // fails because the common type is the third
  89. BOOST_CHECK_TYPE(tt::common_type<int>::type, int);
  90. BOOST_CHECK_TYPE(tt::common_type<char>::type, char);
  91. BOOST_CHECK_TYPE3(tt::common_type<char, char>::type, char);
  92. BOOST_CHECK_TYPE3(tt::common_type<char, unsigned char>::type, int);
  93. BOOST_CHECK_TYPE3(tt::common_type<char, short>::type, int);
  94. BOOST_CHECK_TYPE3(tt::common_type<char, unsigned short>::type, int);
  95. BOOST_CHECK_TYPE3(tt::common_type<char, int>::type, int);
  96. BOOST_CHECK_TYPE3(tt::common_type<char, unsigned int>::type, unsigned int);
  97. #ifndef BOOST_NO_LONG_LONG
  98. BOOST_CHECK_TYPE3(tt::common_type<char, boost::long_long_type>::type, boost::long_long_type);
  99. BOOST_CHECK_TYPE3(tt::common_type<char, boost::ulong_long_type>::type, boost::ulong_long_type);
  100. #endif
  101. BOOST_CHECK_TYPE3(tt::common_type<char, double>::type, double);
  102. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, char>::type, int);
  103. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned char>::type, unsigned char);
  104. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, short>::type, int);
  105. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned short>::type, int);
  106. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, int>::type, int);
  107. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned int>::type, unsigned int);
  108. #ifndef BOOST_NO_LONG_LONG
  109. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::long_long_type>::type, boost::long_long_type);
  110. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::ulong_long_type>::type, boost::ulong_long_type);
  111. #endif
  112. BOOST_CHECK_TYPE3(tt::common_type<unsigned char, double>::type, double);
  113. BOOST_CHECK_TYPE3(tt::common_type<short, char>::type, int);
  114. BOOST_CHECK_TYPE3(tt::common_type<short, unsigned char>::type, int);
  115. BOOST_CHECK_TYPE3(tt::common_type<short, short>::type, short);
  116. BOOST_CHECK_TYPE3(tt::common_type<short, unsigned short>::type, int);
  117. BOOST_CHECK_TYPE3(tt::common_type<short, int>::type, int);
  118. BOOST_CHECK_TYPE3(tt::common_type<short, unsigned int>::type, unsigned int);
  119. #ifndef BOOST_NO_LONG_LONG
  120. BOOST_CHECK_TYPE3(tt::common_type<short, boost::long_long_type>::type, boost::long_long_type);
  121. BOOST_CHECK_TYPE3(tt::common_type<short, boost::ulong_long_type>::type, boost::ulong_long_type);
  122. #endif
  123. BOOST_CHECK_TYPE3(tt::common_type<short, double>::type, double);
  124. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, char>::type, int);
  125. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned char>::type, int);
  126. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, short>::type, int);
  127. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned short>::type, unsigned short);
  128. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, int>::type, int);
  129. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned int>::type, unsigned int);
  130. #ifndef BOOST_NO_LONG_LONG
  131. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::long_long_type>::type, boost::long_long_type);
  132. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::ulong_long_type>::type, boost::ulong_long_type);
  133. #endif
  134. BOOST_CHECK_TYPE3(tt::common_type<unsigned short, double>::type, double);
  135. BOOST_CHECK_TYPE3(tt::common_type<int, char>::type, int);
  136. BOOST_CHECK_TYPE3(tt::common_type<int, unsigned char>::type, int);
  137. BOOST_CHECK_TYPE3(tt::common_type<int, short>::type, int);
  138. BOOST_CHECK_TYPE3(tt::common_type<int, unsigned short>::type, int);
  139. BOOST_CHECK_TYPE3(tt::common_type<int, int>::type, int);
  140. BOOST_CHECK_TYPE3(tt::common_type<int, unsigned int>::type, unsigned int);
  141. #ifndef BOOST_NO_LONG_LONG
  142. BOOST_CHECK_TYPE3(tt::common_type<int, boost::long_long_type>::type, boost::long_long_type);
  143. BOOST_CHECK_TYPE3(tt::common_type<int, boost::ulong_long_type>::type, boost::ulong_long_type);
  144. #endif
  145. BOOST_CHECK_TYPE3(tt::common_type<int, double>::type, double);
  146. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, char>::type, unsigned int);
  147. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned char>::type, unsigned int);
  148. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, short>::type, unsigned int);
  149. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned short>::type, unsigned int);
  150. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, int>::type, unsigned int);
  151. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned int>::type, unsigned int);
  152. #ifndef BOOST_NO_LONG_LONG
  153. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::long_long_type>::type, boost::long_long_type);
  154. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::ulong_long_type>::type, boost::ulong_long_type);
  155. #endif
  156. BOOST_CHECK_TYPE3(tt::common_type<unsigned int, double>::type, double);
  157. #ifndef BOOST_NO_LONG_LONG
  158. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, char>::type, boost::long_long_type);
  159. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned char>::type, boost::long_long_type);
  160. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, short>::type, boost::long_long_type);
  161. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned short>::type, boost::long_long_type);
  162. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, int>::type, boost::long_long_type);
  163. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned int>::type, boost::long_long_type);
  164. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::long_long_type>::type, boost::long_long_type);
  165. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::ulong_long_type>::type, boost::ulong_long_type);
  166. BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, double>::type, double);
  167. #endif
  168. BOOST_CHECK_TYPE3(tt::common_type<double, char>::type, double);
  169. BOOST_CHECK_TYPE3(tt::common_type<double, unsigned char>::type, double);
  170. BOOST_CHECK_TYPE3(tt::common_type<double, short>::type, double);
  171. BOOST_CHECK_TYPE3(tt::common_type<double, unsigned short>::type, double);
  172. BOOST_CHECK_TYPE3(tt::common_type<double, int>::type, double);
  173. BOOST_CHECK_TYPE3(tt::common_type<double, unsigned int>::type, double);
  174. #ifndef BOOST_NO_LONG_LONG
  175. BOOST_CHECK_TYPE3(tt::common_type<double, boost::long_long_type>::type, double);
  176. BOOST_CHECK_TYPE3(tt::common_type<double, boost::ulong_long_type>::type, double);
  177. #endif
  178. BOOST_CHECK_TYPE3(tt::common_type<double, double>::type, double);
  179. BOOST_CHECK_TYPE4(tt::common_type<double, char, int>::type, double);
  180. #ifndef BOOST_NO_LONG_LONG
  181. BOOST_CHECK_TYPE4(tt::common_type<unsigned, char, boost::long_long_type>::type, boost::long_long_type);
  182. #endif
  183. //changes related to defect LWG2141
  184. BOOST_CHECK_TYPE(tt::common_type<int&>::type, int);
  185. BOOST_CHECK_TYPE(tt::common_type<const int>::type, int);
  186. BOOST_CHECK_TYPE3(tt::common_type<const int, const int>::type, int);
  187. BOOST_CHECK_TYPE3(tt::common_type<const int, const long>::type, long);
  188. }
  189. TT_TEST_END