xref: /llvm-project/libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp (revision 5d73c7d1af78379827759dfb57887246e47a503e)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <variant>
12 
13 // template <class ...Types> class variant;
14 
15 // template <class T>
16 // variant& operator=(T&&) noexcept(see below);
17 
18 #include <cassert>
19 #include <string>
20 #include <type_traits>
21 #include <variant>
22 #include <vector>
23 #include <memory>
24 
25 #include "test_macros.h"
26 #include "variant_test_helpers.h"
27 
28 namespace MetaHelpers {
29 
30 struct Dummy {
31   Dummy() = default;
32 };
33 
34 struct ThrowsCtorT {
35   ThrowsCtorT(int) noexcept(false) {}
36   ThrowsCtorT &operator=(int) noexcept { return *this; }
37 };
38 
39 struct ThrowsAssignT {
40   ThrowsAssignT(int) noexcept {}
41   ThrowsAssignT &operator=(int) noexcept(false) { return *this; }
42 };
43 
44 struct NoThrowT {
45   NoThrowT(int) noexcept {}
46   NoThrowT &operator=(int) noexcept { return *this; }
47 };
48 
49 } // namespace MetaHelpers
50 
51 namespace RuntimeHelpers {
52 #ifndef TEST_HAS_NO_EXCEPTIONS
53 
54 struct ThrowsCtorT {
55   int value;
56   ThrowsCtorT() : value(0) {}
57   ThrowsCtorT(int) noexcept(false) { throw 42; }
58   ThrowsCtorT &operator=(int v) noexcept {
59     value = v;
60     return *this;
61   }
62 };
63 
64 struct MoveCrashes {
65   int value;
66   MoveCrashes(int v = 0) noexcept : value{v} {}
67   MoveCrashes(MoveCrashes &&) noexcept { assert(false); }
68   MoveCrashes &operator=(MoveCrashes &&) noexcept { assert(false); return *this; }
69   MoveCrashes &operator=(int v) noexcept {
70     value = v;
71     return *this;
72   }
73 };
74 
75 struct ThrowsCtorTandMove {
76   int value;
77   ThrowsCtorTandMove() : value(0) {}
78   ThrowsCtorTandMove(int) noexcept(false) { throw 42; }
79   ThrowsCtorTandMove(ThrowsCtorTandMove &&) noexcept(false) { assert(false); }
80   ThrowsCtorTandMove &operator=(int v) noexcept {
81     value = v;
82     return *this;
83   }
84 };
85 
86 struct ThrowsAssignT {
87   int value;
88   ThrowsAssignT() : value(0) {}
89   ThrowsAssignT(int v) noexcept : value(v) {}
90   ThrowsAssignT &operator=(int) noexcept(false) { throw 42; }
91 };
92 
93 struct NoThrowT {
94   int value;
95   NoThrowT() : value(0) {}
96   NoThrowT(int v) noexcept : value(v) {}
97   NoThrowT &operator=(int v) noexcept {
98     value = v;
99     return *this;
100   }
101 };
102 
103 #endif // !defined(TEST_HAS_NO_EXCEPTIONS)
104 } // namespace RuntimeHelpers
105 
106 void test_T_assignment_noexcept() {
107   using namespace MetaHelpers;
108   {
109     using V = std::variant<Dummy, NoThrowT>;
110     static_assert(std::is_nothrow_assignable<V, int>::value, "");
111   }
112   {
113     using V = std::variant<Dummy, ThrowsCtorT>;
114     static_assert(!std::is_nothrow_assignable<V, int>::value, "");
115   }
116   {
117     using V = std::variant<Dummy, ThrowsAssignT>;
118     static_assert(!std::is_nothrow_assignable<V, int>::value, "");
119   }
120 }
121 
122 void test_T_assignment_sfinae() {
123   {
124     using V = std::variant<long, long long>;
125     static_assert(!std::is_assignable<V, int>::value, "ambiguous");
126   }
127   {
128     using V = std::variant<std::string, std::string>;
129     static_assert(!std::is_assignable<V, const char *>::value, "ambiguous");
130   }
131   {
132     using V = std::variant<std::string, void *>;
133     static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
134   }
135   {
136     using V = std::variant<std::string, float>;
137     static_assert(std::is_assignable<V, int>::value == VariantAllowsNarrowingConversions,
138     "no matching operator=");
139   }
140   {
141     using V = std::variant<std::unique_ptr<int>, bool>;
142     static_assert(!std::is_assignable<V, std::unique_ptr<char>>::value,
143                   "no explicit bool in operator=");
144     struct X {
145       operator void*();
146     };
147     static_assert(!std::is_assignable<V, X>::value,
148                   "no boolean conversion in operator=");
149 #ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
150     static_assert(std::is_assignable<V, std::false_type>::value,
151                   "converted to bool in operator=");
152 #endif
153   }
154   {
155     struct X {};
156     struct Y {
157       operator X();
158     };
159     using V = std::variant<X>;
160     static_assert(std::is_assignable<V, Y>::value,
161                   "regression on user-defined conversions in operator=");
162   }
163 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
164   {
165     using V = std::variant<int, int &&>;
166     static_assert(!std::is_assignable<V, int>::value, "ambiguous");
167   }
168   {
169     using V = std::variant<int, const int &>;
170     static_assert(!std::is_assignable<V, int>::value, "ambiguous");
171   }
172 #endif // TEST_VARIANT_HAS_NO_REFERENCES
173 }
174 
175 void test_T_assignment_basic() {
176   {
177     std::variant<int> v(43);
178     v = 42;
179     assert(v.index() == 0);
180     assert(std::get<0>(v) == 42);
181   }
182   {
183     std::variant<int, long> v(43l);
184     v = 42;
185     assert(v.index() == 0);
186     assert(std::get<0>(v) == 42);
187     v = 43l;
188     assert(v.index() == 1);
189     assert(std::get<1>(v) == 43);
190   }
191 #ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
192   {
193     std::variant<unsigned, long> v;
194     v = 42;
195     assert(v.index() == 1);
196     assert(std::get<1>(v) == 42);
197     v = 43u;
198     assert(v.index() == 0);
199     assert(std::get<0>(v) == 43);
200   }
201 #endif
202   {
203     std::variant<std::string, bool> v = true;
204     v = "bar";
205     assert(v.index() == 0);
206     assert(std::get<0>(v) == "bar");
207   }
208   {
209     std::variant<bool, std::unique_ptr<int>> v;
210     v = nullptr;
211     assert(v.index() == 1);
212     assert(std::get<1>(v) == nullptr);
213   }
214 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
215   {
216     using V = std::variant<int &, int &&, long>;
217     int x = 42;
218     V v(43l);
219     v = x;
220     assert(v.index() == 0);
221     assert(&std::get<0>(v) == &x);
222     v = std::move(x);
223     assert(v.index() == 1);
224     assert(&std::get<1>(v) == &x);
225     // 'long' is selected by FUN(const int &) since 'const int &' cannot bind
226     // to 'int&'.
227     const int &cx = x;
228     v = cx;
229     assert(v.index() == 2);
230     assert(std::get<2>(v) == 42);
231   }
232 #endif // TEST_VARIANT_HAS_NO_REFERENCES
233 }
234 
235 void test_T_assignment_performs_construction() {
236   using namespace RuntimeHelpers;
237 #ifndef TEST_HAS_NO_EXCEPTIONS
238   {
239     using V = std::variant<std::string, ThrowsCtorT>;
240     V v(std::in_place_type<std::string>, "hello");
241     try {
242       v = 42;
243       assert(false);
244     } catch (...) { /* ... */
245     }
246     assert(v.index() == 0);
247     assert(std::get<0>(v) == "hello");
248   }
249   {
250     using V = std::variant<ThrowsAssignT, std::string>;
251     V v(std::in_place_type<std::string>, "hello");
252     v = 42;
253     assert(v.index() == 0);
254     assert(std::get<0>(v).value == 42);
255   }
256 #endif // TEST_HAS_NO_EXCEPTIONS
257 }
258 
259 void test_T_assignment_performs_assignment() {
260   using namespace RuntimeHelpers;
261 #ifndef TEST_HAS_NO_EXCEPTIONS
262   {
263     using V = std::variant<ThrowsCtorT>;
264     V v;
265     v = 42;
266     assert(v.index() == 0);
267     assert(std::get<0>(v).value == 42);
268   }
269   {
270     using V = std::variant<ThrowsCtorT, std::string>;
271     V v;
272     v = 42;
273     assert(v.index() == 0);
274     assert(std::get<0>(v).value == 42);
275   }
276   {
277     using V = std::variant<ThrowsAssignT>;
278     V v(100);
279     try {
280       v = 42;
281       assert(false);
282     } catch (...) { /* ... */
283     }
284     assert(v.index() == 0);
285     assert(std::get<0>(v).value == 100);
286   }
287   {
288     using V = std::variant<std::string, ThrowsAssignT>;
289     V v(100);
290     try {
291       v = 42;
292       assert(false);
293     } catch (...) { /* ... */
294     }
295     assert(v.index() == 1);
296     assert(std::get<1>(v).value == 100);
297   }
298 #endif // TEST_HAS_NO_EXCEPTIONS
299 }
300 
301 void test_T_assignment_vector_bool() {
302 #ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
303   std::vector<bool> vec = {true};
304   std::variant<bool, int> v;
305   v = vec[0];
306   assert(v.index() == 0);
307   assert(std::get<0>(v) == true);
308 #endif
309 }
310 
311 int main(int, char**) {
312   test_T_assignment_basic();
313   test_T_assignment_performs_construction();
314   test_T_assignment_performs_assignment();
315   test_T_assignment_noexcept();
316   test_T_assignment_sfinae();
317   test_T_assignment_vector_bool();
318 
319   return 0;
320 }
321