variant_move_assign.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2017 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/variant2/variant.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/core/lightweight_test_trait.hpp>
  10. #include <type_traits>
  11. #include <utility>
  12. #include <string>
  13. using namespace boost::variant2;
  14. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  15. struct X1
  16. {
  17. int v;
  18. X1(): v(0) {}
  19. explicit X1(int v): v(v) {}
  20. X1(X1 const& r): v(r.v) {}
  21. X1(X1&& r): v(r.v) {}
  22. X1& operator=( X1 const& r ) { v = r.v; return *this; }
  23. X1& operator=( X1&& r ) { v = r.v; return *this; }
  24. };
  25. inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
  26. STATIC_ASSERT( !std::is_nothrow_default_constructible<X1>::value );
  27. STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
  28. STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
  29. STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
  30. STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
  31. struct X2
  32. {
  33. int v;
  34. X2(): v(0) {}
  35. explicit X2(int v): v(v) {}
  36. X2(X2 const& r): v(r.v) {}
  37. X2(X2&& r): v(r.v) {}
  38. X2& operator=( X2 const& r ) { v = r.v; return *this; }
  39. X2& operator=( X2&& r ) { v = r.v; return *this; }
  40. };
  41. inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
  42. STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
  43. STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
  44. STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
  45. STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
  46. STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
  47. struct Y
  48. {
  49. Y& operator=( Y&& ) = delete;
  50. };
  51. int main()
  52. {
  53. {
  54. variant<int> v;
  55. BOOST_TEST_EQ( get<0>(v), 0 );
  56. variant<int> v2( 1 );
  57. v = std::move(v2);
  58. BOOST_TEST_EQ( get<0>(v), 1 );
  59. variant<int> v3( 2 );
  60. v = std::move(v3);
  61. BOOST_TEST_EQ( get<0>(v), 2 );
  62. }
  63. {
  64. variant<int, float> v;
  65. BOOST_TEST_EQ( v.index(), 0 );
  66. BOOST_TEST_EQ( get<0>(v), 0 );
  67. variant<int, float> v2( 1 );
  68. v = std::move(v2);
  69. BOOST_TEST_EQ( v.index(), 0 );
  70. BOOST_TEST_EQ( get<0>(v), 1 );
  71. variant<int, float> v3( 3.14f );
  72. v = std::move(v3);
  73. BOOST_TEST_EQ( v.index(), 1 );
  74. BOOST_TEST_EQ( get<1>(v), 3.14f );
  75. variant<int, float> v4( 3.15f );
  76. v = std::move(v4);
  77. BOOST_TEST_EQ( v.index(), 1 );
  78. BOOST_TEST_EQ( get<1>(v), 3.15f );
  79. }
  80. {
  81. variant<int, int, float, std::string> v;
  82. BOOST_TEST_EQ( v.index(), 0 );
  83. BOOST_TEST_EQ( get<0>(v), 0 );
  84. variant<int, int, float, std::string> v2( in_place_index_t<1>{}, 1 );
  85. v = std::move(v2);
  86. BOOST_TEST_EQ( v.index(), 1 );
  87. BOOST_TEST_EQ( get<1>(v), 1 );
  88. variant<int, int, float, std::string> v3( 3.14f );
  89. v = std::move(v3);
  90. BOOST_TEST_EQ( v.index(), 2 );
  91. BOOST_TEST_EQ( get<2>(v), 3.14f );
  92. variant<int, int, float, std::string> v4( 3.15f );
  93. v = std::move(v4);
  94. BOOST_TEST_EQ( v.index(), 2 );
  95. BOOST_TEST_EQ( get<2>(v), 3.15f );
  96. variant<int, int, float, std::string> v5( "s1" );
  97. v = std::move(v5);
  98. BOOST_TEST_EQ( v.index(), 3 );
  99. BOOST_TEST_EQ( get<3>(v), std::string("s1") );
  100. variant<int, int, float, std::string> v6( "s2" );
  101. v = std::move(v6);
  102. BOOST_TEST_EQ( v.index(), 3 );
  103. BOOST_TEST_EQ( get<3>(v), std::string("s2") );
  104. }
  105. {
  106. variant<X1, X2> v;
  107. BOOST_TEST_EQ( v.index(), 0 );
  108. BOOST_TEST_EQ( get<0>(v).v, 0 );
  109. variant<X1, X2> v2( X1{1} );
  110. v = std::move(v2);
  111. BOOST_TEST_EQ( v.index(), 0 );
  112. BOOST_TEST_EQ( get<0>(v).v, 1 );
  113. variant<X1, X2> v3( in_place_index_t<1>{}, 2 );
  114. v = std::move(v3);
  115. BOOST_TEST_EQ( v.index(), 1 );
  116. BOOST_TEST_EQ( get<1>(v).v, 2 );
  117. variant<X1, X2> v4( in_place_index_t<1>{}, 3 );
  118. v = std::move(v4);
  119. BOOST_TEST_EQ( v.index(), 1 );
  120. BOOST_TEST_EQ( get<1>(v).v, 3 );
  121. variant<X1, X2> v5( in_place_index_t<0>{}, 4 );
  122. v = std::move(v5);
  123. BOOST_TEST_EQ( v.index(), 0 );
  124. BOOST_TEST_EQ( get<0>(v).v, 4 );
  125. }
  126. {
  127. BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int>>));
  128. BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int>>));
  129. BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, float>>));
  130. BOOST_TEST_TRAIT_TRUE((std::is_nothrow_move_assignable<variant<int, int, float, float>>));
  131. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1>>));
  132. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int>>));
  133. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, int, float>>));
  134. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, X1>>));
  135. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<int, int, X1>>));
  136. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2>>));
  137. BOOST_TEST_TRAIT_FALSE((std::is_nothrow_move_assignable<variant<X1, X2, int, int>>));
  138. BOOST_TEST_TRAIT_TRUE((std::is_move_assignable<variant<X1, X2>>));
  139. BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int const>>));
  140. BOOST_TEST_TRAIT_FALSE((std::is_move_assignable<variant<int, float, Y>>));
  141. }
  142. return boost::report_errors();
  143. }