]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/optional/assignment/4.cc
1 // { dg-do run { target c++14 } }
3 // Copyright (C) 2013-2022 Free Software Foundation, Inc.
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)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <experimental/optional>
21 #include <testsuite_hooks.h>
29 mixin_counter() { ++counter
; }
30 mixin_counter(mixin_counter
const&) { ++counter
; }
31 ~mixin_counter() { --counter
; }
34 struct value_type
: private mixin_counter
40 throwing_construction
,
42 throwing_copy_assignment
,
44 throwing_move_assignment
,
48 value_type() = default;
50 explicit value_type(state_type state_
)
53 throw_if(throwing_construction
);
56 value_type(value_type
const& other
)
59 throw_if(throwing_copy
);
63 operator=(value_type
const& other
)
66 throw_if(throwing_copy_assignment
);
70 value_type(value_type
&& other
)
73 other
.state
= moved_from
;
74 throw_if(throwing_move
);
78 operator=(value_type
&& other
)
81 other
.state
= moved_from
;
82 throw_if(throwing_move_assignment
);
86 void throw_if(state_type match
)
95 state_type state
= zero
;
100 using O
= std::experimental::optional
<value_type
>;
101 using S
= value_type::state_type
;
102 auto const make
= [](S s
= S::zero
) { return value_type
{ s
}; };
104 enum outcome_type
{ nothrow
, caught
, bad_catch
};
106 // Check value assignment for engaged optional
110 value_type v
= make(S::throwing_copy
);
112 VERIFY( o
&& o
->state
== S::throwing_copy
);
117 value_type v
= make(S::throwing_move
);
119 VERIFY( o
&& o
->state
== S::throwing_move
);
123 outcome_type outcome
{};
125 value_type v
= make(S::throwing_copy_assignment
);
131 catch(exception
const&)
132 { outcome
= caught
; }
134 { outcome
= bad_catch
; }
136 VERIFY( o
&& o
->state
== S::threw
);
140 outcome_type outcome
{};
142 value_type v
= make(S::throwing_move_assignment
);
148 catch(exception
const&)
149 { outcome
= caught
; }
151 { outcome
= bad_catch
; }
153 VERIFY( o
&& o
->state
== S::threw
);
156 VERIFY( counter
== 0 );