2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
15 // template <class ...Types> class variant;
17 // variant(variant&&) noexcept(see below);
19 #include <type_traits>
22 #include "test_macros.h"
25 constexpr NTMove(int v) : value(v) {}
26 NTMove(const NTMove &) = delete;
27 NTMove(NTMove &&that) : value(that.value) { that.value = -1; }
31 static_assert(!std::is_trivially_move_constructible<NTMove>::value, "");
32 static_assert(std::is_move_constructible<NTMove>::value, "");
35 constexpr TMove(int v) : value(v) {}
36 TMove(const TMove &) = delete;
37 TMove(TMove &&) = default;
41 static_assert(std::is_trivially_move_constructible<TMove>::value, "");
44 constexpr TMoveNTCopy(int v) : value(v) {}
45 TMoveNTCopy(const TMoveNTCopy& that) : value(that.value) {}
46 TMoveNTCopy(TMoveNTCopy&&) = default;
50 static_assert(std::is_trivially_move_constructible<TMoveNTCopy>::value, "");
52 void test_move_ctor_sfinae() {
54 using V = std::variant<int, long>;
55 static_assert(std::is_trivially_move_constructible<V>::value, "");
58 using V = std::variant<int, NTMove>;
59 static_assert(!std::is_trivially_move_constructible<V>::value, "");
60 static_assert(std::is_move_constructible<V>::value, "");
63 using V = std::variant<int, TMove>;
64 static_assert(std::is_trivially_move_constructible<V>::value, "");
67 using V = std::variant<int, TMoveNTCopy>;
68 static_assert(std::is_trivially_move_constructible<V>::value, "");
73 struct Result { size_t index; T value; };
75 void test_move_ctor_basic() {
78 constexpr Result<int> operator()() const {
79 std::variant<int> v(std::in_place_index<0>, 42);
80 std::variant<int> v2 = std::move(v);
81 return {v2.index(), std::get<0>(std::move(v2))};
84 constexpr auto result = test();
85 static_assert(result.index == 0);
86 static_assert(result.value == 42);
90 constexpr Result<long> operator()() const {
91 std::variant<int, long> v(std::in_place_index<1>, 42);
92 std::variant<int, long> v2 = std::move(v);
93 return {v2.index(), std::get<1>(std::move(v2))};
96 constexpr auto result = test();
97 static_assert(result.index == 1);
98 static_assert(result.value == 42);
102 constexpr Result<TMove> operator()() const {
103 std::variant<TMove> v(std::in_place_index<0>, 42);
104 std::variant<TMove> v2(std::move(v));
105 return {v2.index(), std::get<0>(std::move(v2))};
108 constexpr auto result = test();
109 static_assert(result.index == 0);
110 static_assert(result.value.value == 42);
114 constexpr Result<TMove> operator()() const {
115 std::variant<int, TMove> v(std::in_place_index<1>, 42);
116 std::variant<int, TMove> v2(std::move(v));
117 return {v2.index(), std::get<1>(std::move(v2))};
120 constexpr auto result = test();
121 static_assert(result.index == 1);
122 static_assert(result.value.value == 42);
126 constexpr Result<TMoveNTCopy> operator()() const {
127 std::variant<TMoveNTCopy> v(std::in_place_index<0>, 42);
128 std::variant<TMoveNTCopy> v2(std::move(v));
129 return {v2.index(), std::get<0>(std::move(v2))};
132 constexpr auto result = test();
133 static_assert(result.index == 0);
134 static_assert(result.value.value == 42);
138 constexpr Result<TMoveNTCopy> operator()() const {
139 std::variant<int, TMoveNTCopy> v(std::in_place_index<1>, 42);
140 std::variant<int, TMoveNTCopy> v2(std::move(v));
141 return {v2.index(), std::get<1>(std::move(v2))};
144 constexpr auto result = test();
145 static_assert(result.index == 1);
146 static_assert(result.value.value == 42);
151 test_move_ctor_basic();
152 test_move_ctor_sfinae();