move.pass.cpp 9.8 KB

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