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 variant
<monostate
, string
> v
;
63 VERIFY(holds_alternative
<monostate
>(v
));
68 variant
<monostate
, string
> v("a");
69 VERIFY(holds_alternative
<string
>(v
));
70 variant
<monostate
, string
> u(v
);
71 VERIFY(holds_alternative
<string
>(u
));
72 VERIFY(get
<string
>(u
) == "a");
77 variant
<monostate
, string
> v("a");
78 VERIFY(holds_alternative
<string
>(v
));
79 variant
<monostate
, string
> u(std::move(v
));
80 VERIFY(holds_alternative
<string
>(u
));
81 VERIFY(get
<string
>(u
) == "a");
82 VERIFY(holds_alternative
<string
>(v
));
87 variant
<int, string
> v("a");
88 VERIFY(holds_alternative
<string
>(v
));
89 VERIFY(get
<1>(v
) == "a");
92 struct ThrowingMoveCtorThrowsCopyCtor
94 ThrowingMoveCtorThrowsCopyCtor() noexcept
= default;
95 ThrowingMoveCtorThrowsCopyCtor(ThrowingMoveCtorThrowsCopyCtor
&&) {}
96 ThrowingMoveCtorThrowsCopyCtor(ThrowingMoveCtorThrowsCopyCtor
const&)
101 ThrowingMoveCtorThrowsCopyCtor
& operator=(ThrowingMoveCtorThrowsCopyCtor
&&) noexcept
103 ThrowingMoveCtorThrowsCopyCtor
& operator=(ThrowingMoveCtorThrowsCopyCtor
const&) noexcept
109 variant
<monostate
, string
> v("a");
110 VERIFY(holds_alternative
<string
>(v
));
111 variant
<monostate
, string
> u
;
113 VERIFY(holds_alternative
<string
>(u
));
114 VERIFY(get
<string
>(u
) == "a");
116 std::variant
<int, ThrowingMoveCtorThrowsCopyCtor
> v1
,
117 v2
= ThrowingMoveCtorThrowsCopyCtor();
118 bool should_throw
= false;
127 VERIFY(should_throw
);
133 variant
<monostate
, string
> v("a");
134 VERIFY(holds_alternative
<string
>(v
));
135 variant
<monostate
, string
> u
;
137 VERIFY(holds_alternative
<string
>(u
));
138 VERIFY(get
<string
>(u
) == "a");
139 VERIFY(holds_alternative
<string
>(v
));
142 void arbitrary_assign()
144 variant
<int, string
> v
;
147 VERIFY(holds_alternative
<string
>(variant
<int, string
>("a")));
148 VERIFY(get
<1>(v
) == "a");
154 A(int& called
) : called(called
) {}
162 { variant
<string
, A
> a(in_place_index
<1>, called
); }
167 { variant
<string
, A
> a(in_place_index
<0>); }
172 void in_place_index_ctor()
175 variant
<int, string
> v(in_place_index
<1>, "a");
176 VERIFY(holds_alternative
<string
>(v
));
177 VERIFY(get
<1>(v
) == "a");
180 variant
<int, string
> v(in_place_index
<1>, {'a', 'b'});
181 VERIFY(holds_alternative
<string
>(v
));
182 VERIFY(get
<1>(v
) == "ab");
186 void in_place_type_ctor()
189 variant
<int, string
> v(in_place_type
<string
>, "a");
190 VERIFY(holds_alternative
<string
>(v
));
191 VERIFY(get
<1>(v
) == "a");
194 variant
<int, string
> v(in_place_type
<string
>, {'a', 'b'});
195 VERIFY(holds_alternative
<string
>(v
));
196 VERIFY(get
<1>(v
) == "ab");
202 variant
<int, string
> v
;
204 VERIFY(get
<0>(v
) == 1);
205 v
.emplace
<string
>("a");
206 VERIFY(get
<string
>(v
) == "a");
207 v
.emplace
<1>({'a', 'b'});
208 VERIFY(get
<1>(v
) == "ab");
209 v
.emplace
<string
>({'a', 'c'});
210 VERIFY(get
<string
>(v
) == "ac");
212 variant
<int, AlwaysThrow
> v
;
214 try { v
.emplace
<1>(a
); } catch (nullptr_t
) { }
215 VERIFY(v
.valueless_by_exception());
217 VERIFY(!v
.valueless_by_exception());
220 variant
<int, AlwaysThrow
> v
;
221 try { v
.emplace
<1>(AlwaysThrow
{}); } catch (nullptr_t
) { }
222 VERIFY(v
.valueless_by_exception());
224 VERIFY(!v
.valueless_by_exception());
226 VERIFY(&v
.emplace
<0>(1) == &std::get
<0>(v
));
227 VERIFY(&v
.emplace
<int>(1) == &std::get
<int>(v
));
228 VERIFY(&v
.emplace
<1>("a") == &std::get
<1>(v
));
229 VERIFY(&v
.emplace
<string
>("a") == &std::get
<string
>(v
));
231 variant
<vector
<int>> v
;
232 VERIFY(&v
.emplace
<0>({1,2,3}) == &std::get
<0>(v
));
233 VERIFY(&v
.emplace
<vector
<int>>({1,2,3}) == &std::get
<vector
<int>>(v
));
239 VERIFY(get
<1>(variant
<int, string
>("a")) == "a");
240 VERIFY(get
<string
>(variant
<int, string
>("a")) == "a");
246 get
<0>(variant
<int, string
>("a"));
248 catch (const bad_variant_access
&)
259 get
<int>(variant
<int, string
>("a"));
261 catch (const bad_variant_access
&)
269 void test_relational()
271 VERIFY((variant
<int, string
>(2) < variant
<int, string
>(3)));
272 VERIFY((variant
<int, string
>(3) == variant
<int, string
>(3)));
273 VERIFY((variant
<int, string
>(3) > variant
<int, string
>(2)));
274 VERIFY((variant
<int, string
>(3) <= variant
<int, string
>(3)));
275 VERIFY((variant
<int, string
>(2) <= variant
<int, string
>(3)));
276 VERIFY((variant
<int, string
>(3) >= variant
<int, string
>(3)));
277 VERIFY((variant
<int, string
>(3) >= variant
<int, string
>(2)));
278 VERIFY((variant
<int, string
>(2) != variant
<int, string
>(3)));
280 VERIFY((variant
<int, string
>(2) < variant
<int, string
>("a")));
281 VERIFY((variant
<string
, int>(2) > variant
<string
, int>("a")));
284 variant
<int, AlwaysThrow
> v
, w
;
290 catch (nullptr_t
) { }
291 VERIFY(v
.valueless_by_exception());
304 variant
<int, string
> a("a"), b("b");
306 VERIFY(get
<1>(a
) == "b");
307 VERIFY(get
<1>(b
) == "a");
309 VERIFY(get
<1>(a
) == "a");
310 VERIFY(get
<1>(b
) == "b");
318 int operator()(int, float) {
321 int operator()(int, double) {
324 int operator()(char, float) {
327 int operator()(char, double) {
330 int operator()(int, float) const {
333 int operator()(int, double) const {
336 int operator()(char, float) const {
339 int operator()(char, double) const {
343 VERIFY(visit(visitor1
, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 0);
344 VERIFY(visit(visitor1
, variant
<int, char>(1), variant
<float, double>(1.0)) == 1);
345 VERIFY(visit(visitor1
, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 2);
346 VERIFY(visit(visitor1
, variant
<int, char>('a'), variant
<float, double>(1.0)) == 3);
348 const auto& visitor2
= visitor1
;
349 VERIFY(visit(visitor2
, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 5);
350 VERIFY(visit(visitor2
, variant
<int, char>(1), variant
<float, double>(1.0)) == 6);
351 VERIFY(visit(visitor2
, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 7);
352 VERIFY(visit(visitor2
, variant
<int, char>('a'), variant
<float, double>(1.0)) == 8);
358 int operator()(int, float) && {
361 int operator()(int, double) && {
364 int operator()(char, float) && {
367 int operator()(char, double) && {
371 VERIFY(visit(Visitor
{}, variant
<int, char>(1), variant
<float, double>(1.0f
)) == 0);
372 VERIFY(visit(Visitor
{}, variant
<int, char>(1), variant
<float, double>(1.0)) == 1);
373 VERIFY(visit(Visitor
{}, variant
<int, char>('a'), variant
<float, double>(1.0f
)) == 2);
374 VERIFY(visit(Visitor
{}, variant
<int, char>('a'), variant
<float, double>(1.0)) == 3);
380 unordered_set
<variant
<int, pmr::string
>> s
;
381 VERIFY(s
.emplace(3).second
);
382 VERIFY(s
.emplace("asdf").second
);
383 VERIFY(s
.emplace().second
);
384 VERIFY(s
.size() == 3);
385 VERIFY(!s
.emplace(3).second
);
386 VERIFY(!s
.emplace("asdf").second
);
387 VERIFY(!s
.emplace().second
);
388 VERIFY(s
.size() == 3);
392 operator pmr::string()
397 variant
<int, pmr::string
> v
;
405 VERIFY(v
.valueless_by_exception());
406 VERIFY(s
.insert(v
).second
);
407 VERIFY(s
.size() == 4);
408 VERIFY(!s
.insert(v
).second
);
412 void test_valueless_by_exception()
419 variant
<int, AlwaysThrow
> v(a
);
432 variant
<int, AlwaysThrow
> v(a
);
441 variant
<int, AlwaysThrow
> v
;
453 VERIFY(v
.valueless_by_exception());
456 variant
<int, AlwaysThrow
> v
;
467 VERIFY(v
.valueless_by_exception());
477 in_place_index_ctor();
478 in_place_type_ctor();
489 test_valueless_by_exception();