move.pass.cpp 5.8 KB

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