move.pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct ThrowsMove {
  20. ThrowsMove(ThrowsMove &&) noexcept(false) {}
  21. };
  22. struct NoCopy {
  23. NoCopy(NoCopy const &) = delete;
  24. };
  25. struct MoveOnly {
  26. int value;
  27. MoveOnly(int v) : value(v) {}
  28. MoveOnly(MoveOnly const &) = delete;
  29. MoveOnly(MoveOnly &&) = default;
  30. };
  31. struct MoveOnlyNT {
  32. int value;
  33. MoveOnlyNT(int v) : value(v) {}
  34. MoveOnlyNT(MoveOnlyNT const &) = delete;
  35. MoveOnlyNT(MoveOnlyNT &&other) : value(other.value) { other.value = -1; }
  36. };
  37. #ifndef TEST_HAS_NO_EXCEPTIONS
  38. struct MakeEmptyT {
  39. static int alive;
  40. MakeEmptyT() { ++alive; }
  41. MakeEmptyT(MakeEmptyT const &) {
  42. ++alive;
  43. // Don't throw from the copy constructor since variant's assignment
  44. // operator performs a copy before committing to the assignment.
  45. }
  46. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  47. MakeEmptyT &operator=(MakeEmptyT const &) { throw 42; }
  48. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  49. ~MakeEmptyT() { --alive; }
  50. };
  51. int MakeEmptyT::alive = 0;
  52. template <class Variant> void makeEmpty(Variant &v) {
  53. Variant v2(std::in_place_type<MakeEmptyT>);
  54. try {
  55. v = v2;
  56. assert(false);
  57. } catch (...) {
  58. assert(v.valueless_by_exception());
  59. }
  60. }
  61. #endif // TEST_HAS_NO_EXCEPTIONS
  62. void test_move_noexcept() {
  63. {
  64. using V = std::variant<int, long>;
  65. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  66. }
  67. {
  68. using V = std::variant<int, MoveOnly>;
  69. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  70. }
  71. {
  72. using V = std::variant<int, MoveOnlyNT>;
  73. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  74. }
  75. {
  76. using V = std::variant<int, ThrowsMove>;
  77. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  78. }
  79. }
  80. void test_move_ctor_sfinae() {
  81. {
  82. using V = std::variant<int, long>;
  83. static_assert(std::is_move_constructible<V>::value, "");
  84. }
  85. {
  86. using V = std::variant<int, MoveOnly>;
  87. static_assert(std::is_move_constructible<V>::value, "");
  88. }
  89. {
  90. using V = std::variant<int, MoveOnlyNT>;
  91. static_assert(std::is_move_constructible<V>::value, "");
  92. }
  93. {
  94. using V = std::variant<int, NoCopy>;
  95. static_assert(!std::is_move_constructible<V>::value, "");
  96. }
  97. }
  98. void test_move_ctor_basic() {
  99. {
  100. std::variant<int> v(std::in_place_index<0>, 42);
  101. std::variant<int> v2 = std::move(v);
  102. assert(v2.index() == 0);
  103. assert(std::get<0>(v2) == 42);
  104. }
  105. {
  106. std::variant<int, long> v(std::in_place_index<1>, 42);
  107. std::variant<int, long> v2 = std::move(v);
  108. assert(v2.index() == 1);
  109. assert(std::get<1>(v2) == 42);
  110. }
  111. {
  112. std::variant<MoveOnly> v(std::in_place_index<0>, 42);
  113. assert(v.index() == 0);
  114. std::variant<MoveOnly> v2(std::move(v));
  115. assert(v2.index() == 0);
  116. assert(std::get<0>(v2).value == 42);
  117. }
  118. {
  119. std::variant<int, MoveOnly> v(std::in_place_index<1>, 42);
  120. assert(v.index() == 1);
  121. std::variant<int, MoveOnly> v2(std::move(v));
  122. assert(v2.index() == 1);
  123. assert(std::get<1>(v2).value == 42);
  124. }
  125. {
  126. std::variant<MoveOnlyNT> v(std::in_place_index<0>, 42);
  127. assert(v.index() == 0);
  128. std::variant<MoveOnlyNT> v2(std::move(v));
  129. assert(v2.index() == 0);
  130. assert(std::get<0>(v).value == -1);
  131. assert(std::get<0>(v2).value == 42);
  132. }
  133. {
  134. std::variant<int, MoveOnlyNT> v(std::in_place_index<1>, 42);
  135. assert(v.index() == 1);
  136. std::variant<int, MoveOnlyNT> v2(std::move(v));
  137. assert(v2.index() == 1);
  138. assert(std::get<1>(v).value == -1);
  139. assert(std::get<1>(v2).value == 42);
  140. }
  141. }
  142. void test_move_ctor_valueless_by_exception() {
  143. #ifndef TEST_HAS_NO_EXCEPTIONS
  144. using V = std::variant<int, MakeEmptyT>;
  145. V v1;
  146. makeEmpty(v1);
  147. V v(std::move(v1));
  148. assert(v.valueless_by_exception());
  149. #endif
  150. }
  151. int main() {
  152. test_move_ctor_basic();
  153. test_move_ctor_valueless_by_exception();
  154. test_move_noexcept();
  155. test_move_ctor_sfinae();
  156. }