]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/optional/77288.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
4 // Copyright (C) 2016-2019 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/>.
26 #include <testsuite_hooks.h>
30 optional
<optional
<int>> nested_element
;
31 optional
<int> element
= {};
32 nested_element
= element
;
33 VERIFY(nested_element
);
39 static optional
<T
> get_result()
45 static optional
<T
> get_result_with_cond(bool cond
)
55 VERIFY(service_result
<int>::get_result());
56 VERIFY(service_result
<optional
<int>>::get_result());
57 VERIFY(service_result
<int>::get_result_with_cond(true));
58 VERIFY(service_result
<optional
<int>>::get_result_with_cond(true));
59 VERIFY(!service_result
<int>::get_result_with_cond(false));
60 VERIFY(!service_result
<optional
<int>>::get_result_with_cond(false));
66 Widget(optional
<int>) {}
75 static_assert(!std::is_assignable_v
<optional
<Widget
>&,
77 w
= optional
<optional
<int>>();
79 static_assert(!std::is_assignable_v
<optional
<Widget
>&,
80 optional
<optional
<short>>>);;
82 optional
<Widget
> w2
{optional
<int>()};
84 optional
<Widget
> w3
= optional
<int>();
86 optional
<Widget
> w4
{optional
<short>()};
88 static_assert(!std::is_convertible_v
<optional
<short>&&, optional
<Widget
>>);
90 optional
<Widget
> w6
{optional
<optional
<int>>()};
92 optional
<Widget
> w7
= optional
<optional
<int>>();
94 optional
<Widget
> w8
{optional
<optional
<short>>()};
96 static_assert(!std::is_convertible_v
<optional
<optional
<short>>&&,
98 optional
<Widget
> w10
{optional
<optional
<short>>(10)};
100 optional
<Widget
> w11
= std::nullopt
;
102 optional
<Widget
> w12
= {};
104 optional
<Widget
> w13
{std::nullopt
};
106 optional
<Widget
> w14
;
114 Widget2(optional
<int>) {}
115 Widget2
& operator=(int) {return *this;}
116 Widget2
& operator=(optional
<int>) {return *this;}
124 w
= optional
<short>();
126 w
= optional
<optional
<int>>();
128 w
= optional
<optional
<short>>();
130 w
= optional
<optional
<short>>(10);
131 optional
<Widget2
> w2
= std::nullopt
;
133 optional
<Widget2
> w3
= {};
135 optional
<Widget2
> w4
{std::nullopt
};
137 optional
<Widget2
> w5
;
152 static_assert(!std::is_assignable_v
<optional
<Thingy
>&,
154 static_assert(std::is_assignable_v
<optional
<Thingy
>&,
156 static_assert(!std::is_assignable_v
<optional
<Thingy
>&,
157 optional
<optional
<int>>>);
158 ot
= optional
<Widget
>();
160 optional
<Thingy
> ot2
{optional
<int>()};
162 static_assert(!std::is_convertible_v
<optional
<int>&&,
164 optional
<Thingy
> ot3
{optional
<short>()};
166 optional
<Thingy
> ot4
= optional
<short>();
169 optional
<Thingy
> ot5
{optional
<optional
<int>>()};
171 static_assert(!std::is_convertible_v
<optional
<optional
<int>>&&,
174 optional
<Thingy
> ot7
{optional
<Widget
>()};
176 optional
<Thingy
> ot8
= optional
<Widget
>();
178 static_assert(!std::is_constructible_v
<optional
<Thingy
>,
179 optional
<optional
<short>>>);
180 static_assert(!std::is_convertible_v
<optional
<optional
<short>>,
182 static_assert(!std::is_assignable_v
<optional
<Thingy
>&,
183 optional
<optional
<short>>>);
184 optional
<Thingy
> ot9
= std::nullopt
;
186 optional
<Thingy
> ot10
= {};
188 optional
<Thingy
> ot11
{std::nullopt
};
190 optional
<Thingy
> ot12
;
195 struct RvalueConstructible
197 RvalueConstructible(int) {}
198 RvalueConstructible(optional
<int>&&) {}
204 optional
<RvalueConstructible
> ori
;
205 static_assert(!std::is_assignable_v
<optional
<RvalueConstructible
>&,
210 optional
<optional
<int>> ooi
;
211 static_assert(!std::is_assignable_v
<optional
<RvalueConstructible
>&,
212 optional
<optional
<int>>&>);
213 ori
= std::move(ooi
);
216 static_assert(!std::is_constructible_v
<optional
<RvalueConstructible
>,
218 static_assert(!std::is_convertible_v
<optional
<int>&,
219 optional
<RvalueConstructible
>>);
221 optional
<RvalueConstructible
> ori2(std::move(oi
));
223 optional
<RvalueConstructible
> ori3
= std::move(oi
);
226 static_assert(!std::is_constructible_v
<optional
<RvalueConstructible
>,
227 optional
<optional
<int>>&>);
228 static_assert(!std::is_convertible_v
<optional
<optional
<int>>&,
229 optional
<RvalueConstructible
>>);
230 optional
<RvalueConstructible
> ori6(std::move(ooi
));
232 optional
<RvalueConstructible
> ori7
= std::move(ooi
);
234 optional
<RvalueConstructible
> ori8
= std::nullopt
;
236 optional
<RvalueConstructible
> ori9
= {};
238 optional
<RvalueConstructible
> ori10
{std::nullopt
};
240 optional
<RvalueConstructible
> ori11
;
248 explicit Thingy2(optional
<int>) {}
254 optional
<Thingy2
> ot
{optional
<int>{}};
256 static_assert(!std::is_convertible_v
<optional
<int>,
258 optional
<Thingy2
> ot2
{optional
<short>{}};
260 static_assert(!std::is_convertible_v
<optional
<short>,
262 optional
<Thingy2
> ot3
{optional
<optional
<int>>{}};
264 static_assert(!std::is_convertible_v
<optional
<optional
<int>>,
266 optional
<Thingy2
> ot4
{optional
<optional
<short>>{}};
268 static_assert(!std::is_convertible_v
<optional
<optional
<short>>,
271 optional
<Thingy2
> ot5
{optional
<Widget
>{}};
273 optional
<Thingy2
> ot6
= optional
<Widget
>();
276 static_assert(!std::is_assignable_v
<optional
<Thingy2
>&,
278 static_assert(!std::is_assignable_v
<optional
<Thingy2
>&,
280 static_assert(!std::is_assignable_v
<optional
<Thingy2
>&,
281 optional
<optional
<int>>>);
282 static_assert(!std::is_assignable_v
<optional
<Thingy2
>&,
283 optional
<optional
<short>>>);
284 optional
<Thingy2
> ot7
;
285 ot
= optional
<Widget
>();
287 optional
<Thingy2
> ot8
= std::nullopt
;
289 optional
<Thingy2
> ot9
= {};
291 optional
<Thingy2
> ot10
{std::nullopt
};
293 optional
<Thingy2
> ot11
;
301 template<class... Args
,
302 std::enable_if_t
<std::is_constructible_v
<Widget
, Args
&&...>,
304 explicit Thingy3(Args
&&... args
) {}
310 optional
<Thingy3
> ot
{optional
<int>{}};
312 static_assert(!std::is_convertible_v
<optional
<int>,
314 optional
<Thingy3
> ot2
{optional
<short>{}};
316 static_assert(!std::is_convertible_v
<optional
<short>,
318 optional
<Thingy3
> ot3
{optional
<optional
<int>>{}};
320 static_assert(!std::is_convertible_v
<optional
<optional
<int>>,
322 optional
<Thingy3
> ot4
{optional
<optional
<short>>{}};
324 static_assert(!std::is_convertible_v
<optional
<optional
<short>>,
327 optional
<Thingy3
> ot5
{optional
<Widget
>{}};
329 optional
<Thingy3
> ot6
= optional
<Widget
>();
332 static_assert(!std::is_assignable_v
<optional
<Thingy3
>&,
334 static_assert(!std::is_assignable_v
<optional
<Thingy3
>&,
336 static_assert(!std::is_assignable_v
<optional
<Thingy3
>&,
337 optional
<optional
<int>>>);
338 static_assert(!std::is_assignable_v
<optional
<Thingy3
>&,
339 optional
<optional
<short>>>);
340 optional
<Thingy3
> ot7
;
341 ot
= optional
<Widget
>();
343 optional
<Thingy3
> ot8
= std::nullopt
;
345 optional
<Thingy3
> ot9
= {};
347 optional
<Thingy3
> ot10
{std::nullopt
};
349 optional
<Thingy3
> ot11
;
357 optional
<std::any
> oa2
= a
;
359 VERIFY(std::any_cast
<int>(*oa2
) == 42);
360 optional
<std::any
> oa3
= oa2
;
362 VERIFY(std::any_cast
<int>(*oa3
) == 42);
363 optional
<std::any
> oa4
{oa2
};
365 VERIFY(std::any_cast
<int>(*oa4
) == 42);
366 optional
<std::any
> oa5(oa2
);
368 VERIFY(std::any_cast
<int>(*oa5
) == 42);
369 optional
<std::any
> oa6
;
371 optional
<std::any
> oa7
= oa6
;
373 optional
<std::any
> oa8
{oa6
};
375 optional
<std::any
> oa9(oa6
);
382 optional
<int> oi(std::in_place
);
384 VERIFY(oi
.has_value() == false);
385 optional
<X
> ot(std::in_place
);
387 VERIFY(ot
.has_value() == false);
388 optional
<int> oi2(std::in_place
);