is_copy_constructible_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // (C) Copyright John Maddock 2000.
  2. // (C) Copyright Antony Polukhin 2013.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifdef TEST_STD
  7. # include <type_traits>
  8. #else
  9. # include <boost/type_traits/is_copy_constructible.hpp>
  10. #endif
  11. #include "test.hpp"
  12. #include "check_integral_constant.hpp"
  13. #include <boost/move/core.hpp>
  14. struct has {
  15. has(){}
  16. has(const has&){}
  17. };
  18. // MSVC can not generate neither default constructor, nor assignment operator,
  19. // nor copy constructor for `has2` type. Suppressing those warnings is essential,
  20. // because we treat warnings as errors in those tests
  21. #if (defined _MSC_VER)
  22. # pragma warning( push )
  23. # pragma warning( disable : 4510 4512 4610)
  24. #endif
  25. struct has2 {
  26. const int& i;
  27. explicit has2(const int& val) : i(val) {}
  28. };
  29. #if (defined _MSC_VER)
  30. # pragma warning( pop )
  31. #endif
  32. struct has3 {
  33. has3(has3&){}
  34. };
  35. struct has4 { // Copy constructor must be generated by compiler
  36. has4(has4*){}
  37. };
  38. struct has_not: public boost::noncopyable {
  39. typedef boost::noncopyable base_t;
  40. has_not() : base_t() {}
  41. };
  42. #ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
  43. struct has_not2 {
  44. has_not2() {}
  45. has_not2(has_not2&) = delete;
  46. };
  47. struct has_not3 {
  48. has_not3() {}
  49. has_not3(const has_not3&) = delete;
  50. };
  51. #endif // BOOST_NO_CXX11_DELETED_FUNCTIONS
  52. struct has_not4: private boost::noncopyable {
  53. typedef boost::noncopyable base_t;
  54. has_not4() : base_t() {}
  55. private:
  56. has_not4(const has_not4&);
  57. };
  58. struct has_not5 {
  59. private:
  60. BOOST_MOVABLE_BUT_NOT_COPYABLE(has_not5)
  61. };
  62. TT_TEST_BEGIN(is_copy_constructible)
  63. // Main part of the test
  64. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has>::value, true);
  65. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has2>::value, true);
  66. // Only constructible from has3& not from const-reference, this only works if we have decltype:
  67. #if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  68. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has3>::value, false);
  69. #endif
  70. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has4>::value, true);
  71. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not>::value, false);
  72. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) && !defined(BOOST_INTEL_CXX_VERSION)
  73. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not2>::value, false);
  74. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not3>::value, false);
  75. #endif
  76. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not4>::value, false);
  77. // Requires some basic support from Boost.Move in C++03
  78. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not5>::value, false);
  79. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<bool>::value, true);
  80. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<bool const>::value, true);
  81. #ifndef TEST_STD
  82. // unspecified behaviour:
  83. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<bool volatile>::value, true);
  84. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<bool const volatile>::value, true);
  85. #endif
  86. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<signed char>::value, true);
  87. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<signed char const>::value, true);
  88. #ifndef TEST_STD
  89. // unspecified behaviour:
  90. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<signed char volatile>::value, true);
  91. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<signed char const volatile>::value, true);
  92. #endif
  93. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned char>::value, true);
  94. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<char>::value, true);
  95. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned char const>::value, true);
  96. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<char const>::value, true);
  97. #ifndef TEST_STD
  98. // unspecified behaviour:
  99. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned char volatile>::value, true);
  100. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<char volatile>::value, true);
  101. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned char const volatile>::value, true);
  102. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<char const volatile>::value, true);
  103. #endif
  104. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned short>::value, true);
  105. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<short>::value, true);
  106. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned short const>::value, true);
  107. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<short const>::value, true);
  108. #ifndef TEST_STD
  109. // unspecified behaviour:
  110. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned short volatile>::value, true);
  111. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<short volatile>::value, true);
  112. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned short const volatile>::value, true);
  113. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<short const volatile>::value, true);
  114. #endif
  115. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned int>::value, true);
  116. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int>::value, true);
  117. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned int const>::value, true);
  118. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int const>::value, true);
  119. #ifndef TEST_STD
  120. // unspecified behaviour:
  121. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned int volatile>::value, true);
  122. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int volatile>::value, true);
  123. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned int const volatile>::value, true);
  124. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int const volatile>::value, true);
  125. #endif
  126. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned long>::value, true);
  127. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long>::value, true);
  128. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned long const>::value, true);
  129. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long const>::value, true);
  130. #ifndef TEST_STD
  131. // unspecified behaviour:
  132. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned long volatile>::value, true);
  133. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long volatile>::value, true);
  134. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned long const volatile>::value, true);
  135. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long const volatile>::value, true);
  136. #endif
  137. #ifdef BOOST_HAS_LONG_LONG
  138. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::ulong_long_type>::value, true);
  139. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::long_long_type>::value, true);
  140. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::ulong_long_type const>::value, true);
  141. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::long_long_type const>::value, true);
  142. #ifndef TEST_STD
  143. // unspecified behaviour:
  144. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::ulong_long_type volatile>::value, true);
  145. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::long_long_type volatile>::value, true);
  146. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::ulong_long_type const volatile>::value, true);
  147. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible< ::boost::long_long_type const volatile>::value, true);
  148. #endif
  149. #endif
  150. #ifdef BOOST_HAS_MS_INT64
  151. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int8>::value, true);
  152. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int8>::value, true);
  153. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int8 const>::value, true);
  154. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int8 const>::value, true);
  155. #ifndef TEST_STD
  156. // unspecified behaviour:
  157. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int8 volatile>::value, true);
  158. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int8 volatile>::value, true);
  159. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int8 const volatile>::value, true);
  160. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int8 const volatile>::value, true);
  161. #endif
  162. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int16>::value, true);
  163. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int16>::value, true);
  164. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int16 const>::value, true);
  165. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int16 const>::value, true);
  166. #ifndef TEST_STD
  167. // unspecified behaviour:
  168. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int16 volatile>::value, true);
  169. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int16 volatile>::value, true);
  170. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int16 const volatile>::value, true);
  171. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int16 const volatile>::value, true);
  172. #endif
  173. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int32>::value, true);
  174. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int32>::value, true);
  175. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int32 const>::value, true);
  176. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int32 const>::value, true);
  177. #ifndef TEST_STD
  178. // unspecified behaviour:
  179. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int32 volatile>::value, true);
  180. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int32 volatile>::value, true);
  181. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int32 const volatile>::value, true);
  182. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int32 const volatile>::value, true);
  183. #endif
  184. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int64>::value, true);
  185. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int64>::value, true);
  186. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int64 const>::value, true);
  187. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int64 const>::value, true);
  188. #ifndef TEST_STD
  189. // unspecified behaviour:
  190. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int64 volatile>::value, true);
  191. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int64 volatile>::value, true);
  192. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<unsigned __int64 const volatile>::value, true);
  193. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<__int64 const volatile>::value, true);
  194. #endif
  195. #endif
  196. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<float>::value, true);
  197. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<float const>::value, true);
  198. #ifndef TEST_STD
  199. // unspecified behaviour:
  200. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<float volatile>::value, true);
  201. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<float const volatile>::value, true);
  202. #endif
  203. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<double>::value, true);
  204. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<double const>::value, true);
  205. #ifndef TEST_STD
  206. // unspecified behaviour:
  207. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<double volatile>::value, true);
  208. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<double const volatile>::value, true);
  209. #endif
  210. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long double>::value, true);
  211. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long double const>::value, true);
  212. #ifndef TEST_STD
  213. // unspecified behaviour:
  214. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long double volatile>::value, true);
  215. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<long double const volatile>::value, true);
  216. #endif
  217. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int>::value, true);
  218. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<void*>::value, true);
  219. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int*const>::value, true);
  220. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<f1>::value, true);
  221. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<f2>::value, true);
  222. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<f3>::value, true);
  223. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<mf1>::value, true);
  224. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<mf2>::value, true);
  225. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<mf3>::value, true);
  226. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<mp>::value, true);
  227. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<cmf>::value, true);
  228. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<enum_UDT>::value, true);
  229. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int&>::value, true);
  230. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  231. // This is debatable, we used to insist this was true, but copy-constructibility
  232. // implies copying a constant-object, and that isn't the case here:
  233. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int&&>::value, false);
  234. #endif
  235. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<const int&>::value, true);
  236. // Following three tests may give different results because of compiler and C++03/C++11.
  237. // On C++11 compiler following code:
  238. // int c[2][4][5][6][3];
  239. // int b[2][4][5][6][3] = std::move(c);
  240. // does not compile, so we expect `false` to be the result of those three tests.
  241. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int[2]>::value, false, true);
  242. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int[3][2]>::value, false, true);
  243. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int[2][4][5][6][3]>::value, false, true);
  244. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<UDT>::value, true);
  245. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<void>::value, false);
  246. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<empty_POD_UDT>::value, true);
  247. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<POD_UDT>::value, true);
  248. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<POD_union_UDT>::value, true);
  249. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<empty_POD_union_UDT>::value, true);
  250. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<nothrow_copy_UDT>::value, true);
  251. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<nothrow_assign_UDT>::value, true);
  252. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<nothrow_construct_UDT>::value, true);
  253. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_NOEXCEPT)
  254. // Copy constructor of nothrow_move_UDT is implicitly deleted because of user declared move constructor.
  255. // Hovewer not all compilers implement that feature correctly.
  256. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_constructible<nothrow_move_UDT>::value, false, true);
  257. #endif
  258. //TODO: What do we need to do in this situation?
  259. //BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<test_abc1>::value, true);
  260. TT_TEST_END