common_type_2_test.cpp 9.2 KB

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