libstdc++
expected
Go to the documentation of this file.
1// <expected> -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/expected
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#define __glibcxx_want_expected
37#define __glibcxx_want_freestanding_expected
38#define __glibcxx_want_constrained_equality
39#include <bits/version.h>
40
41#ifdef __cpp_lib_expected // C++ >= 23 && __cpp_concepts >= 202002L
42#include <initializer_list>
43#include <bits/exception.h> // exception
44#include <bits/invoke.h> // __invoke
45#include <bits/stl_construct.h> // construct_at
46#include <bits/utility.h> // in_place_t
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52 /**
53 * @defgroup expected_values Expected values
54 * @addtogroup utilities
55 * @since C++23
56 * @{
57 */
58
59 /// Discriminated union that holds an expected value or an error value.
60 /**
61 * @since C++23
62 */
63 template<typename _Tp, typename _Er>
64 class expected;
65
66 /// Wrapper type used to pass an error value to a `std::expected`.
67 /**
68 * @since C++23
69 */
70 template<typename _Er>
71 class unexpected;
72
73 /// Exception thrown by std::expected when the value() is not present.
74 /**
75 * @since C++23
76 */
77 template<typename _Er>
78 class bad_expected_access;
79
80 template<>
81 class bad_expected_access<void> : public exception
82 {
83 protected:
84 bad_expected_access() noexcept { }
85 bad_expected_access(const bad_expected_access&) noexcept = default;
86 bad_expected_access(bad_expected_access&&) noexcept = default;
87 bad_expected_access& operator=(const bad_expected_access&) noexcept = default;
88 bad_expected_access& operator=(bad_expected_access&&) noexcept = default;
89 ~bad_expected_access() = default;
90
91 public:
92
93 [[nodiscard]]
94 const char*
95 what() const noexcept override
96 { return "bad access to std::expected without expected value"; }
97 };
98
99 template<typename _Er>
100 class bad_expected_access : public bad_expected_access<void> {
101 public:
102 explicit
103 bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
104
105 // XXX const char* what() const noexcept override;
106
107 [[nodiscard]]
108 _Er&
109 error() & noexcept
110 { return _M_unex; }
111
112 [[nodiscard]]
113 const _Er&
114 error() const & noexcept
115 { return _M_unex; }
116
117 [[nodiscard]]
118 _Er&&
119 error() && noexcept
120 { return std::move(_M_unex); }
121
122 [[nodiscard]]
123 const _Er&&
124 error() const && noexcept
125 { return std::move(_M_unex); }
126
127 private:
128 _Er _M_unex;
129 };
130
131 /// Tag type for constructing unexpected values in a std::expected
132 /**
133 * @since C++23
134 */
135 struct unexpect_t
136 {
137 explicit unexpect_t() = default;
138 };
139
140 /// Tag for constructing unexpected values in a std::expected
141 /**
142 * @since C++23
143 */
144 inline constexpr unexpect_t unexpect{};
145
146/// @cond undocumented
147namespace __expected
148{
149 template<typename _Tp>
150 constexpr bool __is_expected = false;
151 template<typename _Tp, typename _Er>
152 constexpr bool __is_expected<expected<_Tp, _Er>> = true;
153
154 template<typename _Tp>
155 constexpr bool __is_unexpected = false;
156 template<typename _Tp>
157 constexpr bool __is_unexpected<unexpected<_Tp>> = true;
158
159 template<typename _Fn, typename _Tp>
160 using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
161 template<typename _Fn, typename _Tp>
162 using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
163 template<typename _Fn>
164 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
165 template<typename _Fn>
166 using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
167
168 template<typename _Er>
169 concept __can_be_unexpected
170 = is_object_v<_Er> && (!is_array_v<_Er>)
171 && (!__expected::__is_unexpected<_Er>)
172 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
173
174 // Tag types for in-place construction from an invocation result.
175 struct __in_place_inv { };
176 struct __unexpect_inv { };
177}
178/// @endcond
179
180 template<typename _Er>
181 class unexpected
182 {
183 static_assert( __expected::__can_be_unexpected<_Er> );
184
185 public:
186 constexpr unexpected(const unexpected&) = default;
187 constexpr unexpected(unexpected&&) = default;
188
189 template<typename _Err = _Er>
190 requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
191 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
192 && is_constructible_v<_Er, _Err>
193 constexpr explicit
194 unexpected(_Err&& __e)
195 noexcept(is_nothrow_constructible_v<_Er, _Err>)
196 : _M_unex(std::forward<_Err>(__e))
197 { }
198
199 template<typename... _Args>
200 requires is_constructible_v<_Er, _Args...>
201 constexpr explicit
202 unexpected(in_place_t, _Args&&... __args)
203 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
204 : _M_unex(std::forward<_Args>(__args)...)
205 { }
206
207 template<typename _Up, typename... _Args>
208 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
209 constexpr explicit
210 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
211 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
212 _Args...>)
213 : _M_unex(__il, std::forward<_Args>(__args)...)
214 { }
215
216 constexpr unexpected& operator=(const unexpected&) = default;
217 constexpr unexpected& operator=(unexpected&&) = default;
218
219
220 [[nodiscard]]
221 constexpr const _Er&
222 error() const & noexcept { return _M_unex; }
223
224 [[nodiscard]]
225 constexpr _Er&
226 error() & noexcept { return _M_unex; }
227
228 [[nodiscard]]
229 constexpr const _Er&&
230 error() const && noexcept { return std::move(_M_unex); }
231
232 [[nodiscard]]
233 constexpr _Er&&
234 error() && noexcept { return std::move(_M_unex); }
235
236 constexpr void
237 swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
238 requires is_swappable_v<_Er>
239 {
240 using std::swap;
241 swap(_M_unex, __other._M_unex);
242 }
243
244 template<typename _Err>
245 [[nodiscard]]
246 friend constexpr bool
247 operator==(const unexpected& __x, const unexpected<_Err>& __y)
248 { return __x._M_unex == __y.error(); }
249
250 friend constexpr void
251 swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
252 requires is_swappable_v<_Er>
253 { __x.swap(__y); }
254
255 private:
256 _Er _M_unex;
257 };
258
259 template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
260
261/// @cond undocumented
262namespace __expected
263{
264 template<typename _Tp>
265 struct _Guard
266 {
267 static_assert( is_nothrow_move_constructible_v<_Tp> );
268
269 constexpr explicit
270 _Guard(_Tp& __x)
271 : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
272 { std::destroy_at(_M_guarded); }
273
274 constexpr
275 ~_Guard()
276 {
277 if (_M_guarded) [[unlikely]]
278 std::construct_at(_M_guarded, std::move(_M_tmp));
279 }
280
281 _Guard(const _Guard&) = delete;
282 _Guard& operator=(const _Guard&) = delete;
283
284 constexpr _Tp&&
285 release() noexcept
286 {
287 _M_guarded = nullptr;
288 return std::move(_M_tmp);
289 }
290
291 private:
292 _Tp* _M_guarded;
293 _Tp _M_tmp;
294 };
295
296 // reinit-expected helper from [expected.object.assign]
297 template<typename _Tp, typename _Up, typename _Vp>
298 constexpr void
299 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
300 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
301 {
302 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
303 {
304 std::destroy_at(__oldval);
305 std::construct_at(__newval, std::forward<_Vp>(__arg));
306 }
307 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
308 {
309 _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
310 std::destroy_at(__oldval);
311 std::construct_at(__newval, std::move(__tmp));
312 }
313 else
314 {
315 _Guard<_Up> __guard(*__oldval);
316 std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
317 __guard.release();
318 }
319 }
320
321 // _GLIBCXX_RESOLVE_LIB_DEFECTS
322 // 3836. std::expected<bool, E1> conversion constructor
323 // expected(const expected<U, G>&) should take precedence over
324 // expected(U&&) with operator bool
325
326 // If T is cv bool, remove_cvref_t<U> is not a specialization of expected.
327 template<typename _Tp, typename _Up>
328 concept __not_constructing_bool_from_expected
329 = ! is_same_v<remove_cv_t<_Tp>, bool>
330 || ! __is_expected<remove_cvref_t<_Up>>;
331}
332/// @endcond
333
334 template<typename _Tp, typename _Er>
335 class expected
336 {
337 static_assert( ! is_reference_v<_Tp> );
338 static_assert( ! is_function_v<_Tp> );
339 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
340 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
341 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
342 static_assert( __expected::__can_be_unexpected<_Er> );
343
344 // If T is not cv bool, converts-from-any-cvref<T, expected<U, G>> and
345 // is_constructible<unexpected<E>, cv expected<U, G> ref-qual> are false.
346 template<typename _Up, typename _Gr, typename _Unex = unexpected<_Er>,
347 typename = remove_cv_t<_Tp>>
348 static constexpr bool __cons_from_expected
349 = __or_v<is_constructible<_Tp, expected<_Up, _Gr>&>,
350 is_constructible<_Tp, expected<_Up, _Gr>>,
351 is_constructible<_Tp, const expected<_Up, _Gr>&>,
352 is_constructible<_Tp, const expected<_Up, _Gr>>,
353 is_convertible<expected<_Up, _Gr>&, _Tp>,
354 is_convertible<expected<_Up, _Gr>, _Tp>,
355 is_convertible<const expected<_Up, _Gr>&, _Tp>,
356 is_convertible<const expected<_Up, _Gr>, _Tp>,
357 is_constructible<_Unex, expected<_Up, _Gr>&>,
358 is_constructible<_Unex, expected<_Up, _Gr>>,
359 is_constructible<_Unex, const expected<_Up, _Gr>&>,
360 is_constructible<_Unex, const expected<_Up, _Gr>>
361 >;
362
363 // _GLIBCXX_RESOLVE_LIB_DEFECTS
364 // If t is cv bool, we know it can be constructed from expected<U, G>,
365 // but we don't want to cause the expected(U&&) constructor to be used,
366 // so we only check the is_constructible<unexpected<E>, ...> cases.
367 template<typename _Up, typename _Gr, typename _Unex>
368 static constexpr bool __cons_from_expected<_Up, _Gr, _Unex, bool>
369 = __or_v<is_constructible<_Unex, expected<_Up, _Gr>&>,
370 is_constructible<_Unex, expected<_Up, _Gr>>,
371 is_constructible<_Unex, const expected<_Up, _Gr>&>,
372 is_constructible<_Unex, const expected<_Up, _Gr>>
373 >;
374
375 template<typename _Up, typename _Gr>
376 constexpr static bool __explicit_conv
377 = __or_v<__not_<is_convertible<_Up, _Tp>>,
378 __not_<is_convertible<_Gr, _Er>>
379 >;
380
381 template<typename _Up>
382 static constexpr bool __same_val
383 = is_same_v<typename _Up::value_type, _Tp>;
384
385 template<typename _Up>
386 static constexpr bool __same_err
387 = is_same_v<typename _Up::error_type, _Er>;
388
389 public:
390 using value_type = _Tp;
391 using error_type = _Er;
392 using unexpected_type = unexpected<_Er>;
393
394 template<typename _Up>
395 using rebind = expected<_Up, error_type>;
396
397 constexpr
398 expected()
399 noexcept(is_nothrow_default_constructible_v<_Tp>)
400 requires is_default_constructible_v<_Tp>
401 : _M_val(), _M_has_value(true)
402 { }
403
404 expected(const expected&) = default;
405
406 constexpr
407 expected(const expected& __x)
408 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
409 is_nothrow_copy_constructible<_Er>>)
410 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
411 && (!is_trivially_copy_constructible_v<_Tp>
412 || !is_trivially_copy_constructible_v<_Er>)
413 : _M_has_value(__x._M_has_value)
414 {
415 if (_M_has_value)
416 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
417 else
418 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
419 }
420
421 expected(expected&&) = default;
422
423 constexpr
424 expected(expected&& __x)
425 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
426 is_nothrow_move_constructible<_Er>>)
427 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
428 && (!is_trivially_move_constructible_v<_Tp>
429 || !is_trivially_move_constructible_v<_Er>)
430 : _M_has_value(__x._M_has_value)
431 {
432 if (_M_has_value)
433 std::construct_at(__builtin_addressof(_M_val),
434 std::move(__x)._M_val);
435 else
436 std::construct_at(__builtin_addressof(_M_unex),
437 std::move(__x)._M_unex);
438 }
439
440 template<typename _Up, typename _Gr>
441 requires is_constructible_v<_Tp, const _Up&>
442 && is_constructible_v<_Er, const _Gr&>
443 && (!__cons_from_expected<_Up, _Gr>)
444 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
445 expected(const expected<_Up, _Gr>& __x)
446 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
447 is_nothrow_constructible<_Er, const _Gr&>>)
448 : _M_has_value(__x._M_has_value)
449 {
450 if (_M_has_value)
451 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
452 else
453 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
454 }
455
456 template<typename _Up, typename _Gr>
457 requires is_constructible_v<_Tp, _Up>
458 && is_constructible_v<_Er, _Gr>
459 && (!__cons_from_expected<_Up, _Gr>)
460 constexpr explicit(__explicit_conv<_Up, _Gr>)
461 expected(expected<_Up, _Gr>&& __x)
462 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
463 is_nothrow_constructible<_Er, _Gr>>)
464 : _M_has_value(__x._M_has_value)
465 {
466 if (_M_has_value)
467 std::construct_at(__builtin_addressof(_M_val),
468 std::move(__x)._M_val);
469 else
470 std::construct_at(__builtin_addressof(_M_unex),
471 std::move(__x)._M_unex);
472 }
473
474 template<typename _Up = remove_cv_t<_Tp>>
475 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
476 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
477 && (!is_same_v<remove_cvref_t<_Up>, unexpect_t>)
478 && is_constructible_v<_Tp, _Up>
479 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
480 && __expected::__not_constructing_bool_from_expected<_Tp, _Up>
481 constexpr explicit(!is_convertible_v<_Up, _Tp>)
482 expected(_Up&& __v)
483 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
484 : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
485 { }
486
487 template<typename _Gr = _Er>
488 requires is_constructible_v<_Er, const _Gr&>
489 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
490 expected(const unexpected<_Gr>& __u)
491 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
492 : _M_unex(__u.error()), _M_has_value(false)
493 { }
494
495 template<typename _Gr = _Er>
496 requires is_constructible_v<_Er, _Gr>
497 constexpr explicit(!is_convertible_v<_Gr, _Er>)
498 expected(unexpected<_Gr>&& __u)
499 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
500 : _M_unex(std::move(__u).error()), _M_has_value(false)
501 { }
502
503 template<typename... _Args>
504 requires is_constructible_v<_Tp, _Args...>
505 constexpr explicit
506 expected(in_place_t, _Args&&... __args)
507 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
508 : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
509 { }
510
511 template<typename _Up, typename... _Args>
512 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
513 constexpr explicit
514 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
515 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
516 _Args...>)
517 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
518 { }
519
520 template<typename... _Args>
521 requires is_constructible_v<_Er, _Args...>
522 constexpr explicit
523 expected(unexpect_t, _Args&&... __args)
524 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
525 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
526 { }
527
528 template<typename _Up, typename... _Args>
529 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
530 constexpr explicit
531 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
532 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
533 _Args...>)
534 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
535 { }
536
537 constexpr ~expected() = default;
538
539 constexpr ~expected()
540 requires (!is_trivially_destructible_v<_Tp>)
541 || (!is_trivially_destructible_v<_Er>)
542 {
543 if (_M_has_value)
544 std::destroy_at(__builtin_addressof(_M_val));
545 else
546 std::destroy_at(__builtin_addressof(_M_unex));
547 }
548
549 // assignment
550
551 expected& operator=(const expected&) = delete;
552
553 constexpr expected&
554 operator=(const expected& __x)
555 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
556 is_nothrow_copy_constructible<_Er>,
557 is_nothrow_copy_assignable<_Tp>,
558 is_nothrow_copy_assignable<_Er>>)
559 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
560 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
561 && (is_nothrow_move_constructible_v<_Tp>
562 || is_nothrow_move_constructible_v<_Er>)
563 {
564 if (__x._M_has_value)
565 this->_M_assign_val(__x._M_val);
566 else
567 this->_M_assign_unex(__x._M_unex);
568 return *this;
569 }
570
571 constexpr expected&
572 operator=(expected&& __x)
573 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
574 is_nothrow_move_constructible<_Er>,
575 is_nothrow_move_assignable<_Tp>,
576 is_nothrow_move_assignable<_Er>>)
577 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
578 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
579 && (is_nothrow_move_constructible_v<_Tp>
580 || is_nothrow_move_constructible_v<_Er>)
581 {
582 if (__x._M_has_value)
583 _M_assign_val(std::move(__x._M_val));
584 else
585 _M_assign_unex(std::move(__x._M_unex));
586 return *this;
587 }
588
589 template<typename _Up = remove_cv_t<_Tp>>
590 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
591 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
592 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
593 && (is_nothrow_constructible_v<_Tp, _Up>
594 || is_nothrow_move_constructible_v<_Tp>
595 || is_nothrow_move_constructible_v<_Er>)
596 constexpr expected&
597 operator=(_Up&& __v)
598 {
599 _M_assign_val(std::forward<_Up>(__v));
600 return *this;
601 }
602
603 template<typename _Gr>
604 requires is_constructible_v<_Er, const _Gr&>
605 && is_assignable_v<_Er&, const _Gr&>
606 && (is_nothrow_constructible_v<_Er, const _Gr&>
607 || is_nothrow_move_constructible_v<_Tp>
608 || is_nothrow_move_constructible_v<_Er>)
609 constexpr expected&
610 operator=(const unexpected<_Gr>& __e)
611 {
612 _M_assign_unex(__e.error());
613 return *this;
614 }
615
616 template<typename _Gr>
617 requires is_constructible_v<_Er, _Gr>
618 && is_assignable_v<_Er&, _Gr>
619 && (is_nothrow_constructible_v<_Er, _Gr>
620 || is_nothrow_move_constructible_v<_Tp>
621 || is_nothrow_move_constructible_v<_Er>)
622 constexpr expected&
623 operator=(unexpected<_Gr>&& __e)
624 {
625 _M_assign_unex(std::move(__e).error());
626 return *this;
627 }
628
629 // modifiers
630
631 template<typename... _Args>
632 requires is_nothrow_constructible_v<_Tp, _Args...>
633 constexpr _Tp&
634 emplace(_Args&&... __args) noexcept
635 {
636 if (_M_has_value)
637 std::destroy_at(__builtin_addressof(_M_val));
638 else
639 {
640 std::destroy_at(__builtin_addressof(_M_unex));
641 _M_has_value = true;
642 }
643 std::construct_at(__builtin_addressof(_M_val),
644 std::forward<_Args>(__args)...);
645 return _M_val;
646 }
647
648 template<typename _Up, typename... _Args>
649 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
650 _Args...>
651 constexpr _Tp&
652 emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
653 {
654 if (_M_has_value)
655 std::destroy_at(__builtin_addressof(_M_val));
656 else
657 {
658 std::destroy_at(__builtin_addressof(_M_unex));
659 _M_has_value = true;
660 }
661 std::construct_at(__builtin_addressof(_M_val),
662 __il, std::forward<_Args>(__args)...);
663 return _M_val;
664 }
665
666 // swap
667 constexpr void
668 swap(expected& __x)
669 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
670 is_nothrow_move_constructible<_Er>,
671 is_nothrow_swappable<_Tp&>,
672 is_nothrow_swappable<_Er&>>)
673 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
674 && is_move_constructible_v<_Tp>
675 && is_move_constructible_v<_Er>
676 && (is_nothrow_move_constructible_v<_Tp>
677 || is_nothrow_move_constructible_v<_Er>)
678 {
679 if (_M_has_value)
680 {
681 if (__x._M_has_value)
682 {
683 using std::swap;
684 swap(_M_val, __x._M_val);
685 }
686 else
687 this->_M_swap_val_unex(__x);
688 }
689 else
690 {
691 if (__x._M_has_value)
692 __x._M_swap_val_unex(*this);
693 else
694 {
695 using std::swap;
696 swap(_M_unex, __x._M_unex);
697 }
698 }
699 }
700
701 // observers
702
703 [[nodiscard]]
704 constexpr const _Tp*
705 operator->() const noexcept
706 {
707 __glibcxx_assert(_M_has_value);
708 return __builtin_addressof(_M_val);
709 }
710
711 [[nodiscard]]
712 constexpr _Tp*
713 operator->() noexcept
714 {
715 __glibcxx_assert(_M_has_value);
716 return __builtin_addressof(_M_val);
717 }
718
719 [[nodiscard]]
720 constexpr const _Tp&
721 operator*() const & noexcept
722 {
723 __glibcxx_assert(_M_has_value);
724 return _M_val;
725 }
726
727 [[nodiscard]]
728 constexpr _Tp&
729 operator*() & noexcept
730 {
731 __glibcxx_assert(_M_has_value);
732 return _M_val;
733 }
734
735 [[nodiscard]]
736 constexpr const _Tp&&
737 operator*() const && noexcept
738 {
739 __glibcxx_assert(_M_has_value);
740 return std::move(_M_val);
741 }
742
743 [[nodiscard]]
744 constexpr _Tp&&
745 operator*() && noexcept
746 {
747 __glibcxx_assert(_M_has_value);
748 return std::move(_M_val);
749 }
750
751 [[nodiscard]]
752 constexpr explicit
753 operator bool() const noexcept { return _M_has_value; }
754
755 [[nodiscard]]
756 constexpr bool has_value() const noexcept { return _M_has_value; }
757
758 constexpr const _Tp&
759 value() const &
760 {
761 static_assert( is_copy_constructible_v<_Er> );
762 if (_M_has_value) [[likely]]
763 return _M_val;
764 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
765 }
766
767 constexpr _Tp&
768 value() &
769 {
770 static_assert( is_copy_constructible_v<_Er> );
771 if (_M_has_value) [[likely]]
772 return _M_val;
773 const auto& __unex = _M_unex;
774 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
775 }
776
777 constexpr const _Tp&&
778 value() const &&
779 {
780 static_assert( is_copy_constructible_v<_Er> );
781 static_assert( is_constructible_v<_Er, const _Er&&> );
782 if (_M_has_value) [[likely]]
783 return std::move(_M_val);
784 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
785 }
786
787 constexpr _Tp&&
788 value() &&
789 {
790 static_assert( is_copy_constructible_v<_Er> );
791 static_assert( is_constructible_v<_Er, _Er&&> );
792 if (_M_has_value) [[likely]]
793 return std::move(_M_val);
794 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
795 }
796
797 constexpr const _Er&
798 error() const & noexcept
799 {
800 __glibcxx_assert(!_M_has_value);
801 return _M_unex;
802 }
803
804 constexpr _Er&
805 error() & noexcept
806 {
807 __glibcxx_assert(!_M_has_value);
808 return _M_unex;
809 }
810
811 constexpr const _Er&&
812 error() const && noexcept
813 {
814 __glibcxx_assert(!_M_has_value);
815 return std::move(_M_unex);
816 }
817
818 constexpr _Er&&
819 error() && noexcept
820 {
821 __glibcxx_assert(!_M_has_value);
822 return std::move(_M_unex);
823 }
824
825 // _GLIBCXX_RESOLVE_LIB_DEFECTS
826 // 4406. value_or return statement is inconsistent with Mandates
827 template<typename _Up = remove_cv_t<_Tp>>
828 constexpr remove_cv_t<_Tp>
829 value_or(_Up&& __v) const &
830 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
831 is_nothrow_convertible<_Up, _Tp>>)
832 {
833 using _Xp = remove_cv_t<_Tp>;
834 static_assert( is_convertible_v<const _Tp&, _Xp> );
835 static_assert( is_convertible_v<_Up, _Tp> );
836
837 if (_M_has_value)
838 return _M_val;
839 return std::forward<_Up>(__v);
840 }
841
842 template<typename _Up = remove_cv_t<_Tp>>
843 constexpr remove_cv_t<_Tp>
844 value_or(_Up&& __v) &&
845 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
846 is_nothrow_convertible<_Up, _Tp>>)
847 {
848 using _Xp = remove_cv_t<_Tp>;
849 static_assert( is_convertible_v<_Tp, _Xp> );
850 static_assert( is_convertible_v<_Up, _Xp> );
851
852 if (_M_has_value)
853 return std::move(_M_val);
854 return std::forward<_Up>(__v);
855 }
856
857 template<typename _Gr = _Er>
858 constexpr _Er
859 error_or(_Gr&& __e) const&
860 {
861 static_assert( is_copy_constructible_v<_Er> );
862 static_assert( is_convertible_v<_Gr, _Er> );
863
864 if (_M_has_value)
865 return std::forward<_Gr>(__e);
866 return _M_unex;
867 }
868
869 template<typename _Gr = _Er>
870 constexpr _Er
871 error_or(_Gr&& __e) &&
872 {
873 static_assert( is_move_constructible_v<_Er> );
874 static_assert( is_convertible_v<_Gr, _Er> );
875
876 if (_M_has_value)
877 return std::forward<_Gr>(__e);
878 return std::move(_M_unex);
879 }
880
881 // monadic operations
882
883 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
884 constexpr auto
885 and_then(_Fn&& __f) &
886 {
887 using _Up = __expected::__result<_Fn, _Tp&>;
888 static_assert(__expected::__is_expected<_Up>,
889 "the function passed to std::expected<T, E>::and_then "
890 "must return a std::expected");
891 static_assert(is_same_v<typename _Up::error_type, _Er>,
892 "the function passed to std::expected<T, E>::and_then "
893 "must return a std::expected with the same error_type");
894
895 if (has_value())
896 return std::__invoke(std::forward<_Fn>(__f), _M_val);
897 else
898 return _Up(unexpect, _M_unex);
899 }
900
901 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
902 constexpr auto
903 and_then(_Fn&& __f) const &
904 {
905 using _Up = __expected::__result<_Fn, const _Tp&>;
906 static_assert(__expected::__is_expected<_Up>,
907 "the function passed to std::expected<T, E>::and_then "
908 "must return a std::expected");
909 static_assert(is_same_v<typename _Up::error_type, _Er>,
910 "the function passed to std::expected<T, E>::and_then "
911 "must return a std::expected with the same error_type");
912
913 if (has_value())
914 return std::__invoke(std::forward<_Fn>(__f), _M_val);
915 else
916 return _Up(unexpect, _M_unex);
917 }
918
919 template<typename _Fn> requires is_constructible_v<_Er, _Er>
920 constexpr auto
921 and_then(_Fn&& __f) &&
922 {
923 using _Up = __expected::__result<_Fn, _Tp&&>;
924 static_assert(__expected::__is_expected<_Up>,
925 "the function passed to std::expected<T, E>::and_then "
926 "must return a std::expected");
927 static_assert(is_same_v<typename _Up::error_type, _Er>,
928 "the function passed to std::expected<T, E>::and_then "
929 "must return a std::expected with the same error_type");
930
931 if (has_value())
932 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
933 else
934 return _Up(unexpect, std::move(_M_unex));
935 }
936
937
938 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
939 constexpr auto
940 and_then(_Fn&& __f) const &&
941 {
942 using _Up = __expected::__result<_Fn, const _Tp&&>;
943 static_assert(__expected::__is_expected<_Up>,
944 "the function passed to std::expected<T, E>::and_then "
945 "must return a std::expected");
946 static_assert(is_same_v<typename _Up::error_type, _Er>,
947 "the function passed to std::expected<T, E>::and_then "
948 "must return a std::expected with the same error_type");
949
950 if (has_value())
951 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
952 else
953 return _Up(unexpect, std::move(_M_unex));
954 }
955
956 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
957 constexpr auto
958 or_else(_Fn&& __f) &
959 {
960 using _Gr = __expected::__result<_Fn, _Er&>;
961 static_assert(__expected::__is_expected<_Gr>,
962 "the function passed to std::expected<T, E>::or_else "
963 "must return a std::expected");
964 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
965 "the function passed to std::expected<T, E>::or_else "
966 "must return a std::expected with the same value_type");
967
968 if (has_value())
969 return _Gr(in_place, _M_val);
970 else
971 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
972 }
973
974 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
975 constexpr auto
976 or_else(_Fn&& __f) const &
977 {
978 using _Gr = __expected::__result<_Fn, const _Er&>;
979 static_assert(__expected::__is_expected<_Gr>,
980 "the function passed to std::expected<T, E>::or_else "
981 "must return a std::expected");
982 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
983 "the function passed to std::expected<T, E>::or_else "
984 "must return a std::expected with the same value_type");
985
986 if (has_value())
987 return _Gr(in_place, _M_val);
988 else
989 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
990 }
991
992
993 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
994 constexpr auto
995 or_else(_Fn&& __f) &&
996 {
997 using _Gr = __expected::__result<_Fn, _Er&&>;
998 static_assert(__expected::__is_expected<_Gr>,
999 "the function passed to std::expected<T, E>::or_else "
1000 "must return a std::expected");
1001 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1002 "the function passed to std::expected<T, E>::or_else "
1003 "must return a std::expected with the same value_type");
1004
1005 if (has_value())
1006 return _Gr(in_place, std::move(_M_val));
1007 else
1008 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1009 }
1010
1011 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1012 constexpr auto
1013 or_else(_Fn&& __f) const &&
1014 {
1015 using _Gr = __expected::__result<_Fn, const _Er&&>;
1016 static_assert(__expected::__is_expected<_Gr>,
1017 "the function passed to std::expected<T, E>::or_else "
1018 "must return a std::expected");
1019 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1020 "the function passed to std::expected<T, E>::or_else "
1021 "must return a std::expected with the same value_type");
1022
1023 if (has_value())
1024 return _Gr(in_place, std::move(_M_val));
1025 else
1026 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1027 }
1028
1029 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1030 constexpr auto
1031 transform(_Fn&& __f) &
1032 {
1033 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1034 using _Res = expected<_Up, _Er>;
1035
1036 if (has_value())
1037 return _Res(__in_place_inv{}, [&]() {
1038 return std::__invoke(std::forward<_Fn>(__f),
1039 _M_val);
1040 });
1041 else
1042 return _Res(unexpect, _M_unex);
1043 }
1044
1045 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1046 constexpr auto
1047 transform(_Fn&& __f) const &
1048 {
1049 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1050 using _Res = expected<_Up, _Er>;
1051
1052 if (has_value())
1053 return _Res(__in_place_inv{}, [&]() {
1054 return std::__invoke(std::forward<_Fn>(__f),
1055 _M_val);
1056 });
1057 else
1058 return _Res(unexpect, _M_unex);
1059 }
1060
1061 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1062 constexpr auto
1063 transform(_Fn&& __f) &&
1064 {
1065 using _Up = __expected::__result_xform<_Fn, _Tp>;
1066 using _Res = expected<_Up, _Er>;
1067
1068 if (has_value())
1069 return _Res(__in_place_inv{}, [&]() {
1070 return std::__invoke(std::forward<_Fn>(__f),
1071 std::move(_M_val));
1072 });
1073 else
1074 return _Res(unexpect, std::move(_M_unex));
1075 }
1076
1077 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1078 constexpr auto
1079 transform(_Fn&& __f) const &&
1080 {
1081 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1082 using _Res = expected<_Up, _Er>;
1083
1084 if (has_value())
1085 return _Res(__in_place_inv{}, [&]() {
1086 return std::__invoke(std::forward<_Fn>(__f),
1087 std::move(_M_val));
1088 });
1089 else
1090 return _Res(unexpect, std::move(_M_unex));
1091 }
1092
1093 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
1094 constexpr auto
1095 transform_error(_Fn&& __f) &
1096 {
1097 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1098 using _Res = expected<_Tp, _Gr>;
1099
1100 if (has_value())
1101 return _Res(in_place, _M_val);
1102 else
1103 return _Res(__unexpect_inv{}, [&]() {
1104 return std::__invoke(std::forward<_Fn>(__f),
1105 _M_unex);
1106 });
1107 }
1108
1109 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
1110 constexpr auto
1111 transform_error(_Fn&& __f) const &
1112 {
1113 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1114 using _Res = expected<_Tp, _Gr>;
1115
1116 if (has_value())
1117 return _Res(in_place, _M_val);
1118 else
1119 return _Res(__unexpect_inv{}, [&]() {
1120 return std::__invoke(std::forward<_Fn>(__f),
1121 _M_unex);
1122 });
1123 }
1124
1125 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
1126 constexpr auto
1127 transform_error(_Fn&& __f) &&
1128 {
1129 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1130 using _Res = expected<_Tp, _Gr>;
1131
1132 if (has_value())
1133 return _Res(in_place, std::move(_M_val));
1134 else
1135 return _Res(__unexpect_inv{}, [&]() {
1136 return std::__invoke(std::forward<_Fn>(__f),
1137 std::move(_M_unex));
1138 });
1139 }
1140
1141 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1142 constexpr auto
1143 transform_error(_Fn&& __f) const &&
1144 {
1145 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1146 using _Res = expected<_Tp, _Gr>;
1147
1148 if (has_value())
1149 return _Res(in_place, std::move(_M_val));
1150 else
1151 return _Res(__unexpect_inv{}, [&]() {
1152 return std::__invoke(std::forward<_Fn>(__f),
1153 std::move(_M_unex));
1154 });
1155 }
1156
1157 // equality operators
1158
1159 template<typename _Up, typename _Er2>
1160 requires (!is_void_v<_Up>)
1161 && requires (const _Tp& __t, const _Up& __u,
1162 const _Er& __e, const _Er2& __e2) {
1163 { __t == __u } -> convertible_to<bool>;
1164 { __e == __e2 } -> convertible_to<bool>;
1165 }
1166 friend constexpr bool
1167 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1168 noexcept(noexcept(bool(*__x == *__y))
1169 && noexcept(bool(__x.error() == __y.error())))
1170 {
1171 if (__x.has_value())
1172 return __y.has_value() && bool(*__x == *__y);
1173 else
1174 return !__y.has_value() && bool(__x.error() == __y.error());
1175 }
1176
1177 template<typename _Up, same_as<_Tp> _Vp>
1178 requires (!__expected::__is_expected<_Up>)
1179 && requires (const _Tp& __t, const _Up& __u) {
1180 { __t == __u } -> convertible_to<bool>;
1181 }
1182 friend constexpr bool
1183 operator==(const expected<_Vp, _Er>& __x, const _Up& __v)
1184 noexcept(noexcept(bool(*__x == __v)))
1185 { return __x.has_value() && bool(*__x == __v); }
1186
1187 template<typename _Er2>
1188 requires requires (const _Er& __e, const _Er2& __e2) {
1189 { __e == __e2 } -> convertible_to<bool>;
1190 }
1191 friend constexpr bool
1192 operator==(const expected& __x, const unexpected<_Er2>& __e)
1193 noexcept(noexcept(bool(__x.error() == __e.error())))
1194 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1195
1196 friend constexpr void
1197 swap(expected& __x, expected& __y)
1198 noexcept(noexcept(__x.swap(__y)))
1199 requires requires {__x.swap(__y);}
1200 { __x.swap(__y); }
1201
1202 private:
1203 template<typename, typename> friend class expected;
1204
1205 template<typename _Vp>
1206 constexpr void
1207 _M_assign_val(_Vp&& __v)
1208 {
1209 if (_M_has_value)
1210 _M_val = std::forward<_Vp>(__v);
1211 else
1212 {
1213 __expected::__reinit(__builtin_addressof(_M_val),
1214 __builtin_addressof(_M_unex),
1215 std::forward<_Vp>(__v));
1216 _M_has_value = true;
1217 }
1218 }
1219
1220 template<typename _Vp>
1221 constexpr void
1222 _M_assign_unex(_Vp&& __v)
1223 {
1224 if (_M_has_value)
1225 {
1226 __expected::__reinit(__builtin_addressof(_M_unex),
1227 __builtin_addressof(_M_val),
1228 std::forward<_Vp>(__v));
1229 _M_has_value = false;
1230 }
1231 else
1232 _M_unex = std::forward<_Vp>(__v);
1233 }
1234
1235 // Swap two expected objects when only one has a value.
1236 // Precondition: this->_M_has_value && !__rhs._M_has_value
1237 constexpr void
1238 _M_swap_val_unex(expected& __rhs)
1239 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1240 is_nothrow_move_constructible<_Tp>>)
1241 {
1242 if constexpr (is_nothrow_move_constructible_v<_Er>)
1243 {
1244 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1245 std::construct_at(__builtin_addressof(__rhs._M_val),
1246 std::move(_M_val)); // might throw
1247 __rhs._M_has_value = true;
1248 std::destroy_at(__builtin_addressof(_M_val));
1249 std::construct_at(__builtin_addressof(_M_unex),
1250 __guard.release());
1251 _M_has_value = false;
1252 }
1253 else
1254 {
1255 __expected::_Guard<_Tp> __guard(_M_val);
1256 std::construct_at(__builtin_addressof(_M_unex),
1257 std::move(__rhs._M_unex)); // might throw
1258 _M_has_value = false;
1259 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1260 std::construct_at(__builtin_addressof(__rhs._M_val),
1261 __guard.release());
1262 __rhs._M_has_value = true;
1263 }
1264 }
1265
1266 using __in_place_inv = __expected::__in_place_inv;
1267 using __unexpect_inv = __expected::__unexpect_inv;
1268
1269 template<typename _Fn>
1270 explicit constexpr
1271 expected(__in_place_inv, _Fn&& __fn)
1272 : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
1273 { }
1274
1275 template<typename _Fn>
1276 explicit constexpr
1277 expected(__unexpect_inv, _Fn&& __fn)
1278 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1279 { }
1280
1281 union {
1282 remove_cv_t<_Tp> _M_val;
1283 _Er _M_unex;
1284 };
1285
1286 bool _M_has_value;
1287 };
1288
1289 // Partial specialization for std::expected<cv void, E>
1290 template<typename _Tp, typename _Er> requires is_void_v<_Tp>
1291 class expected<_Tp, _Er>
1292 {
1293 static_assert( __expected::__can_be_unexpected<_Er> );
1294
1295 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
1296 static constexpr bool __cons_from_expected
1297 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1298 is_constructible<_Unex, expected<_Up, _Err>>,
1299 is_constructible<_Unex, const expected<_Up, _Err>&>,
1300 is_constructible<_Unex, const expected<_Up, _Err>>
1301 >;
1302
1303 template<typename _Up>
1304 static constexpr bool __same_val
1305 = is_same_v<typename _Up::value_type, _Tp>;
1306
1307 template<typename _Up>
1308 static constexpr bool __same_err
1309 = is_same_v<typename _Up::error_type, _Er>;
1310
1311 public:
1312 using value_type = _Tp;
1313 using error_type = _Er;
1314 using unexpected_type = unexpected<_Er>;
1315
1316 template<typename _Up>
1317 using rebind = expected<_Up, error_type>;
1318
1319 constexpr
1320 expected() noexcept
1321 : _M_void(), _M_has_value(true)
1322 { }
1323
1324 expected(const expected&) = default;
1325
1326 constexpr
1327 expected(const expected& __x)
1328 noexcept(is_nothrow_copy_constructible_v<_Er>)
1329 requires is_copy_constructible_v<_Er>
1330 && (!is_trivially_copy_constructible_v<_Er>)
1331 : _M_void(), _M_has_value(__x._M_has_value)
1332 {
1333 if (!_M_has_value)
1334 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1335 }
1336
1337 expected(expected&&) = default;
1338
1339 constexpr
1340 expected(expected&& __x)
1341 noexcept(is_nothrow_move_constructible_v<_Er>)
1342 requires is_move_constructible_v<_Er>
1343 && (!is_trivially_move_constructible_v<_Er>)
1344 : _M_void(), _M_has_value(__x._M_has_value)
1345 {
1346 if (!_M_has_value)
1347 std::construct_at(__builtin_addressof(_M_unex),
1348 std::move(__x)._M_unex);
1349 }
1350
1351 template<typename _Up, typename _Gr>
1352 requires is_void_v<_Up>
1353 && is_constructible_v<_Er, const _Gr&>
1354 && (!__cons_from_expected<_Up, _Gr>)
1355 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1356 expected(const expected<_Up, _Gr>& __x)
1357 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1358 : _M_void(), _M_has_value(__x._M_has_value)
1359 {
1360 if (!_M_has_value)
1361 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1362 }
1363
1364 template<typename _Up, typename _Gr>
1365 requires is_void_v<_Up>
1366 && is_constructible_v<_Er, _Gr>
1367 && (!__cons_from_expected<_Up, _Gr>)
1368 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1369 expected(expected<_Up, _Gr>&& __x)
1370 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1371 : _M_void(), _M_has_value(__x._M_has_value)
1372 {
1373 if (!_M_has_value)
1374 std::construct_at(__builtin_addressof(_M_unex),
1375 std::move(__x)._M_unex);
1376 }
1377
1378 template<typename _Gr = _Er>
1379 requires is_constructible_v<_Er, const _Gr&>
1380 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1381 expected(const unexpected<_Gr>& __u)
1382 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1383 : _M_unex(__u.error()), _M_has_value(false)
1384 { }
1385
1386 template<typename _Gr = _Er>
1387 requires is_constructible_v<_Er, _Gr>
1388 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1389 expected(unexpected<_Gr>&& __u)
1390 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1391 : _M_unex(std::move(__u).error()), _M_has_value(false)
1392 { }
1393
1394 constexpr explicit
1395 expected(in_place_t) noexcept
1396 : expected()
1397 { }
1398
1399 template<typename... _Args>
1400 requires is_constructible_v<_Er, _Args...>
1401 constexpr explicit
1402 expected(unexpect_t, _Args&&... __args)
1403 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1404 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
1405 { }
1406
1407 template<typename _Up, typename... _Args>
1408 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1409 constexpr explicit
1410 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1411 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1412 _Args...>)
1413 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
1414 { }
1415
1416 constexpr ~expected() = default;
1417
1418 constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
1419 {
1420 if (!_M_has_value)
1421 std::destroy_at(__builtin_addressof(_M_unex));
1422 }
1423
1424 // assignment
1425
1426 expected& operator=(const expected&) = delete;
1427
1428 constexpr expected&
1429 operator=(const expected& __x)
1430 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1431 is_nothrow_copy_assignable<_Er>>)
1432 requires is_copy_constructible_v<_Er>
1433 && is_copy_assignable_v<_Er>
1434 {
1435 if (__x._M_has_value)
1436 emplace();
1437 else
1438 _M_assign_unex(__x._M_unex);
1439 return *this;
1440 }
1441
1442 constexpr expected&
1443 operator=(expected&& __x)
1444 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1445 is_nothrow_move_assignable<_Er>>)
1446 requires is_move_constructible_v<_Er>
1447 && is_move_assignable_v<_Er>
1448 {
1449 if (__x._M_has_value)
1450 emplace();
1451 else
1452 _M_assign_unex(std::move(__x._M_unex));
1453 return *this;
1454 }
1455
1456 template<typename _Gr>
1457 requires is_constructible_v<_Er, const _Gr&>
1458 && is_assignable_v<_Er&, const _Gr&>
1459 constexpr expected&
1460 operator=(const unexpected<_Gr>& __e)
1461 {
1462 _M_assign_unex(__e.error());
1463 return *this;
1464 }
1465
1466 template<typename _Gr>
1467 requires is_constructible_v<_Er, _Gr>
1468 && is_assignable_v<_Er&, _Gr>
1469 constexpr expected&
1470 operator=(unexpected<_Gr>&& __e)
1471 {
1472 _M_assign_unex(std::move(__e.error()));
1473 return *this;
1474 }
1475
1476 // modifiers
1477
1478 constexpr void
1479 emplace() noexcept
1480 {
1481 if (!_M_has_value)
1482 {
1483 std::destroy_at(__builtin_addressof(_M_unex));
1484 _M_has_value = true;
1485 }
1486 }
1487
1488 // swap
1489 constexpr void
1490 swap(expected& __x)
1491 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1492 is_nothrow_move_constructible<_Er>>)
1493 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1494 {
1495 if (_M_has_value)
1496 {
1497 if (!__x._M_has_value)
1498 {
1499 std::construct_at(__builtin_addressof(_M_unex),
1500 std::move(__x._M_unex)); // might throw
1501 std::destroy_at(__builtin_addressof(__x._M_unex));
1502 _M_has_value = false;
1503 __x._M_has_value = true;
1504 }
1505 }
1506 else
1507 {
1508 if (__x._M_has_value)
1509 {
1510 std::construct_at(__builtin_addressof(__x._M_unex),
1511 std::move(_M_unex)); // might throw
1512 std::destroy_at(__builtin_addressof(_M_unex));
1513 _M_has_value = true;
1514 __x._M_has_value = false;
1515 }
1516 else
1517 {
1518 using std::swap;
1519 swap(_M_unex, __x._M_unex);
1520 }
1521 }
1522 }
1523
1524 // observers
1525
1526 [[nodiscard]]
1527 constexpr explicit
1528 operator bool() const noexcept { return _M_has_value; }
1529
1530 [[nodiscard]]
1531 constexpr bool has_value() const noexcept { return _M_has_value; }
1532
1533 constexpr void
1534 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1535
1536 constexpr void
1537 value() const&
1538 {
1539 static_assert( is_copy_constructible_v<_Er> );
1540 if (_M_has_value) [[likely]]
1541 return;
1542 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1543 }
1544
1545 constexpr void
1546 value() &&
1547 {
1548 static_assert( is_copy_constructible_v<_Er> );
1549 static_assert( is_move_constructible_v<_Er> );
1550 if (_M_has_value) [[likely]]
1551 return;
1552 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
1553 }
1554
1555 constexpr const _Er&
1556 error() const & noexcept
1557 {
1558 __glibcxx_assert(!_M_has_value);
1559 return _M_unex;
1560 }
1561
1562 constexpr _Er&
1563 error() & noexcept
1564 {
1565 __glibcxx_assert(!_M_has_value);
1566 return _M_unex;
1567 }
1568
1569 constexpr const _Er&&
1570 error() const && noexcept
1571 {
1572 __glibcxx_assert(!_M_has_value);
1573 return std::move(_M_unex);
1574 }
1575
1576 constexpr _Er&&
1577 error() && noexcept
1578 {
1579 __glibcxx_assert(!_M_has_value);
1580 return std::move(_M_unex);
1581 }
1582
1583 template<typename _Gr = _Er>
1584 constexpr _Er
1585 error_or(_Gr&& __e) const&
1586 {
1587 static_assert( is_copy_constructible_v<_Er> );
1588 static_assert( is_convertible_v<_Gr, _Er> );
1589
1590 if (_M_has_value)
1591 return std::forward<_Gr>(__e);
1592 return _M_unex;
1593 }
1594
1595 template<typename _Gr = _Er>
1596 constexpr _Er
1597 error_or(_Gr&& __e) &&
1598 {
1599 static_assert( is_move_constructible_v<_Er> );
1600 static_assert( is_convertible_v<_Gr, _Er> );
1601
1602 if (_M_has_value)
1603 return std::forward<_Gr>(__e);
1604 return std::move(_M_unex);
1605 }
1606
1607 // monadic operations
1608
1609 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1610 constexpr auto
1611 and_then(_Fn&& __f) &
1612 {
1613 using _Up = __expected::__result0<_Fn>;
1614 static_assert(__expected::__is_expected<_Up>);
1615 static_assert(is_same_v<typename _Up::error_type, _Er>);
1616
1617 if (has_value())
1618 return std::__invoke(std::forward<_Fn>(__f));
1619 else
1620 return _Up(unexpect, _M_unex);
1621 }
1622
1623 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1624 constexpr auto
1625 and_then(_Fn&& __f) const &
1626 {
1627 using _Up = __expected::__result0<_Fn>;
1628 static_assert(__expected::__is_expected<_Up>);
1629 static_assert(is_same_v<typename _Up::error_type, _Er>);
1630
1631 if (has_value())
1632 return std::__invoke(std::forward<_Fn>(__f));
1633 else
1634 return _Up(unexpect, _M_unex);
1635 }
1636
1637 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1638 constexpr auto
1639 and_then(_Fn&& __f) &&
1640 {
1641 using _Up = __expected::__result0<_Fn>;
1642 static_assert(__expected::__is_expected<_Up>);
1643 static_assert(is_same_v<typename _Up::error_type, _Er>);
1644
1645 if (has_value())
1646 return std::__invoke(std::forward<_Fn>(__f));
1647 else
1648 return _Up(unexpect, std::move(_M_unex));
1649 }
1650
1651 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1652 constexpr auto
1653 and_then(_Fn&& __f) const &&
1654 {
1655 using _Up = __expected::__result0<_Fn>;
1656 static_assert(__expected::__is_expected<_Up>);
1657 static_assert(is_same_v<typename _Up::error_type, _Er>);
1658
1659 if (has_value())
1660 return std::__invoke(std::forward<_Fn>(__f));
1661 else
1662 return _Up(unexpect, std::move(_M_unex));
1663 }
1664
1665 template<typename _Fn>
1666 constexpr auto
1667 or_else(_Fn&& __f) &
1668 {
1669 using _Gr = __expected::__result<_Fn, _Er&>;
1670 static_assert(__expected::__is_expected<_Gr>);
1671 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1672
1673 if (has_value())
1674 return _Gr();
1675 else
1676 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1677 }
1678
1679 template<typename _Fn>
1680 constexpr auto
1681 or_else(_Fn&& __f) const &
1682 {
1683 using _Gr = __expected::__result<_Fn, const _Er&>;
1684 static_assert(__expected::__is_expected<_Gr>);
1685 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1686
1687 if (has_value())
1688 return _Gr();
1689 else
1690 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1691 }
1692
1693 template<typename _Fn>
1694 constexpr auto
1695 or_else(_Fn&& __f) &&
1696 {
1697 using _Gr = __expected::__result<_Fn, _Er&&>;
1698 static_assert(__expected::__is_expected<_Gr>);
1699 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1700
1701 if (has_value())
1702 return _Gr();
1703 else
1704 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1705 }
1706
1707 template<typename _Fn>
1708 constexpr auto
1709 or_else(_Fn&& __f) const &&
1710 {
1711 using _Gr = __expected::__result<_Fn, const _Er&&>;
1712 static_assert(__expected::__is_expected<_Gr>);
1713 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1714
1715 if (has_value())
1716 return _Gr();
1717 else
1718 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1719 }
1720
1721 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1722 constexpr auto
1723 transform(_Fn&& __f) &
1724 {
1725 using _Up = __expected::__result0_xform<_Fn>;
1726 using _Res = expected<_Up, _Er>;
1727
1728 if (has_value())
1729 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1730 else
1731 return _Res(unexpect, _M_unex);
1732 }
1733
1734 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1735 constexpr auto
1736 transform(_Fn&& __f) const &
1737 {
1738 using _Up = __expected::__result0_xform<_Fn>;
1739 using _Res = expected<_Up, _Er>;
1740
1741 if (has_value())
1742 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1743 else
1744 return _Res(unexpect, _M_unex);
1745 }
1746
1747 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1748 constexpr auto
1749 transform(_Fn&& __f) &&
1750 {
1751 using _Up = __expected::__result0_xform<_Fn>;
1752 using _Res = expected<_Up, _Er>;
1753
1754 if (has_value())
1755 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1756 else
1757 return _Res(unexpect, std::move(_M_unex));
1758 }
1759
1760 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1761 constexpr auto
1762 transform(_Fn&& __f) const &&
1763 {
1764 using _Up = __expected::__result0_xform<_Fn>;
1765 using _Res = expected<_Up, _Er>;
1766
1767 if (has_value())
1768 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1769 else
1770 return _Res(unexpect, std::move(_M_unex));
1771 }
1772
1773 template<typename _Fn>
1774 constexpr auto
1775 transform_error(_Fn&& __f) &
1776 {
1777 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1778 using _Res = expected<_Tp, _Gr>;
1779
1780 if (has_value())
1781 return _Res();
1782 else
1783 return _Res(__unexpect_inv{}, [&]() {
1784 return std::__invoke(std::forward<_Fn>(__f),
1785 _M_unex);
1786 });
1787 }
1788
1789 template<typename _Fn>
1790 constexpr auto
1791 transform_error(_Fn&& __f) const &
1792 {
1793 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1794 using _Res = expected<_Tp, _Gr>;
1795
1796 if (has_value())
1797 return _Res();
1798 else
1799 return _Res(__unexpect_inv{}, [&]() {
1800 return std::__invoke(std::forward<_Fn>(__f),
1801 _M_unex);
1802 });
1803 }
1804
1805 template<typename _Fn>
1806 constexpr auto
1807 transform_error(_Fn&& __f) &&
1808 {
1809 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1810 using _Res = expected<_Tp, _Gr>;
1811
1812 if (has_value())
1813 return _Res();
1814 else
1815 return _Res(__unexpect_inv{}, [&]() {
1816 return std::__invoke(std::forward<_Fn>(__f),
1817 std::move(_M_unex));
1818 });
1819 }
1820
1821 template<typename _Fn>
1822 constexpr auto
1823 transform_error(_Fn&& __f) const &&
1824 {
1825 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1826 using _Res = expected<_Tp, _Gr>;
1827
1828 if (has_value())
1829 return _Res();
1830 else
1831 return _Res(__unexpect_inv{}, [&]() {
1832 return std::__invoke(std::forward<_Fn>(__f),
1833 std::move(_M_unex));
1834 });
1835 }
1836
1837 // equality operators
1838
1839 template<typename _Up, typename _Er2>
1840 requires is_void_v<_Up>
1841 && requires (const _Er& __e, const _Er2& __e2) {
1842 { __e == __e2 } -> convertible_to<bool>;
1843 }
1844 friend constexpr bool
1845 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1846 noexcept(noexcept(bool(__x.error() == __y.error())))
1847 {
1848 if (__x.has_value())
1849 return __y.has_value();
1850 else
1851 return !__y.has_value() && bool(__x.error() == __y.error());
1852 }
1853
1854 template<typename _Er2>
1855 requires requires (const _Er& __e, const _Er2& __e2) {
1856 { __e == __e2 } -> convertible_to<bool>;
1857 }
1858 friend constexpr bool
1859 operator==(const expected& __x, const unexpected<_Er2>& __e)
1860 noexcept(noexcept(bool(__x.error() == __e.error())))
1861 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1862
1863 friend constexpr void
1864 swap(expected& __x, expected& __y)
1865 noexcept(noexcept(__x.swap(__y)))
1866 requires requires { __x.swap(__y); }
1867 { __x.swap(__y); }
1868
1869 private:
1870 template<typename, typename> friend class expected;
1871
1872 template<typename _Vp>
1873 constexpr void
1874 _M_assign_unex(_Vp&& __v)
1875 {
1876 if (_M_has_value)
1877 {
1878 std::construct_at(__builtin_addressof(_M_unex),
1879 std::forward<_Vp>(__v));
1880 _M_has_value = false;
1881 }
1882 else
1883 _M_unex = std::forward<_Vp>(__v);
1884 }
1885
1886 using __in_place_inv = __expected::__in_place_inv;
1887 using __unexpect_inv = __expected::__unexpect_inv;
1888
1889 template<typename _Fn>
1890 explicit constexpr
1891 expected(__in_place_inv, _Fn&& __fn)
1892 : _M_void(), _M_has_value(true)
1893 { std::forward<_Fn>(__fn)(); }
1894
1895 template<typename _Fn>
1896 explicit constexpr
1897 expected(__unexpect_inv, _Fn&& __fn)
1898 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1899 { }
1900
1901 union {
1902 struct { } _M_void;
1903 _Er _M_unex;
1904 };
1905
1906 bool _M_has_value;
1907 };
1908 /// @}
1909
1910_GLIBCXX_END_NAMESPACE_VERSION
1911} // namespace std
1912
1913#endif // __cpp_lib_expected
1914#endif // _GLIBCXX_EXPECTED
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
void unexpected()
ISO C++ entities toplevel namespace is std.
Base class for all library exceptions.
Definition exception.h:62