]>
Commit | Line | Data |
---|---|---|
47df7e19 | 1 | // { dg-do run { target c++17 } } |
435e56fb | 2 | |
7adcbafe | 3 | // Copyright (C) 2013-2022 Free Software Foundation, Inc. |
435e56fb VV |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
c7cbb4da | 16 | // You should have received a copy of the GNU General Public License along |
435e56fb VV |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | #include <optional> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
23 | struct exception {}; | |
24 | ||
25 | int counter = 0; | |
26 | ||
27 | struct mixin_counter | |
28 | { | |
29 | mixin_counter() { ++counter; } | |
30 | mixin_counter(mixin_counter const&) { ++counter; } | |
31 | ~mixin_counter() { --counter; } | |
32 | }; | |
33 | ||
34 | namespace ns | |
35 | { | |
36 | ||
37 | struct value_type : private mixin_counter | |
38 | { | |
39 | explicit value_type(int state) : state(state) { } | |
40 | int state; | |
41 | }; | |
42 | ||
43 | int swaps = 0; | |
44 | ||
45 | void | |
46 | swap(value_type& lhs, value_type& rhs) | |
47 | { | |
48 | ++swaps; | |
49 | using std::swap; | |
50 | swap(lhs.state, rhs.state); | |
51 | } | |
52 | ||
53 | } // namespace ns | |
54 | ||
55 | int main() | |
56 | { | |
57 | using O = std::optional<ns::value_type>; | |
58 | ||
59 | VERIFY( ns::swaps == 0 ); | |
60 | ||
61 | { | |
62 | O o, p; | |
63 | swap(o, p); | |
64 | VERIFY( !o ); | |
65 | VERIFY( !p ); | |
66 | } | |
67 | ||
68 | { | |
69 | O o { std::in_place, 45 }, p; | |
70 | swap(o, p); | |
71 | VERIFY( !o ); | |
72 | VERIFY( p && p->state == 45 ); | |
73 | } | |
74 | ||
75 | { | |
76 | O o, p { std::in_place, 45 }; | |
77 | swap(o, p); | |
78 | VERIFY( o && o->state == 45 ); | |
79 | VERIFY( !p ); | |
80 | } | |
81 | ||
82 | { | |
83 | O o { std::in_place, 167 }, p { std::in_place, 999 }; | |
84 | VERIFY( ns::swaps == 0 ); | |
85 | ||
86 | swap(o, p); | |
87 | ||
88 | VERIFY( o && o->state == 999 ); | |
89 | VERIFY( p && p->state == 167 ); | |
90 | VERIFY( ns::swaps == 1 ); | |
91 | } | |
92 | ||
93 | VERIFY( counter == 0 ); | |
94 | } |