move.pass.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. // The following compilers don't generate constexpr special members correctly.
  12. // XFAIL: clang-3.5, clang-3.6, clang-3.7, clang-3.8
  13. // XFAIL: apple-clang-6, apple-clang-7, apple-clang-8.0
  14. // XFAIL: with_system_cxx_lib=macosx10.12
  15. // XFAIL: with_system_cxx_lib=macosx10.11
  16. // XFAIL: with_system_cxx_lib=macosx10.10
  17. // XFAIL: with_system_cxx_lib=macosx10.9
  18. // XFAIL: with_system_cxx_lib=macosx10.7
  19. // XFAIL: with_system_cxx_lib=macosx10.8
  20. // <variant>
  21. // template <class ...Types> class variant;
  22. // variant& operator=(variant&&) noexcept(see below);
  23. #include <cassert>
  24. #include <string>
  25. #include <type_traits>
  26. #include <utility>
  27. #include <variant>
  28. #include "test_macros.h"
  29. #include "variant_test_helpers.hpp"
  30. struct NoCopy {
  31. NoCopy(const NoCopy &) = delete;
  32. NoCopy &operator=(const NoCopy &) = default;
  33. };
  34. struct CopyOnly {
  35. CopyOnly(const CopyOnly &) = default;
  36. CopyOnly(CopyOnly &&) = delete;
  37. CopyOnly &operator=(const CopyOnly &) = default;
  38. CopyOnly &operator=(CopyOnly &&) = delete;
  39. };
  40. struct MoveOnly {
  41. MoveOnly(const MoveOnly &) = delete;
  42. MoveOnly(MoveOnly &&) = default;
  43. MoveOnly &operator=(const MoveOnly &) = delete;
  44. MoveOnly &operator=(MoveOnly &&) = default;
  45. };
  46. struct MoveOnlyNT {
  47. MoveOnlyNT(const MoveOnlyNT &) = delete;
  48. MoveOnlyNT(MoveOnlyNT &&) {}
  49. MoveOnlyNT &operator=(const MoveOnlyNT &) = delete;
  50. MoveOnlyNT &operator=(MoveOnlyNT &&) = default;
  51. };
  52. struct MoveOnlyOddNothrow {
  53. MoveOnlyOddNothrow(MoveOnlyOddNothrow &&) noexcept(false) {}
  54. MoveOnlyOddNothrow(const MoveOnlyOddNothrow &) = delete;
  55. MoveOnlyOddNothrow &operator=(MoveOnlyOddNothrow &&) noexcept = default;
  56. MoveOnlyOddNothrow &operator=(const MoveOnlyOddNothrow &) = delete;
  57. };
  58. struct MoveAssignOnly {
  59. MoveAssignOnly(MoveAssignOnly &&) = delete;
  60. MoveAssignOnly &operator=(MoveAssignOnly &&) = default;
  61. };
  62. struct MoveAssign {
  63. static int move_construct;
  64. static int move_assign;
  65. static void reset() { move_construct = move_assign = 0; }
  66. MoveAssign(int v) : value(v) {}
  67. MoveAssign(MoveAssign &&o) : value(o.value) {
  68. ++move_construct;
  69. o.value = -1;
  70. }
  71. MoveAssign &operator=(MoveAssign &&o) {
  72. value = o.value;
  73. ++move_assign;
  74. o.value = -1;
  75. return *this;
  76. }
  77. int value;
  78. };
  79. int MoveAssign::move_construct = 0;
  80. int MoveAssign::move_assign = 0;
  81. struct NTMoveAssign {
  82. constexpr NTMoveAssign(int v) : value(v) {}
  83. NTMoveAssign(const NTMoveAssign &) = default;
  84. NTMoveAssign(NTMoveAssign &&) = default;
  85. NTMoveAssign &operator=(const NTMoveAssign &that) = default;
  86. NTMoveAssign &operator=(NTMoveAssign &&that) {
  87. value = that.value;
  88. that.value = -1;
  89. return *this;
  90. };
  91. int value;
  92. };
  93. static_assert(!std::is_trivially_move_assignable<NTMoveAssign>::value, "");
  94. static_assert(std::is_move_assignable<NTMoveAssign>::value, "");
  95. struct TMoveAssign {
  96. constexpr TMoveAssign(int v) : value(v) {}
  97. TMoveAssign(const TMoveAssign &) = delete;
  98. TMoveAssign(TMoveAssign &&) = default;
  99. TMoveAssign &operator=(const TMoveAssign &) = delete;
  100. TMoveAssign &operator=(TMoveAssign &&) = default;
  101. int value;
  102. };
  103. static_assert(std::is_trivially_move_assignable<TMoveAssign>::value, "");
  104. struct TMoveAssignNTCopyAssign {
  105. constexpr TMoveAssignNTCopyAssign(int v) : value(v) {}
  106. TMoveAssignNTCopyAssign(const TMoveAssignNTCopyAssign &) = default;
  107. TMoveAssignNTCopyAssign(TMoveAssignNTCopyAssign &&) = default;
  108. TMoveAssignNTCopyAssign &operator=(const TMoveAssignNTCopyAssign &that) {
  109. value = that.value;
  110. return *this;
  111. }
  112. TMoveAssignNTCopyAssign &operator=(TMoveAssignNTCopyAssign &&) = default;
  113. int value;
  114. };
  115. static_assert(std::is_trivially_move_assignable_v<TMoveAssignNTCopyAssign>, "");
  116. struct TrivialCopyNontrivialMove {
  117. TrivialCopyNontrivialMove(TrivialCopyNontrivialMove const&) = default;
  118. TrivialCopyNontrivialMove(TrivialCopyNontrivialMove&&) noexcept {}
  119. TrivialCopyNontrivialMove& operator=(TrivialCopyNontrivialMove const&) = default;
  120. TrivialCopyNontrivialMove& operator=(TrivialCopyNontrivialMove&&) noexcept {
  121. return *this;
  122. }
  123. };
  124. static_assert(std::is_trivially_copy_assignable_v<TrivialCopyNontrivialMove>, "");
  125. static_assert(!std::is_trivially_move_assignable_v<TrivialCopyNontrivialMove>, "");
  126. void test_move_assignment_noexcept() {
  127. {
  128. using V = std::variant<int>;
  129. static_assert(std::is_nothrow_move_assignable<V>::value, "");
  130. }
  131. {
  132. using V = std::variant<MoveOnly>;
  133. static_assert(std::is_nothrow_move_assignable<V>::value, "");
  134. }
  135. {
  136. using V = std::variant<int, long>;
  137. static_assert(std::is_nothrow_move_assignable<V>::value, "");
  138. }
  139. {
  140. using V = std::variant<int, MoveOnly>;
  141. static_assert(std::is_nothrow_move_assignable<V>::value, "");
  142. }
  143. {
  144. using V = std::variant<MoveOnlyNT>;
  145. static_assert(!std::is_nothrow_move_assignable<V>::value, "");
  146. }
  147. {
  148. using V = std::variant<MoveOnlyOddNothrow>;
  149. static_assert(!std::is_nothrow_move_assignable<V>::value, "");
  150. }
  151. }
  152. void test_move_assignment_sfinae() {
  153. {
  154. using V = std::variant<int, long>;
  155. static_assert(std::is_move_assignable<V>::value, "");
  156. }
  157. {
  158. using V = std::variant<int, CopyOnly>;
  159. static_assert(std::is_move_assignable<V>::value, "");
  160. }
  161. {
  162. using V = std::variant<int, NoCopy>;
  163. static_assert(!std::is_move_assignable<V>::value, "");
  164. }
  165. {
  166. using V = std::variant<int, MoveOnly>;
  167. static_assert(std::is_move_assignable<V>::value, "");
  168. }
  169. {
  170. using V = std::variant<int, MoveOnlyNT>;
  171. static_assert(std::is_move_assignable<V>::value, "");
  172. }
  173. {
  174. // variant only provides move assignment when the types also provide
  175. // a move constructor.
  176. using V = std::variant<int, MoveAssignOnly>;
  177. static_assert(!std::is_move_assignable<V>::value, "");
  178. }
  179. // The following tests are for not-yet-standardized behavior (P0602):
  180. {
  181. using V = std::variant<int, long>;
  182. static_assert(std::is_trivially_move_assignable<V>::value, "");
  183. }
  184. {
  185. using V = std::variant<int, NTMoveAssign>;
  186. static_assert(!std::is_trivially_move_assignable<V>::value, "");
  187. static_assert(std::is_move_assignable<V>::value, "");
  188. }
  189. {
  190. using V = std::variant<int, TMoveAssign>;
  191. static_assert(std::is_trivially_move_assignable<V>::value, "");
  192. }
  193. {
  194. using V = std::variant<int, TMoveAssignNTCopyAssign>;
  195. static_assert(std::is_trivially_move_assignable<V>::value, "");
  196. }
  197. {
  198. using V = std::variant<int, TrivialCopyNontrivialMove>;
  199. static_assert(!std::is_trivially_move_assignable<V>::value, "");
  200. }
  201. {
  202. using V = std::variant<int, CopyOnly>;
  203. static_assert(std::is_trivially_move_assignable<V>::value, "");
  204. }
  205. }
  206. void test_move_assignment_empty_empty() {
  207. #ifndef TEST_HAS_NO_EXCEPTIONS
  208. using MET = MakeEmptyT;
  209. {
  210. using V = std::variant<int, long, MET>;
  211. V v1(std::in_place_index<0>);
  212. makeEmpty(v1);
  213. V v2(std::in_place_index<0>);
  214. makeEmpty(v2);
  215. V &vref = (v1 = std::move(v2));
  216. assert(&vref == &v1);
  217. assert(v1.valueless_by_exception());
  218. assert(v1.index() == std::variant_npos);
  219. }
  220. #endif // TEST_HAS_NO_EXCEPTIONS
  221. }
  222. void test_move_assignment_non_empty_empty() {
  223. #ifndef TEST_HAS_NO_EXCEPTIONS
  224. using MET = MakeEmptyT;
  225. {
  226. using V = std::variant<int, MET>;
  227. V v1(std::in_place_index<0>, 42);
  228. V v2(std::in_place_index<0>);
  229. makeEmpty(v2);
  230. V &vref = (v1 = std::move(v2));
  231. assert(&vref == &v1);
  232. assert(v1.valueless_by_exception());
  233. assert(v1.index() == std::variant_npos);
  234. }
  235. {
  236. using V = std::variant<int, MET, std::string>;
  237. V v1(std::in_place_index<2>, "hello");
  238. V v2(std::in_place_index<0>);
  239. makeEmpty(v2);
  240. V &vref = (v1 = std::move(v2));
  241. assert(&vref == &v1);
  242. assert(v1.valueless_by_exception());
  243. assert(v1.index() == std::variant_npos);
  244. }
  245. #endif // TEST_HAS_NO_EXCEPTIONS
  246. }
  247. void test_move_assignment_empty_non_empty() {
  248. #ifndef TEST_HAS_NO_EXCEPTIONS
  249. using MET = MakeEmptyT;
  250. {
  251. using V = std::variant<int, MET>;
  252. V v1(std::in_place_index<0>);
  253. makeEmpty(v1);
  254. V v2(std::in_place_index<0>, 42);
  255. V &vref = (v1 = std::move(v2));
  256. assert(&vref == &v1);
  257. assert(v1.index() == 0);
  258. assert(std::get<0>(v1) == 42);
  259. }
  260. {
  261. using V = std::variant<int, MET, std::string>;
  262. V v1(std::in_place_index<0>);
  263. makeEmpty(v1);
  264. V v2(std::in_place_type<std::string>, "hello");
  265. V &vref = (v1 = std::move(v2));
  266. assert(&vref == &v1);
  267. assert(v1.index() == 2);
  268. assert(std::get<2>(v1) == "hello");
  269. }
  270. #endif // TEST_HAS_NO_EXCEPTIONS
  271. }
  272. template <typename T> struct Result { size_t index; T value; };
  273. void test_move_assignment_same_index() {
  274. {
  275. using V = std::variant<int>;
  276. V v1(43);
  277. V v2(42);
  278. V &vref = (v1 = std::move(v2));
  279. assert(&vref == &v1);
  280. assert(v1.index() == 0);
  281. assert(std::get<0>(v1) == 42);
  282. }
  283. {
  284. using V = std::variant<int, long, unsigned>;
  285. V v1(43l);
  286. V v2(42l);
  287. V &vref = (v1 = std::move(v2));
  288. assert(&vref == &v1);
  289. assert(v1.index() == 1);
  290. assert(std::get<1>(v1) == 42);
  291. }
  292. {
  293. using V = std::variant<int, MoveAssign, unsigned>;
  294. V v1(std::in_place_type<MoveAssign>, 43);
  295. V v2(std::in_place_type<MoveAssign>, 42);
  296. MoveAssign::reset();
  297. V &vref = (v1 = std::move(v2));
  298. assert(&vref == &v1);
  299. assert(v1.index() == 1);
  300. assert(std::get<1>(v1).value == 42);
  301. assert(MoveAssign::move_construct == 0);
  302. assert(MoveAssign::move_assign == 1);
  303. }
  304. #ifndef TEST_HAS_NO_EXCEPTIONS
  305. using MET = MakeEmptyT;
  306. {
  307. using V = std::variant<int, MET, std::string>;
  308. V v1(std::in_place_type<MET>);
  309. MET &mref = std::get<1>(v1);
  310. V v2(std::in_place_type<MET>);
  311. try {
  312. v1 = std::move(v2);
  313. assert(false);
  314. } catch (...) {
  315. }
  316. assert(v1.index() == 1);
  317. assert(&std::get<1>(v1) == &mref);
  318. }
  319. #endif // TEST_HAS_NO_EXCEPTIONS
  320. // The following tests are for not-yet-standardized behavior (P0602):
  321. {
  322. struct {
  323. constexpr Result<int> operator()() const {
  324. using V = std::variant<int>;
  325. V v(43);
  326. V v2(42);
  327. v = std::move(v2);
  328. return {v.index(), std::get<0>(v)};
  329. }
  330. } test;
  331. constexpr auto result = test();
  332. static_assert(result.index == 0, "");
  333. static_assert(result.value == 42, "");
  334. }
  335. {
  336. struct {
  337. constexpr Result<long> operator()() const {
  338. using V = std::variant<int, long, unsigned>;
  339. V v(43l);
  340. V v2(42l);
  341. v = std::move(v2);
  342. return {v.index(), std::get<1>(v)};
  343. }
  344. } test;
  345. constexpr auto result = test();
  346. static_assert(result.index == 1, "");
  347. static_assert(result.value == 42l, "");
  348. }
  349. {
  350. struct {
  351. constexpr Result<int> operator()() const {
  352. using V = std::variant<int, TMoveAssign, unsigned>;
  353. V v(std::in_place_type<TMoveAssign>, 43);
  354. V v2(std::in_place_type<TMoveAssign>, 42);
  355. v = std::move(v2);
  356. return {v.index(), std::get<1>(v).value};
  357. }
  358. } test;
  359. constexpr auto result = test();
  360. static_assert(result.index == 1, "");
  361. static_assert(result.value == 42, "");
  362. }
  363. }
  364. void test_move_assignment_different_index() {
  365. {
  366. using V = std::variant<int, long, unsigned>;
  367. V v1(43);
  368. V v2(42l);
  369. V &vref = (v1 = std::move(v2));
  370. assert(&vref == &v1);
  371. assert(v1.index() == 1);
  372. assert(std::get<1>(v1) == 42);
  373. }
  374. {
  375. using V = std::variant<int, MoveAssign, unsigned>;
  376. V v1(std::in_place_type<unsigned>, 43);
  377. V v2(std::in_place_type<MoveAssign>, 42);
  378. MoveAssign::reset();
  379. V &vref = (v1 = std::move(v2));
  380. assert(&vref == &v1);
  381. assert(v1.index() == 1);
  382. assert(std::get<1>(v1).value == 42);
  383. assert(MoveAssign::move_construct == 1);
  384. assert(MoveAssign::move_assign == 0);
  385. }
  386. #ifndef TEST_HAS_NO_EXCEPTIONS
  387. using MET = MakeEmptyT;
  388. {
  389. using V = std::variant<int, MET, std::string>;
  390. V v1(std::in_place_type<int>);
  391. V v2(std::in_place_type<MET>);
  392. try {
  393. v1 = std::move(v2);
  394. assert(false);
  395. } catch (...) {
  396. }
  397. assert(v1.valueless_by_exception());
  398. assert(v1.index() == std::variant_npos);
  399. }
  400. {
  401. using V = std::variant<int, MET, std::string>;
  402. V v1(std::in_place_type<MET>);
  403. V v2(std::in_place_type<std::string>, "hello");
  404. V &vref = (v1 = std::move(v2));
  405. assert(&vref == &v1);
  406. assert(v1.index() == 2);
  407. assert(std::get<2>(v1) == "hello");
  408. }
  409. #endif // TEST_HAS_NO_EXCEPTIONS
  410. // The following tests are for not-yet-standardized behavior (P0602):
  411. {
  412. struct {
  413. constexpr Result<long> operator()() const {
  414. using V = std::variant<int, long, unsigned>;
  415. V v(43);
  416. V v2(42l);
  417. v = std::move(v2);
  418. return {v.index(), std::get<1>(v)};
  419. }
  420. } test;
  421. constexpr auto result = test();
  422. static_assert(result.index == 1, "");
  423. static_assert(result.value == 42l, "");
  424. }
  425. {
  426. struct {
  427. constexpr Result<long> operator()() const {
  428. using V = std::variant<int, TMoveAssign, unsigned>;
  429. V v(std::in_place_type<unsigned>, 43);
  430. V v2(std::in_place_type<TMoveAssign>, 42);
  431. v = std::move(v2);
  432. return {v.index(), std::get<1>(v).value};
  433. }
  434. } test;
  435. constexpr auto result = test();
  436. static_assert(result.index == 1, "");
  437. static_assert(result.value == 42, "");
  438. }
  439. }
  440. template <size_t NewIdx, class ValueType>
  441. constexpr bool test_constexpr_assign_extension_imp(
  442. std::variant<long, void*, int>&& v, ValueType&& new_value)
  443. {
  444. std::variant<long, void*, int> v2(
  445. std::forward<ValueType>(new_value));
  446. const auto cp = v2;
  447. v = std::move(v2);
  448. return v.index() == NewIdx &&
  449. std::get<NewIdx>(v) == std::get<NewIdx>(cp);
  450. }
  451. void test_constexpr_move_assignment_extension() {
  452. // The following tests are for not-yet-standardized behavior (P0602):
  453. using V = std::variant<long, void*, int>;
  454. static_assert(std::is_trivially_copyable<V>::value, "");
  455. static_assert(std::is_trivially_move_assignable<V>::value, "");
  456. static_assert(test_constexpr_assign_extension_imp<0>(V(42l), 101l), "");
  457. static_assert(test_constexpr_assign_extension_imp<0>(V(nullptr), 101l), "");
  458. static_assert(test_constexpr_assign_extension_imp<1>(V(42l), nullptr), "");
  459. static_assert(test_constexpr_assign_extension_imp<2>(V(42l), 101), "");
  460. }
  461. int main() {
  462. test_move_assignment_empty_empty();
  463. test_move_assignment_non_empty_empty();
  464. test_move_assignment_empty_non_empty();
  465. test_move_assignment_same_index();
  466. test_move_assignment_different_index();
  467. test_move_assignment_sfinae();
  468. test_move_assignment_noexcept();
  469. test_constexpr_move_assignment_extension();
  470. }