1 // Copyright (C) 2013-2023 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 namespace cons_value
{
22 tracker(int value
) : value(value
) { ++count
; }
23 ~tracker() { --count
; }
25 tracker(tracker
const& other
) : value(other
.value
) { ++count
; }
26 tracker(tracker
&& other
) : value(other
.value
)
32 tracker
& operator=(tracker
const&) = default;
33 tracker
& operator=(tracker
&&) = default;
40 int tracker::count
= 0;
44 struct throwing_construction
46 explicit throwing_construction(bool propagate
) : propagate(propagate
) { }
48 throwing_construction(throwing_construction
const& other
)
49 : propagate(other
.propagate
)
61 // [20.5.4.1] Constructors
65 gdb::optional
<long> o
{ i
};
67 VERIFY( *o
== 0x1234ABCD );
68 VERIFY( i
== 0x1234ABCD );
73 gdb::optional
<long> o
= i
;
75 VERIFY( *o
== 0x1234ABCD );
76 VERIFY( i
== 0x1234ABCD );
81 gdb::optional
<long> o
= { i
};
83 VERIFY( *o
== 0x1234ABCD );
84 VERIFY( i
== 0x1234ABCD );
89 gdb::optional
<long> o
{ std::move(i
) };
91 VERIFY( *o
== 0x1234ABCD );
92 VERIFY( i
== 0x1234ABCD );
97 gdb::optional
<long> o
= std::move(i
);
99 VERIFY( *o
== 0x1234ABCD );
100 VERIFY( i
== 0x1234ABCD );
105 gdb::optional
<long> o
= { std::move(i
) };
107 VERIFY( *o
== 0x1234ABCD );
108 VERIFY( i
== 0x1234ABCD );
113 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
114 gdb::optional
<std::vector
<int>> o
{ v
};
115 VERIFY( !v
.empty() );
116 VERIFY( o
->size() == 6 );
121 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
122 gdb::optional
<std::vector
<int>> o
= v
;
123 VERIFY( !v
.empty() );
124 VERIFY( o
->size() == 6 );
128 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
129 gdb::optional
<std::vector
<int>> o
{ v
};
130 VERIFY( !v
.empty() );
131 VERIFY( o
->size() == 6 );
135 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
136 gdb::optional
<std::vector
<int>> o
{ std::move(v
) };
138 VERIFY( o
->size() == 6 );
142 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
143 gdb::optional
<std::vector
<int>> o
= std::move(v
);
145 VERIFY( o
->size() == 6 );
149 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
150 gdb::optional
<std::vector
<int>> o
{ std::move(v
) };
152 VERIFY( o
->size() == 6 );
157 gdb::optional
<tracker
> o
= t
;
158 VERIFY( o
->value
== 333 );
159 VERIFY( tracker::count
== 2 );
160 VERIFY( t
.value
== 333 );
165 gdb::optional
<tracker
> o
= std::move(t
);
166 VERIFY( o
->value
== 333 );
167 VERIFY( tracker::count
== 2 );
168 VERIFY( t
.value
== -1 );
171 enum outcome
{ nothrow
, caught
, bad_catch
};
174 outcome result
= nothrow
;
175 throwing_construction t
{ false };
179 gdb::optional
<throwing_construction
> o
{ t
};
181 catch(exception
const&)
184 { result
= bad_catch
; }
186 VERIFY( result
== nothrow
);
190 outcome result
= nothrow
;
191 throwing_construction t
{ true };
195 gdb::optional
<throwing_construction
> o
{ t
};
197 catch(exception
const&)
200 { result
= bad_catch
; }
202 VERIFY( result
== caught
);
206 outcome result
= nothrow
;
207 throwing_construction t
{ false };
211 gdb::optional
<throwing_construction
> o
{ std::move(t
) };
213 catch(exception
const&)
216 { result
= bad_catch
; }
218 VERIFY( result
== nothrow
);
222 outcome result
= nothrow
;
223 throwing_construction t
{ true };
227 gdb::optional
<throwing_construction
> o
{ std::move(t
) };
229 catch(exception
const&)
232 { result
= bad_catch
; }
234 VERIFY( result
== caught
);
239 gdb::optional
<std::string
> os
= "foo";
244 X
& operator=(int) {return *this;}
247 gdb::optional
<X
> ox
{42};
249 gdb::optional
<int> oi
{42};
251 gdb::optional
<X
> ox2
{oi
};
253 gdb::optional
<std::string
> os2
;
256 gdb::optional
<X
> ox3
;
258 gdb::optional
<X
> ox4
;
263 // no converting construction.
265 gdb::optional
<int> oi
= gdb::optional
<short>();
267 gdb::optional
<std::string
> os
= gdb::optional
<const char*>();
270 gdb::optional
<gdb::optional
<int>> ooi
= gdb::optional
<int>();
272 ooi
= gdb::optional
<int>();
274 ooi
= gdb::optional
<int>(42);
278 gdb::optional
<gdb::optional
<int>> ooi2
= gdb::optional
<short>();
280 ooi2
= gdb::optional
<short>();
282 ooi2
= gdb::optional
<short>(6);
285 gdb::optional
<gdb::optional
<int>> ooi3
= gdb::optional
<int>(42);
288 gdb::optional
<gdb::optional
<int>> ooi4
= gdb::optional
<short>(6);
295 } // namespace cons_value