]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/optional/assignment/3.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
4 // Copyright (C) 2013-2018 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/>.
22 #include <testsuite_hooks.h>
30 mixin_counter() { ++counter
; }
31 mixin_counter(mixin_counter
const&) { ++counter
; }
32 ~mixin_counter() { --counter
; }
35 struct value_type
: private mixin_counter
41 throwing_construction
,
43 throwing_copy_assignment
,
45 throwing_move_assignment
,
49 value_type() = default;
51 explicit value_type(state_type state_
)
54 throw_if(throwing_construction
);
57 value_type(value_type
const& other
)
60 throw_if(throwing_copy
);
64 operator=(value_type
const& other
)
67 throw_if(throwing_copy_assignment
);
71 value_type(value_type
&& other
)
74 other
.state
= moved_from
;
75 throw_if(throwing_move
);
79 operator=(value_type
&& other
)
82 other
.state
= moved_from
;
83 throw_if(throwing_move_assignment
);
87 void throw_if(state_type match
)
96 state_type state
= zero
;
101 using O
= std::optional
<value_type
>;
102 using S
= value_type::state_type
;
103 auto const make
= [](S s
= S::zero
) { return value_type
{ s
}; };
105 enum outcome_type
{ nothrow
, caught
, bad_catch
};
107 // Check value assignment for disengaged optional
111 value_type v
= make(S::throwing_copy_assignment
);
113 VERIFY( o
&& o
->state
== S::throwing_copy_assignment
);
118 value_type v
= make(S::throwing_move_assignment
);
120 VERIFY( o
&& o
->state
== S::throwing_move_assignment
);
124 outcome_type outcome
{};
126 value_type v
= make(S::throwing_copy
);
132 catch(exception
const&)
133 { outcome
= caught
; }
135 { outcome
= bad_catch
; }
141 outcome_type outcome
{};
143 value_type v
= make(S::throwing_move
);
149 catch(exception
const&)
150 { outcome
= caught
; }
152 { outcome
= bad_catch
; }
157 VERIFY( counter
== 0 );