move.pass.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: dylib-has-no-bad_variant_access
  11. // <variant>
  12. // template <class ...Types> class variant;
  13. // variant(variant&&) noexcept(see below); // constexpr in C++20
  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. struct NTMove {
  39. constexpr NTMove(int v) : value(v) {}
  40. NTMove(const NTMove &) = delete;
  41. NTMove(NTMove &&that) : value(that.value) { that.value = -1; }
  42. int value;
  43. };
  44. static_assert(!std::is_trivially_move_constructible<NTMove>::value, "");
  45. static_assert(std::is_move_constructible<NTMove>::value, "");
  46. struct TMove {
  47. constexpr TMove(int v) : value(v) {}
  48. TMove(const TMove &) = delete;
  49. TMove(TMove &&) = default;
  50. int value;
  51. };
  52. static_assert(std::is_trivially_move_constructible<TMove>::value, "");
  53. struct TMoveNTCopy {
  54. constexpr TMoveNTCopy(int v) : value(v) {}
  55. TMoveNTCopy(const TMoveNTCopy& that) : value(that.value) {}
  56. TMoveNTCopy(TMoveNTCopy&&) = default;
  57. int value;
  58. };
  59. static_assert(std::is_trivially_move_constructible<TMoveNTCopy>::value, "");
  60. #ifndef TEST_HAS_NO_EXCEPTIONS
  61. struct MakeEmptyT {
  62. static int alive;
  63. MakeEmptyT() { ++alive; }
  64. MakeEmptyT(const MakeEmptyT &) {
  65. ++alive;
  66. // Don't throw from the copy constructor since variant's assignment
  67. // operator performs a copy before committing to the assignment.
  68. }
  69. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  70. MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
  71. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  72. ~MakeEmptyT() { --alive; }
  73. };
  74. int MakeEmptyT::alive = 0;
  75. template <class Variant> void makeEmpty(Variant &v) {
  76. Variant v2(std::in_place_type<MakeEmptyT>);
  77. try {
  78. v = std::move(v2);
  79. assert(false);
  80. } catch (...) {
  81. assert(v.valueless_by_exception());
  82. }
  83. }
  84. #endif // TEST_HAS_NO_EXCEPTIONS
  85. void test_move_noexcept() {
  86. {
  87. using V = std::variant<int, long>;
  88. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  89. }
  90. {
  91. using V = std::variant<int, MoveOnly>;
  92. static_assert(std::is_nothrow_move_constructible<V>::value, "");
  93. }
  94. {
  95. using V = std::variant<int, MoveOnlyNT>;
  96. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  97. }
  98. {
  99. using V = std::variant<int, ThrowsMove>;
  100. static_assert(!std::is_nothrow_move_constructible<V>::value, "");
  101. }
  102. }
  103. void test_move_ctor_sfinae() {
  104. {
  105. using V = std::variant<int, long>;
  106. static_assert(std::is_move_constructible<V>::value, "");
  107. }
  108. {
  109. using V = std::variant<int, MoveOnly>;
  110. static_assert(std::is_move_constructible<V>::value, "");
  111. }
  112. {
  113. using V = std::variant<int, MoveOnlyNT>;
  114. static_assert(std::is_move_constructible<V>::value, "");
  115. }
  116. {
  117. using V = std::variant<int, NoCopy>;
  118. static_assert(!std::is_move_constructible<V>::value, "");
  119. }
  120. // Make sure we properly propagate triviality (see P0602R4).
  121. #if TEST_STD_VER > 17
  122. {
  123. using V = std::variant<int, long>;
  124. static_assert(std::is_trivially_move_constructible<V>::value, "");
  125. }
  126. {
  127. using V = std::variant<int, NTMove>;
  128. static_assert(!std::is_trivially_move_constructible<V>::value, "");
  129. static_assert(std::is_move_constructible<V>::value, "");
  130. }
  131. {
  132. using V = std::variant<int, TMove>;
  133. static_assert(std::is_trivially_move_constructible<V>::value, "");
  134. }
  135. {
  136. using V = std::variant<int, TMoveNTCopy>;
  137. static_assert(std::is_trivially_move_constructible<V>::value, "");
  138. }
  139. #endif // > C++17
  140. }
  141. template <typename T>
  142. struct Result { size_t index; T value; };
  143. void test_move_ctor_basic() {
  144. {
  145. std::variant<int> v(std::in_place_index<0>, 42);
  146. std::variant<int> v2 = std::move(v);
  147. assert(v2.index() == 0);
  148. assert(std::get<0>(v2) == 42);
  149. }
  150. {
  151. std::variant<int, long> v(std::in_place_index<1>, 42);
  152. std::variant<int, long> v2 = std::move(v);
  153. assert(v2.index() == 1);
  154. assert(std::get<1>(v2) == 42);
  155. }
  156. {
  157. std::variant<MoveOnly> v(std::in_place_index<0>, 42);
  158. assert(v.index() == 0);
  159. std::variant<MoveOnly> v2(std::move(v));
  160. assert(v2.index() == 0);
  161. assert(std::get<0>(v2).value == 42);
  162. }
  163. {
  164. std::variant<int, MoveOnly> v(std::in_place_index<1>, 42);
  165. assert(v.index() == 1);
  166. std::variant<int, MoveOnly> v2(std::move(v));
  167. assert(v2.index() == 1);
  168. assert(std::get<1>(v2).value == 42);
  169. }
  170. {
  171. std::variant<MoveOnlyNT> v(std::in_place_index<0>, 42);
  172. assert(v.index() == 0);
  173. std::variant<MoveOnlyNT> v2(std::move(v));
  174. assert(v2.index() == 0);
  175. assert(std::get<0>(v).value == -1);
  176. assert(std::get<0>(v2).value == 42);
  177. }
  178. {
  179. std::variant<int, MoveOnlyNT> v(std::in_place_index<1>, 42);
  180. assert(v.index() == 1);
  181. std::variant<int, MoveOnlyNT> v2(std::move(v));
  182. assert(v2.index() == 1);
  183. assert(std::get<1>(v).value == -1);
  184. assert(std::get<1>(v2).value == 42);
  185. }
  186. // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
  187. #if TEST_STD_VER > 17
  188. {
  189. struct {
  190. constexpr Result<int> operator()() const {
  191. std::variant<int> v(std::in_place_index<0>, 42);
  192. std::variant<int> v2 = std::move(v);
  193. return {v2.index(), std::get<0>(std::move(v2))};
  194. }
  195. } test;
  196. constexpr auto result = test();
  197. static_assert(result.index == 0, "");
  198. static_assert(result.value == 42, "");
  199. }
  200. {
  201. struct {
  202. constexpr Result<long> operator()() const {
  203. std::variant<int, long> v(std::in_place_index<1>, 42);
  204. std::variant<int, long> v2 = std::move(v);
  205. return {v2.index(), std::get<1>(std::move(v2))};
  206. }
  207. } test;
  208. constexpr auto result = test();
  209. static_assert(result.index == 1, "");
  210. static_assert(result.value == 42, "");
  211. }
  212. {
  213. struct {
  214. constexpr Result<TMove> operator()() const {
  215. std::variant<TMove> v(std::in_place_index<0>, 42);
  216. std::variant<TMove> v2(std::move(v));
  217. return {v2.index(), std::get<0>(std::move(v2))};
  218. }
  219. } test;
  220. constexpr auto result = test();
  221. static_assert(result.index == 0, "");
  222. static_assert(result.value.value == 42, "");
  223. }
  224. {
  225. struct {
  226. constexpr Result<TMove> operator()() const {
  227. std::variant<int, TMove> v(std::in_place_index<1>, 42);
  228. std::variant<int, TMove> v2(std::move(v));
  229. return {v2.index(), std::get<1>(std::move(v2))};
  230. }
  231. } test;
  232. constexpr auto result = test();
  233. static_assert(result.index == 1, "");
  234. static_assert(result.value.value == 42, "");
  235. }
  236. {
  237. struct {
  238. constexpr Result<TMoveNTCopy> operator()() const {
  239. std::variant<TMoveNTCopy> v(std::in_place_index<0>, 42);
  240. std::variant<TMoveNTCopy> v2(std::move(v));
  241. return {v2.index(), std::get<0>(std::move(v2))};
  242. }
  243. } test;
  244. constexpr auto result = test();
  245. static_assert(result.index == 0, "");
  246. static_assert(result.value.value == 42, "");
  247. }
  248. {
  249. struct {
  250. constexpr Result<TMoveNTCopy> operator()() const {
  251. std::variant<int, TMoveNTCopy> v(std::in_place_index<1>, 42);
  252. std::variant<int, TMoveNTCopy> v2(std::move(v));
  253. return {v2.index(), std::get<1>(std::move(v2))};
  254. }
  255. } test;
  256. constexpr auto result = test();
  257. static_assert(result.index == 1, "");
  258. static_assert(result.value.value == 42, "");
  259. }
  260. #endif // > C++17
  261. }
  262. void test_move_ctor_valueless_by_exception() {
  263. #ifndef TEST_HAS_NO_EXCEPTIONS
  264. using V = std::variant<int, MakeEmptyT>;
  265. V v1;
  266. makeEmpty(v1);
  267. V v(std::move(v1));
  268. assert(v.valueless_by_exception());
  269. #endif // TEST_HAS_NO_EXCEPTIONS
  270. }
  271. template <size_t Idx>
  272. constexpr bool test_constexpr_ctor_imp(std::variant<long, void*, const int> const& v) {
  273. auto copy = v;
  274. auto v2 = std::move(copy);
  275. return v2.index() == v.index() &&
  276. v2.index() == Idx &&
  277. std::get<Idx>(v2) == std::get<Idx>(v);
  278. }
  279. void test_constexpr_move_ctor() {
  280. // Make sure we properly propagate triviality, which implies constexpr-ness (see P0602R4).
  281. #if TEST_STD_VER > 17
  282. using V = std::variant<long, void*, const int>;
  283. #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  284. static_assert(std::is_trivially_destructible<V>::value, "");
  285. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  286. static_assert(std::is_trivially_move_constructible<V>::value, "");
  287. static_assert(!std::is_copy_assignable<V>::value, "");
  288. static_assert(!std::is_move_assignable<V>::value, "");
  289. #else // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  290. static_assert(std::is_trivially_copyable<V>::value, "");
  291. #endif // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  292. static_assert(std::is_trivially_move_constructible<V>::value, "");
  293. static_assert(test_constexpr_ctor_imp<0>(V(42l)), "");
  294. static_assert(test_constexpr_ctor_imp<1>(V(nullptr)), "");
  295. static_assert(test_constexpr_ctor_imp<2>(V(101)), "");
  296. #endif // > C++17
  297. }
  298. int main(int, char**) {
  299. test_move_ctor_basic();
  300. test_move_ctor_valueless_by_exception();
  301. test_move_noexcept();
  302. test_move_ctor_sfinae();
  303. test_constexpr_move_ctor();
  304. return 0;
  305. }