1 // { dg-options "-std=gnu++17" }
4 // Copyright (C) 2013-2017 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>
29 tracker(int value
) : value(value
) { ++count
; }
30 ~tracker() { --count
; }
32 tracker(tracker
const& other
) : value(other
.value
) { ++count
; }
33 tracker(tracker
&& other
) : value(other
.value
)
39 tracker
& operator=(tracker
const&) = default;
40 tracker
& operator=(tracker
&&) = default;
47 int tracker::count
= 0;
51 struct throwing_construction
53 explicit throwing_construction(bool propagate
) : propagate(propagate
) { }
55 throwing_construction(throwing_construction
const& other
)
56 : propagate(other
.propagate
)
67 // [20.5.4.1] Constructors
71 std::optional
<long> o
{ i
};
73 VERIFY( *o
== 0x1234ABCD );
74 VERIFY( i
== 0x1234ABCD );
79 std::optional
<long> o
= i
;
81 VERIFY( *o
== 0x1234ABCD );
82 VERIFY( i
== 0x1234ABCD );
87 std::optional
<long> o
= { i
};
89 VERIFY( *o
== 0x1234ABCD );
90 VERIFY( i
== 0x1234ABCD );
95 std::optional
<long> o
{ std::move(i
) };
97 VERIFY( *o
== 0x1234ABCD );
98 VERIFY( i
== 0x1234ABCD );
103 std::optional
<long> o
= std::move(i
);
105 VERIFY( *o
== 0x1234ABCD );
106 VERIFY( i
== 0x1234ABCD );
111 std::optional
<long> o
= { std::move(i
) };
113 VERIFY( *o
== 0x1234ABCD );
114 VERIFY( i
== 0x1234ABCD );
118 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
119 std::optional
<std::vector
<int>> o
{ v
};
120 VERIFY( !v
.empty() );
121 VERIFY( o
->size() == 6 );
125 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
126 std::optional
<std::vector
<int>> o
= v
;
127 VERIFY( !v
.empty() );
128 VERIFY( o
->size() == 6 );
132 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
133 std::optional
<std::vector
<int>> o
{ v
};
134 VERIFY( !v
.empty() );
135 VERIFY( o
->size() == 6 );
139 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
140 std::optional
<std::vector
<int>> o
{ std::move(v
) };
142 VERIFY( o
->size() == 6 );
146 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
147 std::optional
<std::vector
<int>> o
= std::move(v
);
149 VERIFY( o
->size() == 6 );
153 std::vector
<int> v
= { 0, 1, 2, 3, 4, 5 };
154 std::optional
<std::vector
<int>> o
{ std::move(v
) };
156 VERIFY( o
->size() == 6 );
161 std::optional
<tracker
> o
= t
;
162 VERIFY( o
->value
== 333 );
163 VERIFY( tracker::count
== 2 );
164 VERIFY( t
.value
== 333 );
169 std::optional
<tracker
> o
= std::move(t
);
170 VERIFY( o
->value
== 333 );
171 VERIFY( tracker::count
== 2 );
172 VERIFY( t
.value
== -1 );
175 enum outcome
{ nothrow
, caught
, bad_catch
};
178 outcome result
= nothrow
;
179 throwing_construction t
{ false };
183 std::optional
<throwing_construction
> o
{ t
};
185 catch(exception
const&)
188 { result
= bad_catch
; }
190 VERIFY( result
== nothrow
);
194 outcome result
= nothrow
;
195 throwing_construction t
{ true };
199 std::optional
<throwing_construction
> o
{ t
};
201 catch(exception
const&)
204 { result
= bad_catch
; }
206 VERIFY( result
== caught
);
210 outcome result
= nothrow
;
211 throwing_construction t
{ false };
215 std::optional
<throwing_construction
> o
{ std::move(t
) };
217 catch(exception
const&)
220 { result
= bad_catch
; }
222 VERIFY( result
== nothrow
);
226 outcome result
= nothrow
;
227 throwing_construction t
{ true };
231 std::optional
<throwing_construction
> o
{ std::move(t
) };
233 catch(exception
const&)
236 { result
= bad_catch
; }
238 VERIFY( result
== caught
);
242 std::optional
<std::string
> os
= "foo";
246 X
& operator=(int) {return *this;}
248 std::optional
<X
> ox
{42};
249 std::optional
<int> oi
{42};
250 std::optional
<X
> ox2
{oi
};
251 std::optional
<std::string
> os2
;
253 std::optional
<X
> ox3
;
255 std::optional
<X
> ox4
;
259 std::optional
<int> oi
= std::optional
<short>();
261 std::optional
<std::string
> os
= std::optional
<const char*>();
263 std::optional
<std::optional
<int>> ooi
= std::optional
<int>();
265 ooi
= std::optional
<int>();
267 ooi
= std::optional
<int>(42);
270 std::optional
<std::optional
<int>> ooi2
= std::optional
<short>();
272 ooi2
= std::optional
<short>();
274 ooi2
= std::optional
<short>(6);
277 std::optional
<std::optional
<int>> ooi3
= std::optional
<int>(42);
280 std::optional
<std::optional
<int>> ooi4
= std::optional
<short>(6);