1 // Copyright (C) 2013-2019 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
)
60 // [20.5.4.1] Constructors
64 gdb::optional
<long> o
{ i
};
66 VERIFY( *o
== 0x1234ABCD );
67 VERIFY( i
== 0x1234ABCD );
72 gdb::optional
<long> o
= i
;
74 VERIFY( *o
== 0x1234ABCD );
75 VERIFY( i
== 0x1234ABCD );
80 gdb::optional
<long> o
= { i
};
82 VERIFY( *o
== 0x1234ABCD );
83 VERIFY( i
== 0x1234ABCD );
88 gdb::optional
<long> o
{ std::move(i
) };
90 VERIFY( *o
== 0x1234ABCD );
91 VERIFY( i
== 0x1234ABCD );
96 gdb::optional
<long> o
= std::move(i
);
98 VERIFY( *o
== 0x1234ABCD );
99 VERIFY( i
== 0x1234ABCD );
104 gdb::optional
<long> o
= { std::move(i
) };
106 VERIFY( *o
== 0x1234ABCD );
107 VERIFY( i
== 0x1234ABCD );
112 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
113 gdb::optional
<std::vector
<int>> o
{ v
};
114 VERIFY( !v
.empty() );
115 VERIFY( o
->size() == 6 );
120 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
121 gdb::optional
<std::vector
<int>> o
= v
;
122 VERIFY( !v
.empty() );
123 VERIFY( o
->size() == 6 );
127 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
128 gdb::optional
<std::vector
<int>> o
{ v
};
129 VERIFY( !v
.empty() );
130 VERIFY( o
->size() == 6 );
134 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
135 gdb::optional
<std::vector
<int>> o
{ std::move(v
) };
137 VERIFY( o
->size() == 6 );
141 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
142 gdb::optional
<std::vector
<int>> o
= std::move(v
);
144 VERIFY( o
->size() == 6 );
148 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
149 gdb::optional
<std::vector
<int>> o
{ std::move(v
) };
151 VERIFY( o
->size() == 6 );
156 gdb::optional
<tracker
> o
= t
;
157 VERIFY( o
->value
== 333 );
158 VERIFY( tracker::count
== 2 );
159 VERIFY( t
.value
== 333 );
164 gdb::optional
<tracker
> o
= std::move(t
);
165 VERIFY( o
->value
== 333 );
166 VERIFY( tracker::count
== 2 );
167 VERIFY( t
.value
== -1 );
170 enum outcome
{ nothrow
, caught
, bad_catch
};
173 outcome result
= nothrow
;
174 throwing_construction t
{ false };
178 gdb::optional
<throwing_construction
> o
{ t
};
180 catch(exception
const&)
183 { result
= bad_catch
; }
185 VERIFY( result
== nothrow
);
189 outcome result
= nothrow
;
190 throwing_construction t
{ true };
194 gdb::optional
<throwing_construction
> o
{ t
};
196 catch(exception
const&)
199 { result
= bad_catch
; }
201 VERIFY( result
== caught
);
205 outcome result
= nothrow
;
206 throwing_construction t
{ false };
210 gdb::optional
<throwing_construction
> o
{ std::move(t
) };
212 catch(exception
const&)
215 { result
= bad_catch
; }
217 VERIFY( result
== nothrow
);
221 outcome result
= nothrow
;
222 throwing_construction t
{ true };
226 gdb::optional
<throwing_construction
> o
{ std::move(t
) };
228 catch(exception
const&)
231 { result
= bad_catch
; }
233 VERIFY( result
== caught
);
238 gdb::optional
<std::string
> os
= "foo";
243 X
& operator=(int) {return *this;}
246 gdb::optional
<X
> ox
{42};
248 gdb::optional
<int> oi
{42};
250 gdb::optional
<X
> ox2
{oi
};
252 gdb::optional
<std::string
> os2
;
255 gdb::optional
<X
> ox3
;
257 gdb::optional
<X
> ox4
;
262 // no converting construction.
264 gdb::optional
<int> oi
= gdb::optional
<short>();
266 gdb::optional
<std::string
> os
= gdb::optional
<const char*>();
269 gdb::optional
<gdb::optional
<int>> ooi
= gdb::optional
<int>();
271 ooi
= gdb::optional
<int>();
273 ooi
= gdb::optional
<int>(42);
277 gdb::optional
<gdb::optional
<int>> ooi2
= gdb::optional
<short>();
279 ooi2
= gdb::optional
<short>();
281 ooi2
= gdb::optional
<short>(6);
284 gdb::optional
<gdb::optional
<int>> ooi3
= gdb::optional
<int>(42);
287 gdb::optional
<gdb::optional
<int>> ooi4
= gdb::optional
<short>(6);
294 } // namespace cons_value