PR libstdc++/86874 fix std::variant::swap regression
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / variant / compile.cc
blob0b8eaf04b0d0c53a8b295a2ce5f663134f339ef1
1 // { dg-options "-std=gnu++17" }
2 // { dg-do compile }
4 // Copyright (C) 2016-2018 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include <variant>
22 #include <string>
23 #include <vector>
25 using namespace std;
27 struct AllDeleted
29 AllDeleted() = delete;
30 AllDeleted(const AllDeleted&) = delete;
31 AllDeleted(AllDeleted&&) = delete;
32 AllDeleted& operator=(const AllDeleted&) = delete;
33 AllDeleted& operator=(AllDeleted&&) = delete;
36 struct Empty
38 Empty() { };
39 Empty(const Empty&) { };
40 Empty(Empty&&) { };
41 Empty& operator=(const Empty&) { return *this; };
42 Empty& operator=(Empty&&) { return *this; };
45 struct DefaultNoexcept
47 DefaultNoexcept() noexcept = default;
48 DefaultNoexcept(const DefaultNoexcept&) noexcept = default;
49 DefaultNoexcept(DefaultNoexcept&&) noexcept = default;
50 DefaultNoexcept& operator=(const DefaultNoexcept&) noexcept = default;
51 DefaultNoexcept& operator=(DefaultNoexcept&&) noexcept = default;
54 struct MoveCtorOnly
56 MoveCtorOnly() noexcept = delete;
57 MoveCtorOnly(const DefaultNoexcept&) noexcept = delete;
58 MoveCtorOnly(DefaultNoexcept&&) noexcept { }
59 MoveCtorOnly& operator=(const DefaultNoexcept&) noexcept = delete;
60 MoveCtorOnly& operator=(DefaultNoexcept&&) noexcept = delete;
63 struct nonliteral
65 nonliteral() { }
67 bool operator<(const nonliteral&) const;
68 bool operator<=(const nonliteral&) const;
69 bool operator==(const nonliteral&) const;
70 bool operator!=(const nonliteral&) const;
71 bool operator>=(const nonliteral&) const;
72 bool operator>(const nonliteral&) const;
75 void default_ctor()
77 static_assert(is_default_constructible_v<variant<int, string>>, "");
78 static_assert(is_default_constructible_v<variant<string, string>>, "");
79 static_assert(!is_default_constructible_v<variant<AllDeleted, string>>, "");
80 static_assert(is_default_constructible_v<variant<string, AllDeleted>>, "");
82 static_assert(noexcept(variant<int>()), "");
83 static_assert(!noexcept(variant<Empty>()), "");
84 static_assert(noexcept(variant<DefaultNoexcept>()), "");
87 void copy_ctor()
89 static_assert(is_copy_constructible_v<variant<int, string>>, "");
90 static_assert(!is_copy_constructible_v<variant<AllDeleted, string>>, "");
91 static_assert(is_trivially_copy_constructible_v<variant<int>>, "");
92 static_assert(!is_trivially_copy_constructible_v<variant<std::string>>, "");
95 variant<int> a;
96 static_assert(noexcept(variant<int>(a)), "");
99 variant<string> a;
100 static_assert(!noexcept(variant<string>(a)), "");
103 variant<int, string> a;
104 static_assert(!noexcept(variant<int, string>(a)), "");
107 variant<int, char> a;
108 static_assert(noexcept(variant<int, char>(a)), "");
112 void move_ctor()
114 static_assert(is_move_constructible_v<variant<int, string>>, "");
115 static_assert(!is_move_constructible_v<variant<AllDeleted, string>>, "");
116 static_assert(is_trivially_move_constructible_v<variant<int>>, "");
117 static_assert(!is_trivially_move_constructible_v<variant<std::string>>, "");
118 static_assert(!noexcept(variant<int, Empty>(declval<variant<int, Empty>>())), "");
119 static_assert(noexcept(variant<int, DefaultNoexcept>(declval<variant<int, DefaultNoexcept>>())), "");
122 void arbitrary_ctor()
124 static_assert(!is_constructible_v<variant<string, string>, const char*>, "");
125 static_assert(is_constructible_v<variant<int, string>, const char*>, "");
126 static_assert(noexcept(variant<int, Empty>(int{})), "");
127 static_assert(noexcept(variant<int, DefaultNoexcept>(int{})), "");
128 static_assert(!noexcept(variant<int, Empty>(Empty{})), "");
129 static_assert(noexcept(variant<int, DefaultNoexcept>(DefaultNoexcept{})), "");
132 void in_place_index_ctor()
134 variant<string, string> a(in_place_index<0>, "a");
135 variant<string, string> b(in_place_index<1>, {'a'});
138 void in_place_type_ctor()
140 variant<int, string, int> a(in_place_type<string>, "a");
141 variant<int, string, int> b(in_place_type<string>, {'a'});
142 static_assert(!is_constructible_v<variant<string, string>, in_place_type_t<string>, const char*>, "");
145 void dtor()
147 static_assert(is_destructible_v<variant<int, string>>, "");
148 static_assert(is_destructible_v<variant<AllDeleted, string>>, "");
151 void copy_assign()
153 static_assert(is_copy_assignable_v<variant<int, string>>, "");
154 static_assert(!is_copy_assignable_v<variant<AllDeleted, string>>, "");
155 static_assert(is_trivially_copy_assignable_v<variant<int>>, "");
156 static_assert(!is_trivially_copy_assignable_v<variant<string>>, "");
158 variant<Empty> a;
159 static_assert(!noexcept(a = a), "");
162 variant<DefaultNoexcept> a;
163 static_assert(noexcept(a = a), "");
167 void move_assign()
169 static_assert(is_move_assignable_v<variant<int, string>>, "");
170 static_assert(!is_move_assignable_v<variant<AllDeleted, string>>, "");
171 static_assert(is_trivially_move_assignable_v<variant<int>>, "");
172 static_assert(!is_trivially_move_assignable_v<variant<string>>, "");
174 variant<Empty> a;
175 static_assert(!noexcept(a = std::move(a)), "");
178 variant<DefaultNoexcept> a;
179 static_assert(noexcept(a = std::move(a)), "");
183 void arbitrary_assign()
185 static_assert(!is_assignable_v<variant<string, string>, const char*>, "");
186 static_assert(is_assignable_v<variant<int, string>, const char*>, "");
187 static_assert(noexcept(variant<int, Empty>() = int{}), "");
188 static_assert(noexcept(variant<int, DefaultNoexcept>() = int{}), "");
189 static_assert(!noexcept(variant<int, Empty>() = Empty{}), "");
190 static_assert(noexcept(variant<int, DefaultNoexcept>() = DefaultNoexcept{}), "");
193 void test_get()
195 static_assert(is_same<decltype(get<0>(variant<int, string>())), int&&>::value, "");
196 static_assert(is_same<decltype(get<1>(variant<int, string>())), string&&>::value, "");
197 static_assert(is_same<decltype(get<1>(variant<int, const string>())), const string&&>::value, "");
199 static_assert(is_same<decltype(get<int>(variant<int, string>())), int&&>::value, "");
200 static_assert(is_same<decltype(get<string>(variant<int, string>())), string&&>::value, "");
201 static_assert(is_same<decltype(get<const string>(variant<int, const string>())), const string&&>::value, "");
204 void test_relational()
207 constexpr variant<int, nonliteral> a(42), b(43);
208 static_assert((a < b), "");
209 static_assert(!(a > b), "");
210 static_assert((a <= b), "");
211 static_assert(!(a == b), "");
212 static_assert((a != b), "");
213 static_assert(!(a >= b), "");
216 constexpr variant<int, nonliteral> a(42), b(42);
217 static_assert(!(a < b), "");
218 static_assert(!(a > b), "");
219 static_assert((a <= b), "");
220 static_assert((a == b), "");
221 static_assert(!(a != b), "");
222 static_assert((a >= b), "");
225 constexpr variant<int, nonliteral> a(43), b(42);
226 static_assert(!(a < b), "");
227 static_assert((a > b), "");
228 static_assert(!(a <= b), "");
229 static_assert(!(a == b), "");
230 static_assert((a != b), "");
231 static_assert((a >= b), "");
234 constexpr monostate a, b;
235 static_assert(!(a < b), "");
236 static_assert(!(a > b), "");
237 static_assert((a <= b), "");
238 static_assert((a == b), "");
239 static_assert(!(a != b), "");
240 static_assert((a >= b), "");
244 // Not swappable, and variant<C> not swappable via the generic std::swap.
245 struct C { };
246 void swap(C&, C&) = delete;
248 static_assert( !std::is_swappable_v<variant<C>> );
249 static_assert( !std::is_swappable_v<variant<int, C>> );
250 static_assert( !std::is_swappable_v<variant<C, int>> );
252 // Not swappable, and variant<D> not swappable via the generic std::swap.
253 struct D { D(D&&) = delete; };
255 static_assert( !std::is_swappable_v<variant<D>> );
256 static_assert( !std::is_swappable_v<variant<int, D>> );
257 static_assert( !std::is_swappable_v<variant<D, int>> );
259 void test_swap()
261 static_assert(is_swappable_v<variant<int, string>>, "");
262 static_assert(is_swappable_v<variant<MoveCtorOnly>>, "");
263 static_assert(!is_swappable_v<variant<AllDeleted>>, "");
266 void test_visit()
269 struct Visitor
271 void operator()(monostate) {}
272 void operator()(const int&) {}
274 struct CVisitor
276 void operator()(monostate) const {}
277 void operator()(const int&) const {}
281 struct Visitor
283 bool operator()(int, float) { return false; }
284 bool operator()(int, double) { return false; }
285 bool operator()(char, float) { return false; }
286 bool operator()(char, double) { return false; }
288 visit(Visitor(), variant<int, char>(), variant<float, double>());
291 struct Visitor
293 constexpr bool operator()(const int&) { return true; }
294 constexpr bool operator()(const nonliteral&) { return false; }
296 static_assert(visit(Visitor(), variant<int, nonliteral>(0)), "");
299 struct Visitor
301 constexpr bool operator()(const int&) { return true; }
302 constexpr bool operator()(const nonliteral&) { return false; }
304 static_assert(visit(Visitor(), variant<int, nonliteral>(0)), "");
306 // PR libstdc++/79513
308 std::variant<int> v [[gnu::unused]] (5);
309 std::visit([](int&){}, v);
310 std::visit([](int&&){}, std::move(v));
314 void test_constexpr()
316 constexpr variant<int> a;
317 static_assert(holds_alternative<int>(a), "");
318 constexpr variant<int, char> b(in_place_index<0>, int{});
319 static_assert(holds_alternative<int>(b), "");
320 constexpr variant<int, char> c(in_place_type<int>, int{});
321 static_assert(holds_alternative<int>(c), "");
322 constexpr variant<int, char> d(in_place_index<1>, char{});
323 static_assert(holds_alternative<char>(d), "");
324 constexpr variant<int, char> e(in_place_type<char>, char{});
325 static_assert(holds_alternative<char>(e), "");
326 constexpr variant<int, char> f(char{});
327 static_assert(holds_alternative<char>(f), "");
330 struct literal {
331 constexpr literal() = default;
334 constexpr variant<literal, nonliteral> v{};
335 constexpr variant<literal, nonliteral> v1{in_place_type<literal>};
336 constexpr variant<literal, nonliteral> v2{in_place_index<0>};
340 constexpr variant<int> a(42);
341 static_assert(get<0>(a) == 42, "");
344 constexpr variant<int, nonliteral> a(42);
345 static_assert(get<0>(a) == 42, "");
348 constexpr variant<nonliteral, int> a(42);
349 static_assert(get<1>(a) == 42, "");
352 constexpr variant<int> a(42);
353 static_assert(get<int>(a) == 42, "");
356 constexpr variant<int, nonliteral> a(42);
357 static_assert(get<int>(a) == 42, "");
360 constexpr variant<nonliteral, int> a(42);
361 static_assert(get<int>(a) == 42, "");
364 constexpr variant<int> a(42);
365 static_assert(get<0>(std::move(a)) == 42, "");
368 constexpr variant<int, nonliteral> a(42);
369 static_assert(get<0>(std::move(a)) == 42, "");
372 constexpr variant<nonliteral, int> a(42);
373 static_assert(get<1>(std::move(a)) == 42, "");
376 constexpr variant<int> a(42);
377 static_assert(get<int>(std::move(a)) == 42, "");
380 constexpr variant<int, nonliteral> a(42);
381 static_assert(get<int>(std::move(a)) == 42, "");
384 constexpr variant<nonliteral, int> a(42);
385 static_assert(get<int>(std::move(a)) == 42, "");
389 void test_pr77641()
391 struct X {
392 constexpr X() { }
395 constexpr std::variant<X> v1 = X{};
398 namespace adl_trap
400 struct X {
401 X() = default;
402 X(int) { }
403 X(std::initializer_list<int>, const X&) { }
405 template<typename T> void move(T&) { }
406 template<typename T> void forward(T&) { }
408 struct Visitor {
409 template<typename T> void operator()(T&&) { }
413 void test_adl()
415 using adl_trap::X;
416 X x;
417 std::initializer_list<int> il;
418 adl_trap::Visitor vis;
420 std::variant<X> v0(x);
421 v0 = x;
422 v0.emplace<0>(x);
423 v0.emplace<0>(il, x);
424 visit(vis, v0);
425 variant<X> v1{in_place_index<0>, x};
426 variant<X> v2{in_place_type<X>, x};
427 variant<X> v3{in_place_index<0>, il, x};
428 variant<X> v4{in_place_type<X>, il, x};
431 void test_variant_alternative()
433 static_assert(is_same_v<variant_alternative_t<0, variant<int, string>>, int>, "");
434 static_assert(is_same_v<variant_alternative_t<1, variant<int, string>>, string>, "");
436 static_assert(is_same_v<variant_alternative_t<0, const variant<int>>, const int>, "");
437 static_assert(is_same_v<variant_alternative_t<0, volatile variant<int>>, volatile int>, "");
438 static_assert(is_same_v<variant_alternative_t<0, const volatile variant<int>>, const volatile int>, "");
441 template<typename V, typename T>
442 constexpr auto has_type_emplace(int) -> decltype((declval<V>().template emplace<T>(), true))
443 { return true; };
445 template<typename V, typename T>
446 constexpr bool has_type_emplace(...)
447 { return false; };
449 template<typename V, size_t N>
450 constexpr auto has_index_emplace(int) -> decltype((declval<V>().template emplace<N>(), true))
451 { return true; };
453 template<typename V, size_t T>
454 constexpr bool has_index_emplace(...)
455 { return false; };
457 void test_emplace()
459 static_assert(has_type_emplace<variant<int>, int>(0), "");
460 static_assert(!has_type_emplace<variant<long>, int>(0), "");
461 static_assert(has_index_emplace<variant<int>, 0>(0), "");
462 static_assert(!has_type_emplace<variant<AllDeleted>, AllDeleted>(0), "");
463 static_assert(!has_index_emplace<variant<AllDeleted>, 0>(0), "");
466 void test_triviality()
468 #define TEST_TEMPLATE(DT, CC, MC, CA, MA, CC_VAL, MC_VAL, CA_VAL, MA_VAL) \
470 struct A \
472 ~A() DT; \
473 A(const A&) CC; \
474 A(A&&) MC; \
475 A& operator=(const A&) CA; \
476 A& operator=(A&&) MA; \
477 }; \
478 static_assert(CC_VAL == is_trivially_copy_constructible_v<variant<A>>, ""); \
479 static_assert(MC_VAL == is_trivially_move_constructible_v<variant<A>>, ""); \
480 static_assert(CA_VAL == is_trivially_copy_assignable_v<variant<A>>, ""); \
481 static_assert(MA_VAL == is_trivially_move_assignable_v<variant<A>>, ""); \
483 TEST_TEMPLATE(=default, =default, =default, =default, =default, true, true, true, true)
484 TEST_TEMPLATE(=default, =default, =default, =default, , true, true, true, false)
485 TEST_TEMPLATE(=default, =default, =default, , =default, true, true, false, true)
486 TEST_TEMPLATE(=default, =default, =default, , , true, true, false, false)
487 TEST_TEMPLATE(=default, =default, , =default, =default, true, false, true, true)
488 TEST_TEMPLATE(=default, =default, , =default, , true, false, true, false)
489 TEST_TEMPLATE(=default, =default, , , =default, true, false, false, true)
490 TEST_TEMPLATE(=default, =default, , , , true, false, false, false)
491 TEST_TEMPLATE(=default, , =default, =default, =default, false, true, true, true)
492 TEST_TEMPLATE(=default, , =default, =default, , false, true, true, false)
493 TEST_TEMPLATE(=default, , =default, , =default, false, true, false, true)
494 TEST_TEMPLATE(=default, , =default, , , false, true, false, false)
495 TEST_TEMPLATE(=default, , , =default, =default, false, false, true, true)
496 TEST_TEMPLATE(=default, , , =default, , false, false, true, false)
497 TEST_TEMPLATE(=default, , , , =default, false, false, false, true)
498 TEST_TEMPLATE(=default, , , , , false, false, false, false)
499 TEST_TEMPLATE( , =default, =default, =default, =default, false, false, false, false)
500 TEST_TEMPLATE( , =default, =default, =default, , false, false, false, false)
501 TEST_TEMPLATE( , =default, =default, , =default, false, false, false, false)
502 TEST_TEMPLATE( , =default, =default, , , false, false, false, false)
503 TEST_TEMPLATE( , =default, , =default, =default, false, false, false, false)
504 TEST_TEMPLATE( , =default, , =default, , false, false, false, false)
505 TEST_TEMPLATE( , =default, , , =default, false, false, false, false)
506 TEST_TEMPLATE( , =default, , , , false, false, false, false)
507 TEST_TEMPLATE( , , =default, =default, =default, false, false, false, false)
508 TEST_TEMPLATE( , , =default, =default, , false, false, false, false)
509 TEST_TEMPLATE( , , =default, , =default, false, false, false, false)
510 TEST_TEMPLATE( , , =default, , , false, false, false, false)
511 TEST_TEMPLATE( , , , =default, =default, false, false, false, false)
512 TEST_TEMPLATE( , , , =default, , false, false, false, false)
513 TEST_TEMPLATE( , , , , =default, false, false, false, false)
514 TEST_TEMPLATE( , , , , , false, false, false, false)
515 #undef TEST_TEMPLATE
517 #define TEST_TEMPLATE(CC, MC, CA, MA) \
519 struct A \
521 A(const A&) CC; \
522 A(A&&) MC; \
523 A& operator=(const A&) CA; \
524 A& operator=(A&&) MA; \
525 }; \
526 static_assert(!is_trivially_copy_constructible_v<variant<AllDeleted, A>>, ""); \
527 static_assert(!is_trivially_move_constructible_v<variant<AllDeleted, A>>, ""); \
528 static_assert(!is_trivially_copy_assignable_v<variant<AllDeleted, A>>, ""); \
529 static_assert(!is_trivially_move_assignable_v<variant<AllDeleted, A>>, ""); \
531 TEST_TEMPLATE(=default, =default, =default, =default)
532 TEST_TEMPLATE(=default, =default, =default, )
533 TEST_TEMPLATE(=default, =default, , =default)
534 TEST_TEMPLATE(=default, =default, , )
535 TEST_TEMPLATE(=default, , =default, =default)
536 TEST_TEMPLATE(=default, , =default, )
537 TEST_TEMPLATE(=default, , , =default)
538 TEST_TEMPLATE(=default, , , )
539 TEST_TEMPLATE( , =default, =default, =default)
540 TEST_TEMPLATE( , =default, =default, )
541 TEST_TEMPLATE( , =default, , =default)
542 TEST_TEMPLATE( , =default, , )
543 TEST_TEMPLATE( , , =default, =default)
544 TEST_TEMPLATE( , , =default, )
545 TEST_TEMPLATE( , , , =default)
546 TEST_TEMPLATE( , , , )
547 #undef TEST_TEMPLATE
549 static_assert(is_trivially_copy_constructible_v<variant<DefaultNoexcept, int, char, float, double>>, "");
550 static_assert(is_trivially_move_constructible_v<variant<DefaultNoexcept, int, char, float, double>>, "");
551 static_assert(is_trivially_copy_assignable_v<variant<DefaultNoexcept, int, char, float, double>>, "");
552 static_assert(is_trivially_move_assignable_v<variant<DefaultNoexcept, int, char, float, double>>, "");