is_copy_assignable_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // (C) Copyright John Maddock 2000.
  2. // (C) Copyright Ion Gaztanaga 2014.
  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. //#define TEST_STD
  7. #ifdef TEST_STD
  8. # include <type_traits>
  9. #else
  10. # include <boost/type_traits/is_copy_assignable.hpp>
  11. #endif
  12. #include "test.hpp"
  13. #include "check_integral_constant.hpp"
  14. #include <boost/move/core.hpp>
  15. struct has {
  16. has(){}
  17. has &operator=(const has&){ return *this; }
  18. };
  19. // MSVC can not generate neither default constructor, nor assignment operator,
  20. // nor copy constructor for `has2` type. Suppressing those warnings is essential,
  21. // because we treat warnings as errors in those tests
  22. #if (defined _MSC_VER)
  23. # pragma warning( push )
  24. # pragma warning( disable : 4510 4512 4610)
  25. #endif
  26. struct has2 {
  27. int i;
  28. has2 &operator=(const int& val) { i = val; return *this; }
  29. };
  30. #if (defined _MSC_VER)
  31. # pragma warning( pop )
  32. #endif
  33. struct has3 { // Copy assignment must be generated by compiler
  34. has3(has3*){}
  35. };
  36. struct has_not: public boost::noncopyable {
  37. typedef boost::noncopyable base_t;
  38. has_not() : base_t() {}
  39. };
  40. #if defined(BOOST_TT_CXX11_IS_COPY_ASSIGNABLE)
  41. struct has_not2 {
  42. has_not2() {}
  43. has_not2& operator=(has_not2&) = delete;
  44. };
  45. struct has_not3 {
  46. has_not3() {}
  47. has_not3& operator=(const has_not3&) = delete;
  48. };
  49. #endif // BOOST_TT_CXX11_IS_COPY_ASSIGNABLE
  50. struct has_not4: private boost::noncopyable {
  51. typedef boost::noncopyable base_t;
  52. has_not4() : base_t() {}
  53. private:
  54. has_not4& operator=(const has_not4&);
  55. };
  56. struct has_not5 {
  57. private:
  58. BOOST_MOVABLE_BUT_NOT_COPYABLE(has_not5)
  59. };
  60. struct has_not6 {
  61. has_not6& operator=(has_not6&){ return *this; }
  62. };
  63. TT_TEST_BEGIN(is_copy_assignable)
  64. // Main part of the test
  65. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has>::value, true);
  66. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has2>::value, true);
  67. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has3>::value, true);
  68. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not>::value, false);
  69. #if defined(BOOST_TT_CXX11_IS_COPY_ASSIGNABLE)
  70. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not2>::value, false);
  71. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not3>::value, false);
  72. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not6>::value, false);
  73. #endif
  74. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not4>::value, false);
  75. // Requires some basic support from Boost.Move in C++03
  76. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<has_not5>::value, false);
  77. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<bool>::value, true);
  78. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<bool const>::value, false);
  79. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<bool volatile>::value, true);
  80. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<bool const volatile>::value, false);
  81. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<signed char>::value, true);
  82. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<signed char const>::value, false);
  83. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<signed char volatile>::value, true);
  84. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<signed char const volatile>::value, false);
  85. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned char>::value, true);
  86. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned char const>::value, false);
  87. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<char>::value, true);
  88. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<char const>::value, false);
  89. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned char volatile>::value, true);
  90. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned char const volatile>::value, false);
  91. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<char volatile>::value, true);
  92. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<char const volatile>::value, false);
  93. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned short>::value, true);
  94. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned short const>::value, false);
  95. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<short>::value, true);
  96. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<short const>::value, false);
  97. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned short volatile>::value, true);
  98. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned short const volatile>::value, false);
  99. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<short volatile>::value, true);
  100. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<short const volatile>::value, false);
  101. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned int>::value, true);
  102. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned int const>::value, false);
  103. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int>::value, true);
  104. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int const>::value, false);
  105. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned int volatile>::value, true);
  106. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned int const volatile>::value, false);
  107. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int volatile>::value, true);
  108. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int const volatile>::value, false);
  109. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned long>::value, true);
  110. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned long const>::value, false);
  111. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long>::value, true);
  112. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long const>::value, false);
  113. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned long volatile>::value, true);
  114. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<unsigned long const volatile>::value, false);
  115. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long volatile>::value, true);
  116. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long const volatile>::value, false);
  117. #ifdef BOOST_HAS_LONG_LONG
  118. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::ulong_long_type>::value, true);
  119. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::ulong_long_type const>::value, false);
  120. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::long_long_type>::value, true);
  121. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::long_long_type const>::value, false);
  122. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::ulong_long_type volatile>::value, true);
  123. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::ulong_long_type const volatile>::value, false);
  124. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::long_long_type volatile>::value, true);
  125. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable< ::boost::long_long_type const volatile>::value, false);
  126. #endif
  127. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<float>::value, true);
  128. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<float const>::value, false);
  129. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<float volatile>::value, true);
  130. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<float const volatile>::value, false);
  131. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<double>::value, true);
  132. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<double const>::value, false);
  133. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<double volatile>::value, true);
  134. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<double const volatile>::value, false);
  135. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long double>::value, true);
  136. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long double const>::value, false);
  137. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long double volatile>::value, true);
  138. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<long double const volatile>::value, false);
  139. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int>::value, true);
  140. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<void*>::value, true);
  141. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int*const>::value, false);
  142. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<f1>::value, true);
  143. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<f2>::value, true);
  144. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<f3>::value, true);
  145. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<mf1>::value, true);
  146. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<mf2>::value, true);
  147. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<mf3>::value, true);
  148. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<mp>::value, true);
  149. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<cmf>::value, true);
  150. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<enum_UDT>::value, true);
  151. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int&>::value, true);
  152. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<const int&>::value, false);
  153. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  154. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int&&>::value, true);
  155. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<const int&&>::value, false);
  156. #endif
  157. // Following three tests may give different results because of compiler and C++03/C++11.
  158. // On C++11 compiler following code:
  159. // int c[2][4][5][6][3];
  160. // int b[2][4][5][6][3] = std::move(c);
  161. // does not compile, so we expect `false` to be the result of those three tests.
  162. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int[2]>::value, false, true);
  163. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int[3][2]>::value, false, true);
  164. BOOST_CHECK_SOFT_INTEGRAL_CONSTANT(::tt::is_copy_assignable<int[2][4][5][6][3]>::value, false, true);
  165. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<UDT>::value, true);
  166. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<void>::value, false);
  167. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<empty_POD_UDT>::value, true);
  168. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<POD_UDT>::value, true);
  169. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<POD_union_UDT>::value, true);
  170. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<empty_POD_union_UDT>::value, true);
  171. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<nothrow_copy_UDT>::value, true);
  172. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<nothrow_assign_UDT>::value, true);
  173. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_assignable<nothrow_construct_UDT>::value, true);
  174. TT_TEST_END