1 // { dg-options "-std=gnu++17" }
4 // Copyright (C) 2016-2019 Free Software Foundation, Inc.
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)
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/>.
24 #include <unordered_set>
25 #include <memory_resource>
26 #include <testsuite_hooks.h>
32 AlwaysThrow() = default;
34 AlwaysThrow(const AlwaysThrow
&)
37 AlwaysThrow(AlwaysThrow
&&)
40 AlwaysThrow
& operator=(const AlwaysThrow
&)
46 AlwaysThrow
& operator=(AlwaysThrow
&&)
52 bool operator<(const AlwaysThrow
&) const { VERIFY(false); }
53 bool operator<=(const AlwaysThrow
&) const { VERIFY(false); }
54 bool operator==(const AlwaysThrow
&) const { VERIFY(false); }
55 bool operator!=(const AlwaysThrow
&) const { VERIFY(false); }
56 bool operator>=(const AlwaysThrow
&) const { VERIFY(false); }
57 bool operator>(const AlwaysThrow
&) const { VERIFY(false); }
62 DeletedMoves() = default;
63 DeletedMoves(const DeletedMoves
&) = default;
64 DeletedMoves(DeletedMoves
&&) = delete;
65 DeletedMoves
& operator=(const DeletedMoves
&) = default;
66 DeletedMoves
& operator=(DeletedMoves
&&) = delete;
71 variant
<monostate
, string
> v
;
72 VERIFY(holds_alternative
<monostate
>(v
));
77 variant
<monostate
, string
> v("a");
78 VERIFY(holds_alternative
<string
>(v
));
79 variant
<monostate
, string
> u(v
);
80 VERIFY(holds_alternative
<string
>(u
));
81 VERIFY(get
<string
>(u
) == "a");
86 variant
<monostate
, string
> v("a");
87 VERIFY(holds_alternative
<string
>(v
));
88 variant
<monostate
, string
> u(std::move(v
));
89 VERIFY(holds_alternative
<string
>(u
));
90 VERIFY(get
<string
>(u
) == "a");
91 VERIFY(holds_alternative
<string
>(v
));
93 variant
<vector
<int>, DeletedMoves
> d
{std::in_place_index
<0>, {1, 2, 3, 4}};
94 // DeletedMoves is not move constructible, so this uses copy ctor:
95 variant
<vector
<int>, DeletedMoves
> e(std::move(d
));
96 VERIFY(std::get
<0>(d
).size() == 4);
97 VERIFY(std::get
<0>(e
).size() == 4);
100 void arbitrary_ctor()
102 variant
<int, string
> v("a");
103 VERIFY(holds_alternative
<string
>(v
));
104 VERIFY(get
<1>(v
) == "a");
107 struct ThrowingMoveCtorThrowsCopyCtor
109 ThrowingMoveCtorThrowsCopyCtor() noexcept
= default;
110 ThrowingMoveCtorThrowsCopyCtor(ThrowingMoveCtorThrowsCopyCtor
&&) {}
111 ThrowingMoveCtorThrowsCopyCtor(ThrowingMoveCtorThrowsCopyCtor
const&)
116 ThrowingMoveCtorThrowsCopyCtor
& operator=(ThrowingMoveCtorThrowsCopyCtor
&&) noexcept
118 ThrowingMoveCtorThrowsCopyCtor
& operator=(ThrowingMoveCtorThrowsCopyCtor
const&) noexcept
124 variant
<monostate
, string
> v("a");
125 VERIFY(holds_alternative
<string
>(v
));
126 variant
<monostate
, string
> u
;
128 VERIFY(holds_alternative
<string
>(u
));
129 VERIFY(get
<string
>(u
) == "a");
131 std::variant
<int, ThrowingMoveCtorThrowsCopyCtor
> v1
,
132 v2
= ThrowingMoveCtorThrowsCopyCtor();
133 bool should_throw
= false;
142 VERIFY(should_throw
);
148 variant
<monostate
, string
> v("a");
149 VERIFY(holds_alternative
<string
>(v
));
150 variant
<monostate
, string
> u
;
152 VERIFY(holds_alternative
<string
>(u
));
153 VERIFY(get
<string
>(u
) == "a");
154 VERIFY(holds_alternative
<string
>(v
));
156 variant
<vector
<int>, DeletedMoves
> d
{std::in_place_index
<0>, {1, 2, 3, 4}};
157 variant
<vector
<int>, DeletedMoves
> e
;
158 // DeletedMoves is not move assignable, so this uses copy assignment:
160 VERIFY(std::get
<0>(d
).size() == 4);
161 VERIFY(std::get
<0>(e
).size() == 4);
164 void arbitrary_assign()
166 variant
<int, string
> v
;
169 VERIFY(holds_alternative
<string
>(variant
<int, string
>("a")));
170 VERIFY(get
<1>(v
) == "a");
176 A(int& called
) : called(called
) {}
184 { variant
<string
, A
> a(in_place_index
<1>, called
); }
189 { variant
<string
, A
> a(in_place_index
<0>); }
194 void in_place_index_ctor()
197 variant
<int, string
> v(in_place_index
<1>, "a");
198 VERIFY(holds_alternative
<string
>(v
));
199 VERIFY(get
<1>(v
) == "a");
202 variant
<int, string
> v(in_place_index
<1>, {'a', 'b'});
203 VERIFY(holds_alternative
<string
>(v
));
204 VERIFY(get
<1>(v
) == "ab");
208 void in_place_type_ctor()
211 variant
<int, string
> v(in_place_type
<string
>, "a");
212 VERIFY(holds_alternative
<string
>(v
));
213 VERIFY(get
<1>(v
) == "a");
216 variant
<int, string
> v(in_place_type
<string
>, {'a', 'b'});
217 VERIFY(holds_alternative
<string
>(v
));
218 VERIFY(get
<1>(v
) == "ab");
224 variant
<int, string
> v
;
226 VERIFY(get
<0>(v
) == 1);
227 v
.emplace
<string
>("a");
228 VERIFY(get
<string
>(v
) == "a");
229 v
.emplace
<1>({'a', 'b'});
230 VERIFY(get
<1>(v
) == "ab");
231 v
.emplace
<string
>({'a', 'c'});
232 VERIFY(get
<string
>(v
) == "ac");
234 variant
<int, AlwaysThrow
> v
;
236 try { v
.emplace
<1>(a
); } catch (nullptr_t
) { }
237 VERIFY(v
.valueless_by_exception());
239 VERIFY(!v
.valueless_by_exception());
242 variant
<int, AlwaysThrow
> v
;
243 try { v
.emplace
<1>(AlwaysThrow
{}); } catch (nullptr_t
) { }
244 VERIFY(v
.valueless_by_exception());
246 VERIFY(!v
.valueless_by_exception());
248 VERIFY(&v
.emplace
<0>(1) == &std::get
<0>(v
));
249 VERIFY(&v
.emplace
<int>(1) == &std::get
<int>(v
));
250 VERIFY(&v
.emplace
<1>("a") == &std::get
<1>(v
));
251 VERIFY(&v
.emplace
<string
>("a") == &std::get
<string
>(v
));
253 variant
<vector
<int>> v
;
254 VERIFY(&v
.emplace
<0>({1,2,3}) == &std::get
<0>(v
));
255 VERIFY(&v
.emplace
<vector
<int>>({1,2,3}) == &std::get
<vector
<int>>(v
));
261 VERIFY(get
<1>(variant
<int, string
>("a")) == "a");
262 VERIFY(get
<string
>(variant
<int, string
>("a")) == "a");
268 get
<0>(variant
<int, string
>("a"));
270 catch (const bad_variant_access
&)
281 get
<int>(variant
<int, string
>("a"));
283 catch (const bad_variant_access
&)
291 void test_relational()
293 VERIFY((variant
<int, string
>(2) < variant
<int, string
>(3)));
294 VERIFY((variant
<int, string
>(3) == variant
<int, string
>(3)));
295 VERIFY((variant
<int, string
>(3) > variant
<int, string
>(2)));
296 VERIFY((variant
<int, string
>(3) <= variant
<int, string
>(3)));
297 VERIFY((variant
<int, string
>(2) <= variant
<int, string
>(3)));
298 VERIFY((variant
<int, string
>(3) >= variant
<int, string
>(3)));
299 VERIFY((variant
<int, string
>(3) >= variant
<int, string
>(2)));
300 VERIFY((variant
<int, string
>(2) != variant
<int, string
>(3)));
302 VERIFY((variant
<int, string
>(2) < variant
<int, string
>("a")));
303 VERIFY((variant
<string
, int>(2) > variant
<string
, int>("a")));
306 variant
<int, AlwaysThrow
> v
, w
;
312 catch (nullptr_t
) { }
313 VERIFY(v
.valueless_by_exception());
326 variant
<int, string
> a("a"), b("b");
328 VERIFY(get
<1>(a
) == "b");
329 VERIFY(get
<1>(b
) == "a");
331 VERIFY(get
<1>(a
) == "a");
332 VERIFY(get
<1>(b
) == "b");
340 int operator()(int, float) {
343 int operator()(int, double) {
346 int operator()(char, float) {
349 int operator()(char, double) {
352 int operator()(int, float) const {
355 int operator()(int, double) const {
358 int operator()(char, float) const {
361 int operator()(char, double) const {
365 VERIFY(visit(visitor1
, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 0);
366 VERIFY(visit(visitor1
, variant
<int, char>(1), variant
<float, double>(1.0)) == 1);
367 VERIFY(visit(visitor1
, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 2);
368 VERIFY(visit(visitor1
, variant
<int, char>('a'), variant
<float, double>(1.0)) == 3);
370 const auto& visitor2
= visitor1
;
371 VERIFY(visit(visitor2
, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 5);
372 VERIFY(visit(visitor2
, variant
<int, char>(1), variant
<float, double>(1.0)) == 6);
373 VERIFY(visit(visitor2
, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 7);
374 VERIFY(visit(visitor2
, variant
<int, char>('a'), variant
<float, double>(1.0)) == 8);
380 int operator()(int, float) && {
383 int operator()(int, double) && {
386 int operator()(char, float) && {
389 int operator()(char, double) && {
393 VERIFY(visit(Visitor
{}, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 0);
394 VERIFY(visit(Visitor
{}, variant
<int, char>(1), variant
<float, double>(1.0)) == 1);
395 VERIFY(visit(Visitor
{}, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 2);
396 VERIFY(visit(Visitor
{}, variant
<int, char>('a'), variant
<float, double>(1.0)) == 3);
402 unordered_set
<variant
<int, pmr::string
>> s
;
403 VERIFY(s
.emplace(3).second
);
404 VERIFY(s
.emplace("asdf").second
);
405 VERIFY(s
.emplace().second
);
406 VERIFY(s
.size() == 3);
407 VERIFY(!s
.emplace(3).second
);
408 VERIFY(!s
.emplace("asdf").second
);
409 VERIFY(!s
.emplace().second
);
410 VERIFY(s
.size() == 3);
414 operator pmr::string()
419 variant
<int, pmr::string
> v
;
427 VERIFY(v
.valueless_by_exception());
428 VERIFY(s
.insert(v
).second
);
429 VERIFY(s
.size() == 4);
430 VERIFY(!s
.insert(v
).second
);
434 void test_valueless_by_exception()
441 variant
<int, AlwaysThrow
> v(a
);
454 variant
<int, AlwaysThrow
> v(a
);
463 variant
<int, AlwaysThrow
> v
;
475 VERIFY(v
.valueless_by_exception());
478 variant
<int, AlwaysThrow
> v
;
489 VERIFY(v
.valueless_by_exception());
499 in_place_index_ctor();
500 in_place_type_ctor();
511 test_valueless_by_exception();