move.pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // <variant>
  12. // template <class ...Types> class variant;
  13. // variant(variant&&) noexcept(see below);
  14. #include <cassert>
  15. #include <string>
  16. #include <type_traits>
  17. #include <variant>
  18. #include "test_macros.h"
  19. #include "test_workarounds.h"
  20. struct ThrowsMove {
  21. ThrowsMove(ThrowsMove &&) noexcept(false) {}
  22. };
  23. struct NoCopy {
  24. NoCopy(const NoCopy &) = delete;
  25. };
  26. struct MoveOnly {
  27. int value;
  28. MoveOnly(int v) : value(v) {}
  29. MoveOnly(const MoveOnly &) = delete;
  30. MoveOnly(MoveOnly &&) = default;
  31. };
  32. struct MoveOnlyNT {
  33. int value;
  34. MoveOnlyNT(int v) : value(v) {}
  35. MoveOnlyNT(const MoveOnlyNT &) = delete;
  36. MoveOnlyNT(MoveOnlyNT &&other) : value(other.value) { other.value = -1; }
  37. };
  38. #ifndef TEST_HAS_NO_EXCEPTIONS
  39. struct MakeEmptyT {
  40. static int alive;
  41. MakeEmptyT() { ++alive; }
  42. MakeEmptyT(const MakeEmptyT &) {
  43. ++alive;
  44. // Don't throw from the copy constructor since variant's assignment
  45. // operator performs a copy before committing to the assignment.
  46. }
  47. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  48. MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
  49. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  50. ~MakeEmptyT() { --alive; }
  51. };
  52. int MakeEmptyT::alive = 0;
  53. template <class Variant> void makeEmpty(Variant &v) {
  54. Variant v2(std::in_place_type<MakeEmptyT>);
  55. try {
  56. v = v2;
  57. assert(false);
  58. } catch (...) {
  59. assert(v.valueless_by_exception());
  60. }
  61. }
  62. #endif // TEST_HAS_NO_EXCEPTIONS
  63. void test_move_noexcept() {
  64. {
  65. using V = std::variant<int, long>;
  66. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  67. }
  68. {
  69. using V = std::variant<int, MoveOnly>;
  70. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  71. }
  72. {
  73. using V = std::variant<int, MoveOnlyNT>;
  74. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  75. }
  76. {
  77. using V = std::variant<int, ThrowsMove>;
  78. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  79. }
  80. }
  81. void test_move_ctor_sfinae() {
  82. {
  83. using V = std::variant<int, long>;
  84. static_assert(std::is_move_constructible<V>::value, "");
  85. }
  86. {
  87. using V = std::variant<int, MoveOnly>;
  88. static_assert(std::is_move_constructible<V>::value, "");
  89. }
  90. {
  91. using V = std::variant<int, MoveOnlyNT>;
  92. static_assert(std::is_move_constructible<V>::value, "");
  93. }
  94. {
  95. using V = std::variant<int, NoCopy>;
  96. static_assert(!std::is_move_constructible<V>::value, "");
  97. }
  98. }
  99. void test_move_ctor_basic() {
  100. {
  101. std::variant<int> v(std::in_place_index<0>, 42);
  102. std::variant<int> v2 = std::move(v);
  103. assert(v2.index() == 0);
  104. assert(std::get<0>(v2) == 42);
  105. }
  106. {
  107. std::variant<int, long> v(std::in_place_index<1>, 42);
  108. std::variant<int, long> v2 = std::move(v);
  109. assert(v2.index() == 1);
  110. assert(std::get<1>(v2) == 42);
  111. }
  112. {
  113. std::variant<MoveOnly> v(std::in_place_index<0>, 42);
  114. assert(v.index() == 0);
  115. std::variant<MoveOnly> v2(std::move(v));
  116. assert(v2.index() == 0);
  117. assert(std::get<0>(v2).value == 42);
  118. }
  119. {
  120. std::variant<int, MoveOnly> v(std::in_place_index<1>, 42);
  121. assert(v.index() == 1);
  122. std::variant<int, MoveOnly> v2(std::move(v));
  123. assert(v2.index() == 1);
  124. assert(std::get<1>(v2).value == 42);
  125. }
  126. {
  127. std::variant<MoveOnlyNT> v(std::in_place_index<0>, 42);
  128. assert(v.index() == 0);
  129. std::variant<MoveOnlyNT> v2(std::move(v));
  130. assert(v2.index() == 0);
  131. assert(std::get<0>(v).value == -1);
  132. assert(std::get<0>(v2).value == 42);
  133. }
  134. {
  135. std::variant<int, MoveOnlyNT> v(std::in_place_index<1>, 42);
  136. assert(v.index() == 1);
  137. std::variant<int, MoveOnlyNT> v2(std::move(v));
  138. assert(v2.index() == 1);
  139. assert(std::get<1>(v).value == -1);
  140. assert(std::get<1>(v2).value == 42);
  141. }
  142. }
  143. void test_move_ctor_valueless_by_exception() {
  144. #ifndef TEST_HAS_NO_EXCEPTIONS
  145. using V = std::variant<int, MakeEmptyT>;
  146. V v1;
  147. makeEmpty(v1);
  148. V v(std::move(v1));
  149. assert(v.valueless_by_exception());
  150. #endif
  151. }
  152. template <size_t Idx>
  153. constexpr bool test_constexpr_ctor_extension_imp(
  154. std::variant<long, void*, const int> const& v)
  155. {
  156. auto copy = v;
  157. auto v2 = std::move(copy);
  158. return v2.index() == v.index() &&
  159. v2.index() == Idx &&
  160. std::get<Idx>(v2) == std::get<Idx>(v);
  161. }
  162. void test_constexpr_move_ctor_extension() {
  163. #if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER)
  164. using V = std::variant<long, void*, const int>;
  165. #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  166. static_assert(std::is_trivially_destructible<V>::value, "");
  167. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  168. static_assert(std::is_trivially_move_constructible<V>::value, "");
  169. static_assert(!std::is_copy_assignable<V>::value, "");
  170. static_assert(!std::is_move_assignable<V>::value, "");
  171. #else
  172. static_assert(std::is_trivially_copyable<V>::value, "");
  173. #endif
  174. static_assert(std::is_trivially_move_constructible<V>::value, "");
  175. static_assert(test_constexpr_ctor_extension_imp<0>(V(42l)), "");
  176. static_assert(test_constexpr_ctor_extension_imp<1>(V(nullptr)), "");
  177. static_assert(test_constexpr_ctor_extension_imp<2>(V(101)), "");
  178. #endif
  179. }
  180. int main() {
  181. test_move_ctor_basic();
  182. test_move_ctor_valueless_by_exception();
  183. test_move_noexcept();
  184. test_move_ctor_sfinae();
  185. test_constexpr_move_ctor_extension();
  186. }