Debian Codegraph @

libstdc++-v3/testsuite/20_util/variant/constexpr.cc

raw
1 // { dg-do compile { target c++20 } }
2 // { dg-add-options no_pch }
3
4 #include <variant>
5
6 // P2231R1 Missing constexpr in std::optional and std::variant
7
8 #ifndef __cpp_lib_variant
9 #error "Feature test macro for variant is missing in <variant>"
10 #elif __cpp_lib_variant < 202106L
11 # error "Feature test macro for variant has wrong value for C++20 in <variant>"
12 #endif
13
14 #include <testsuite_hooks.h>
15
16
17 constexpr bool
18 test_assign()
19 {
20 std::variant<int, double> v1(1);
21 v1 = 2.5;
22 VERIFY( std::get<double>(v1) == 2.5 );
23
24 v1 = 99;
25 VERIFY( std::get<int>(v1) == 99 );
26 v1 = 999;
27 VERIFY( std::get<int>(v1) == 999 );
28
29 struct S // non-trivial special members
30 {
31 constexpr S(int i) : i(i) { }
32 constexpr ~S() { }
33 constexpr S(const S& s) : i(s.i) { }
34
35 int i;
36 };
37
38 std::variant<int, S> v;
39 v = S(123);
40 VERIFY( std::get<1>(v).i == 123 );
41
42 const S s(456);
43 v = s;
44 VERIFY( std::get<1>(v).i == 456 );
45
46 v = 789;
47 VERIFY( std::get<0>(v) == 789 );
48
49 return true;
50 }
51
52 static_assert( test_assign() );
53
54 constexpr bool
55 test_emplace()
56 {
57 struct S // non-trivial special members
58 {
59 constexpr S(std::initializer_list<int> l) : i(l.begin()[0]) { }
60 constexpr S(std::initializer_list<int> l, int n) : i(l.begin()[n]) { }
61 constexpr ~S() { }
62 constexpr S(const S& s) : i(s.i) { }
63
64 int i;
65 };
66
67 std::variant<int, double, S> v(1);
68
69 // template<class T, class... Args> constexpr T& emplace(Args&&... args);
70 v.emplace<double>(2.0);
71 VERIFY( std::get<1>(v) == 2.0 );
72 v.emplace<double>(2.5);
73 VERIFY( std::get<1>(v) == 2.5 );
74 v.emplace<int>(2.5);
75 VERIFY( std::get<0>(v) == 2 );
76
77 // template<class T, class U, class... Args>
78 // constexpr T& emplace(initializer_list<U>, Args&&... args);
79 v.emplace<S>({3, 2, 1});
80 VERIFY( std::get<2>(v).i == 3 );
81 v.emplace<S>({3, 2, 1}, 1);
82 VERIFY( std::get<2>(v).i == 2 );
83
84 // template<size_t I, class... Args>
85 // constexpr variant_alternative_t<I, ...>& emplace(Args&&... args);
86 v.emplace<1>(3.0);
87 VERIFY( std::get<1>(v) == 3.0 );
88 v.emplace<1>(0.5);
89 VERIFY( std::get<1>(v) == 0.5 );
90 v.emplace<0>(1.5);
91 VERIFY( std::get<0>(v) == 1 );
92
93 // template<size_t I, class U, class... Args>
94 // constexpr variant_alternative_t<I, ...>&
95 // emplace(initializer_list<U>, Args&&... args);
96 v.emplace<2>({7, 8, 9});
97 VERIFY( std::get<2>(v).i == 7 );
98 v.emplace<2>({13, 12, 11}, 1);
99 VERIFY( std::get<2>(v).i == 12 );
100
101 return true;
102 }
103
104 static_assert( test_emplace() );
105
106 constexpr bool
107 test_swap()
108 {
109 std::variant<int, double> v1(1), v2(2.5);
110 v1.swap(v2);
111 VERIFY( std::get<double>(v1) == 2.5 );
112 VERIFY( std::get<int>(v2) == 1 );
113
114 swap(v1, v2);
115 VERIFY( std::get<int>(v1) == 1 );
116 VERIFY( std::get<double>(v2) == 2.5 );
117
118 struct S
119 {
120 constexpr S(int i) : i(i) { }
121 constexpr S(S&& s) : i(s.i) { }
122 constexpr S& operator=(S&& s) { i = s.i; s.i = -1; return *this; }
123
124 int i;
125 };
126
127 std::variant<int, S> v3(3), v4(S(4));
128 v3.swap(v4);
129 VERIFY( std::get<S>(v3).i == 4 );
130 VERIFY( std::get<int>(v4) == 3 );
131 v3.swap(v4);
132 VERIFY( std::get<int>(v3) == 3 );
133 VERIFY( std::get<S>(v4).i == 4 );
134
135 return true;
136 }
137
138 static_assert( test_swap() );