1// <expected> -*- C++ -*-
3// Copyright The GNU Toolchain Authors.
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)
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.
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.
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/>.
25/** @file include/expected
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
32#pragma GCC system_header
34#define __glibcxx_want_expected
35#define __glibcxx_want_freestanding_expected
36#include <bits/version.h>
38#ifdef __cpp_lib_expected // C++ >= 23 && __cpp_concepts >= 202002L
39#include <initializer_list>
40#include <bits/exception.h> // exception
41#include <bits/invoke.h> // __invoke
42#include <bits/stl_construct.h> // construct_at
43#include <bits/utility.h> // in_place_t
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @defgroup expected_values Expected values
51 * @addtogroup utilities
56 /// Discriminated union that holds an expected value or an error value.
60 template<typename _Tp, typename _Er>
63 /// Wrapper type used to pass an error value to a `std::expected`.
67 template<typename _Er>
70 /// Exception thrown by std::expected when the value() is not present.
74 template<typename _Er>
75 class bad_expected_access;
78 class bad_expected_access<void> : public exception
81 bad_expected_access() noexcept { }
82 bad_expected_access(const bad_expected_access&) = default;
83 bad_expected_access(bad_expected_access&&) = default;
84 bad_expected_access& operator=(const bad_expected_access&) = default;
85 bad_expected_access& operator=(bad_expected_access&&) = default;
86 ~bad_expected_access() = default;
92 what() const noexcept override
93 { return "bad access to std::expected without expected value"; }
96 template<typename _Er>
97 class bad_expected_access : public bad_expected_access<void> {
100 bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
102 // XXX const char* what() const noexcept override;
111 error() const & noexcept
117 { return std::move(_M_unex); }
121 error() const && noexcept
122 { return std::move(_M_unex); }
128 /// Tag type for constructing unexpected values in a std::expected
134 explicit unexpect_t() = default;
137 /// Tag for constructing unexpected values in a std::expected
141 inline constexpr unexpect_t unexpect{};
143/// @cond undocumented
146 template<typename _Tp>
147 constexpr bool __is_expected = false;
148 template<typename _Tp, typename _Er>
149 constexpr bool __is_expected<expected<_Tp, _Er>> = true;
151 template<typename _Tp>
152 constexpr bool __is_unexpected = false;
153 template<typename _Tp>
154 constexpr bool __is_unexpected<unexpected<_Tp>> = true;
156 template<typename _Fn, typename _Tp>
157 using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
158 template<typename _Fn, typename _Tp>
159 using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
160 template<typename _Fn>
161 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
162 template<typename _Fn>
163 using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
165 template<typename _Er>
166 concept __can_be_unexpected
167 = is_object_v<_Er> && (!is_array_v<_Er>)
168 && (!__expected::__is_unexpected<_Er>)
169 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
171 // Tag types for in-place construction from an invocation result.
172 struct __in_place_inv { };
173 struct __unexpect_inv { };
177 template<typename _Er>
180 static_assert( __expected::__can_be_unexpected<_Er> );
183 constexpr unexpected(const unexpected&) = default;
184 constexpr unexpected(unexpected&&) = default;
186 template<typename _Err = _Er>
187 requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
188 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
189 && is_constructible_v<_Er, _Err>
191 unexpected(_Err&& __e)
192 noexcept(is_nothrow_constructible_v<_Er, _Err>)
193 : _M_unex(std::forward<_Err>(__e))
196 template<typename... _Args>
197 requires is_constructible_v<_Er, _Args...>
199 unexpected(in_place_t, _Args&&... __args)
200 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
201 : _M_unex(std::forward<_Args>(__args)...)
204 template<typename _Up, typename... _Args>
205 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
207 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
208 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
210 : _M_unex(__il, std::forward<_Args>(__args)...)
213 constexpr unexpected& operator=(const unexpected&) = default;
214 constexpr unexpected& operator=(unexpected&&) = default;
219 error() const & noexcept { return _M_unex; }
223 error() & noexcept { return _M_unex; }
226 constexpr const _Er&&
227 error() const && noexcept { return std::move(_M_unex); }
231 error() && noexcept { return std::move(_M_unex); }
234 swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
235 requires is_swappable_v<_Er>
238 swap(_M_unex, __other._M_unex);
241 template<typename _Err>
243 friend constexpr bool
244 operator==(const unexpected& __x, const unexpected<_Err>& __y)
245 { return __x._M_unex == __y.error(); }
247 friend constexpr void
248 swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
249 requires is_swappable_v<_Er>
256 template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
258/// @cond undocumented
261 template<typename _Tp>
264 static_assert( is_nothrow_move_constructible_v<_Tp> );
268 : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
269 { std::destroy_at(_M_guarded); }
274 if (_M_guarded) [[unlikely]]
275 std::construct_at(_M_guarded, std::move(_M_tmp));
278 _Guard(const _Guard&) = delete;
279 _Guard& operator=(const _Guard&) = delete;
284 _M_guarded = nullptr;
285 return std::move(_M_tmp);
293 // reinit-expected helper from [expected.object.assign]
294 template<typename _Tp, typename _Up, typename _Vp>
296 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
297 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
299 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
301 std::destroy_at(__oldval);
302 std::construct_at(__newval, std::forward<_Vp>(__arg));
304 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
306 _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
307 std::destroy_at(__oldval);
308 std::construct_at(__newval, std::move(__tmp));
312 _Guard<_Up> __guard(*__oldval);
313 std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
318 // _GLIBCXX_RESOLVE_LIB_DEFECTS
319 // 3836. std::expected<bool, E1> conversion constructor
320 // expected(const expected<U, G>&) should take precedence over
321 // expected(U&&) with operator bool
323 // If T is cv bool, remove_cvref_t<U> is not a specialization of expected.
324 template<typename _Tp, typename _Up>
325 concept __not_constructing_bool_from_expected
326 = ! is_same_v<remove_cv_t<_Tp>, bool>
327 || ! __is_expected<remove_cvref_t<_Up>>;
331 template<typename _Tp, typename _Er>
334 static_assert( ! is_reference_v<_Tp> );
335 static_assert( ! is_function_v<_Tp> );
336 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
337 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
338 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
339 static_assert( __expected::__can_be_unexpected<_Er> );
341 // If T is not cv bool, converts-from-any-cvref<T, expected<U, G>> and
342 // is_constructible<unexpected<E>, cv expected<U, G> ref-qual> are false.
343 template<typename _Up, typename _Gr, typename _Unex = unexpected<_Er>,
344 typename = remove_cv_t<_Tp>>
345 static constexpr bool __cons_from_expected
346 = __or_v<is_constructible<_Tp, expected<_Up, _Gr>&>,
347 is_constructible<_Tp, expected<_Up, _Gr>>,
348 is_constructible<_Tp, const expected<_Up, _Gr>&>,
349 is_constructible<_Tp, const expected<_Up, _Gr>>,
350 is_convertible<expected<_Up, _Gr>&, _Tp>,
351 is_convertible<expected<_Up, _Gr>, _Tp>,
352 is_convertible<const expected<_Up, _Gr>&, _Tp>,
353 is_convertible<const expected<_Up, _Gr>, _Tp>,
354 is_constructible<_Unex, expected<_Up, _Gr>&>,
355 is_constructible<_Unex, expected<_Up, _Gr>>,
356 is_constructible<_Unex, const expected<_Up, _Gr>&>,
357 is_constructible<_Unex, const expected<_Up, _Gr>>
360 // _GLIBCXX_RESOLVE_LIB_DEFECTS
361 // If t is cv bool, we know it can be constructed from expected<U, G>,
362 // but we don't want to cause the expected(U&&) constructor to be used,
363 // so we only check the is_constructible<unexpected<E>, ...> cases.
364 template<typename _Up, typename _Gr, typename _Unex>
365 static constexpr bool __cons_from_expected<_Up, _Gr, _Unex, bool>
366 = __or_v<is_constructible<_Unex, expected<_Up, _Gr>&>,
367 is_constructible<_Unex, expected<_Up, _Gr>>,
368 is_constructible<_Unex, const expected<_Up, _Gr>&>,
369 is_constructible<_Unex, const expected<_Up, _Gr>>
372 template<typename _Up, typename _Gr>
373 constexpr static bool __explicit_conv
374 = __or_v<__not_<is_convertible<_Up, _Tp>>,
375 __not_<is_convertible<_Gr, _Er>>
378 template<typename _Up>
379 static constexpr bool __same_val
380 = is_same_v<typename _Up::value_type, _Tp>;
382 template<typename _Up>
383 static constexpr bool __same_err
384 = is_same_v<typename _Up::error_type, _Er>;
387 using value_type = _Tp;
388 using error_type = _Er;
389 using unexpected_type = unexpected<_Er>;
391 template<typename _Up>
392 using rebind = expected<_Up, error_type>;
396 noexcept(is_nothrow_default_constructible_v<_Tp>)
397 requires is_default_constructible_v<_Tp>
398 : _M_val(), _M_has_value(true)
401 expected(const expected&) = default;
404 expected(const expected& __x)
405 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
406 is_nothrow_copy_constructible<_Er>>)
407 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
408 && (!is_trivially_copy_constructible_v<_Tp>
409 || !is_trivially_copy_constructible_v<_Er>)
410 : _M_has_value(__x._M_has_value)
413 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
415 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
418 expected(expected&&) = default;
421 expected(expected&& __x)
422 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
423 is_nothrow_move_constructible<_Er>>)
424 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
425 && (!is_trivially_move_constructible_v<_Tp>
426 || !is_trivially_move_constructible_v<_Er>)
427 : _M_has_value(__x._M_has_value)
430 std::construct_at(__builtin_addressof(_M_val),
431 std::move(__x)._M_val);
433 std::construct_at(__builtin_addressof(_M_unex),
434 std::move(__x)._M_unex);
437 template<typename _Up, typename _Gr>
438 requires is_constructible_v<_Tp, const _Up&>
439 && is_constructible_v<_Er, const _Gr&>
440 && (!__cons_from_expected<_Up, _Gr>)
441 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
442 expected(const expected<_Up, _Gr>& __x)
443 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
444 is_nothrow_constructible<_Er, const _Gr&>>)
445 : _M_has_value(__x._M_has_value)
448 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
450 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
453 template<typename _Up, typename _Gr>
454 requires is_constructible_v<_Tp, _Up>
455 && is_constructible_v<_Er, _Gr>
456 && (!__cons_from_expected<_Up, _Gr>)
457 constexpr explicit(__explicit_conv<_Up, _Gr>)
458 expected(expected<_Up, _Gr>&& __x)
459 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
460 is_nothrow_constructible<_Er, _Gr>>)
461 : _M_has_value(__x._M_has_value)
464 std::construct_at(__builtin_addressof(_M_val),
465 std::move(__x)._M_val);
467 std::construct_at(__builtin_addressof(_M_unex),
468 std::move(__x)._M_unex);
471 template<typename _Up = _Tp>
472 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
473 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
474 && (!is_same_v<remove_cvref_t<_Up>, unexpect_t>)
475 && is_constructible_v<_Tp, _Up>
476 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
477 && __expected::__not_constructing_bool_from_expected<_Tp, _Up>
478 constexpr explicit(!is_convertible_v<_Up, _Tp>)
480 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
481 : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
484 template<typename _Gr = _Er>
485 requires is_constructible_v<_Er, const _Gr&>
486 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
487 expected(const unexpected<_Gr>& __u)
488 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
489 : _M_unex(__u.error()), _M_has_value(false)
492 template<typename _Gr = _Er>
493 requires is_constructible_v<_Er, _Gr>
494 constexpr explicit(!is_convertible_v<_Gr, _Er>)
495 expected(unexpected<_Gr>&& __u)
496 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
497 : _M_unex(std::move(__u).error()), _M_has_value(false)
500 template<typename... _Args>
501 requires is_constructible_v<_Tp, _Args...>
503 expected(in_place_t, _Args&&... __args)
504 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
505 : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
508 template<typename _Up, typename... _Args>
509 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
511 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
512 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
514 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
517 template<typename... _Args>
518 requires is_constructible_v<_Er, _Args...>
520 expected(unexpect_t, _Args&&... __args)
521 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
522 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
525 template<typename _Up, typename... _Args>
526 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
528 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
529 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
531 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
534 constexpr ~expected() = default;
536 constexpr ~expected()
537 requires (!is_trivially_destructible_v<_Tp>)
538 || (!is_trivially_destructible_v<_Er>)
541 std::destroy_at(__builtin_addressof(_M_val));
543 std::destroy_at(__builtin_addressof(_M_unex));
548 expected& operator=(const expected&) = delete;
551 operator=(const expected& __x)
552 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
553 is_nothrow_copy_constructible<_Er>,
554 is_nothrow_copy_assignable<_Tp>,
555 is_nothrow_copy_assignable<_Er>>)
556 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
557 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
558 && (is_nothrow_move_constructible_v<_Tp>
559 || is_nothrow_move_constructible_v<_Er>)
561 if (__x._M_has_value)
562 this->_M_assign_val(__x._M_val);
564 this->_M_assign_unex(__x._M_unex);
569 operator=(expected&& __x)
570 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
571 is_nothrow_move_constructible<_Er>,
572 is_nothrow_move_assignable<_Tp>,
573 is_nothrow_move_assignable<_Er>>)
574 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
575 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
576 && (is_nothrow_move_constructible_v<_Tp>
577 || is_nothrow_move_constructible_v<_Er>)
579 if (__x._M_has_value)
580 _M_assign_val(std::move(__x._M_val));
582 _M_assign_unex(std::move(__x._M_unex));
586 template<typename _Up = _Tp>
587 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
588 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
589 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
590 && (is_nothrow_constructible_v<_Tp, _Up>
591 || is_nothrow_move_constructible_v<_Tp>
592 || is_nothrow_move_constructible_v<_Er>)
596 _M_assign_val(std::forward<_Up>(__v));
600 template<typename _Gr>
601 requires is_constructible_v<_Er, const _Gr&>
602 && is_assignable_v<_Er&, const _Gr&>
603 && (is_nothrow_constructible_v<_Er, const _Gr&>
604 || is_nothrow_move_constructible_v<_Tp>
605 || is_nothrow_move_constructible_v<_Er>)
607 operator=(const unexpected<_Gr>& __e)
609 _M_assign_unex(__e.error());
613 template<typename _Gr>
614 requires is_constructible_v<_Er, _Gr>
615 && is_assignable_v<_Er&, _Gr>
616 && (is_nothrow_constructible_v<_Er, _Gr>
617 || is_nothrow_move_constructible_v<_Tp>
618 || is_nothrow_move_constructible_v<_Er>)
620 operator=(unexpected<_Gr>&& __e)
622 _M_assign_unex(std::move(__e).error());
628 template<typename... _Args>
629 requires is_nothrow_constructible_v<_Tp, _Args...>
631 emplace(_Args&&... __args) noexcept
634 std::destroy_at(__builtin_addressof(_M_val));
637 std::destroy_at(__builtin_addressof(_M_unex));
640 std::construct_at(__builtin_addressof(_M_val),
641 std::forward<_Args>(__args)...);
645 template<typename _Up, typename... _Args>
646 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
649 emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
652 std::destroy_at(__builtin_addressof(_M_val));
655 std::destroy_at(__builtin_addressof(_M_unex));
658 std::construct_at(__builtin_addressof(_M_val),
659 __il, std::forward<_Args>(__args)...);
666 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
667 is_nothrow_move_constructible<_Er>,
668 is_nothrow_swappable<_Tp&>,
669 is_nothrow_swappable<_Er&>>)
670 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
671 && is_move_constructible_v<_Tp>
672 && is_move_constructible_v<_Er>
673 && (is_nothrow_move_constructible_v<_Tp>
674 || is_nothrow_move_constructible_v<_Er>)
678 if (__x._M_has_value)
681 swap(_M_val, __x._M_val);
684 this->_M_swap_val_unex(__x);
688 if (__x._M_has_value)
689 __x._M_swap_val_unex(*this);
693 swap(_M_unex, __x._M_unex);
702 operator->() const noexcept
704 __glibcxx_assert(_M_has_value);
705 return __builtin_addressof(_M_val);
710 operator->() noexcept
712 __glibcxx_assert(_M_has_value);
713 return __builtin_addressof(_M_val);
718 operator*() const & noexcept
720 __glibcxx_assert(_M_has_value);
726 operator*() & noexcept
728 __glibcxx_assert(_M_has_value);
733 constexpr const _Tp&&
734 operator*() const && noexcept
736 __glibcxx_assert(_M_has_value);
737 return std::move(_M_val);
742 operator*() && noexcept
744 __glibcxx_assert(_M_has_value);
745 return std::move(_M_val);
750 operator bool() const noexcept { return _M_has_value; }
753 constexpr bool has_value() const noexcept { return _M_has_value; }
758 if (_M_has_value) [[likely]]
760 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
766 if (_M_has_value) [[likely]]
768 const auto& __unex = _M_unex;
769 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
772 constexpr const _Tp&&
775 if (_M_has_value) [[likely]]
776 return std::move(_M_val);
777 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
783 if (_M_has_value) [[likely]]
784 return std::move(_M_val);
785 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
789 error() const & noexcept
791 __glibcxx_assert(!_M_has_value);
798 __glibcxx_assert(!_M_has_value);
802 constexpr const _Er&&
803 error() const && noexcept
805 __glibcxx_assert(!_M_has_value);
806 return std::move(_M_unex);
812 __glibcxx_assert(!_M_has_value);
813 return std::move(_M_unex);
816 template<typename _Up>
818 value_or(_Up&& __v) const &
819 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
820 is_nothrow_convertible<_Up, _Tp>>)
822 static_assert( is_copy_constructible_v<_Tp> );
823 static_assert( is_convertible_v<_Up, _Tp> );
827 return static_cast<_Tp>(std::forward<_Up>(__v));
830 template<typename _Up>
832 value_or(_Up&& __v) &&
833 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
834 is_nothrow_convertible<_Up, _Tp>>)
836 static_assert( is_move_constructible_v<_Tp> );
837 static_assert( is_convertible_v<_Up, _Tp> );
840 return std::move(_M_val);
841 return static_cast<_Tp>(std::forward<_Up>(__v));
844 template<typename _Gr = _Er>
846 error_or(_Gr&& __e) const&
848 static_assert( is_copy_constructible_v<_Er> );
849 static_assert( is_convertible_v<_Gr, _Er> );
852 return std::forward<_Gr>(__e);
856 template<typename _Gr = _Er>
858 error_or(_Gr&& __e) &&
860 static_assert( is_move_constructible_v<_Er> );
861 static_assert( is_convertible_v<_Gr, _Er> );
864 return std::forward<_Gr>(__e);
865 return std::move(_M_unex);
868 // monadic operations
870 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
872 and_then(_Fn&& __f) &
874 using _Up = __expected::__result<_Fn, _Tp&>;
875 static_assert(__expected::__is_expected<_Up>,
876 "the function passed to std::expected<T, E>::and_then "
877 "must return a std::expected");
878 static_assert(is_same_v<typename _Up::error_type, _Er>,
879 "the function passed to std::expected<T, E>::and_then "
880 "must return a std::expected with the same error_type");
883 return std::__invoke(std::forward<_Fn>(__f), _M_val);
885 return _Up(unexpect, _M_unex);
888 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
890 and_then(_Fn&& __f) const &
892 using _Up = __expected::__result<_Fn, const _Tp&>;
893 static_assert(__expected::__is_expected<_Up>,
894 "the function passed to std::expected<T, E>::and_then "
895 "must return a std::expected");
896 static_assert(is_same_v<typename _Up::error_type, _Er>,
897 "the function passed to std::expected<T, E>::and_then "
898 "must return a std::expected with the same error_type");
901 return std::__invoke(std::forward<_Fn>(__f), _M_val);
903 return _Up(unexpect, _M_unex);
906 template<typename _Fn> requires is_constructible_v<_Er, _Er>
908 and_then(_Fn&& __f) &&
910 using _Up = __expected::__result<_Fn, _Tp&&>;
911 static_assert(__expected::__is_expected<_Up>,
912 "the function passed to std::expected<T, E>::and_then "
913 "must return a std::expected");
914 static_assert(is_same_v<typename _Up::error_type, _Er>,
915 "the function passed to std::expected<T, E>::and_then "
916 "must return a std::expected with the same error_type");
919 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
921 return _Up(unexpect, std::move(_M_unex));
925 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
927 and_then(_Fn&& __f) const &&
929 using _Up = __expected::__result<_Fn, const _Tp&&>;
930 static_assert(__expected::__is_expected<_Up>,
931 "the function passed to std::expected<T, E>::and_then "
932 "must return a std::expected");
933 static_assert(is_same_v<typename _Up::error_type, _Er>,
934 "the function passed to std::expected<T, E>::and_then "
935 "must return a std::expected with the same error_type");
938 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
940 return _Up(unexpect, std::move(_M_unex));
943 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
947 using _Gr = __expected::__result<_Fn, _Er&>;
948 static_assert(__expected::__is_expected<_Gr>,
949 "the function passed to std::expected<T, E>::or_else "
950 "must return a std::expected");
951 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
952 "the function passed to std::expected<T, E>::or_else "
953 "must return a std::expected with the same value_type");
956 return _Gr(in_place, _M_val);
958 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
961 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
963 or_else(_Fn&& __f) const &
965 using _Gr = __expected::__result<_Fn, const _Er&>;
966 static_assert(__expected::__is_expected<_Gr>,
967 "the function passed to std::expected<T, E>::or_else "
968 "must return a std::expected");
969 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
970 "the function passed to std::expected<T, E>::or_else "
971 "must return a std::expected with the same value_type");
974 return _Gr(in_place, _M_val);
976 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
980 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
982 or_else(_Fn&& __f) &&
984 using _Gr = __expected::__result<_Fn, _Er&&>;
985 static_assert(__expected::__is_expected<_Gr>,
986 "the function passed to std::expected<T, E>::or_else "
987 "must return a std::expected");
988 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
989 "the function passed to std::expected<T, E>::or_else "
990 "must return a std::expected with the same value_type");
993 return _Gr(in_place, std::move(_M_val));
995 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
998 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1000 or_else(_Fn&& __f) const &&
1002 using _Gr = __expected::__result<_Fn, const _Er&&>;
1003 static_assert(__expected::__is_expected<_Gr>,
1004 "the function passed to std::expected<T, E>::or_else "
1005 "must return a std::expected");
1006 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1007 "the function passed to std::expected<T, E>::or_else "
1008 "must return a std::expected with the same value_type");
1011 return _Gr(in_place, std::move(_M_val));
1013 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1016 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1018 transform(_Fn&& __f) &
1020 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1021 using _Res = expected<_Up, _Er>;
1024 return _Res(__in_place_inv{}, [&]() {
1025 return std::__invoke(std::forward<_Fn>(__f),
1029 return _Res(unexpect, _M_unex);
1032 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1034 transform(_Fn&& __f) const &
1036 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1037 using _Res = expected<_Up, _Er>;
1040 return _Res(__in_place_inv{}, [&]() {
1041 return std::__invoke(std::forward<_Fn>(__f),
1045 return _Res(unexpect, _M_unex);
1048 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1050 transform(_Fn&& __f) &&
1052 using _Up = __expected::__result_xform<_Fn, _Tp>;
1053 using _Res = expected<_Up, _Er>;
1056 return _Res(__in_place_inv{}, [&]() {
1057 return std::__invoke(std::forward<_Fn>(__f),
1061 return _Res(unexpect, std::move(_M_unex));
1064 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1066 transform(_Fn&& __f) const &&
1068 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1069 using _Res = expected<_Up, _Er>;
1072 return _Res(__in_place_inv{}, [&]() {
1073 return std::__invoke(std::forward<_Fn>(__f),
1077 return _Res(unexpect, std::move(_M_unex));
1080 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
1082 transform_error(_Fn&& __f) &
1084 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1085 using _Res = expected<_Tp, _Gr>;
1088 return _Res(in_place, _M_val);
1090 return _Res(__unexpect_inv{}, [&]() {
1091 return std::__invoke(std::forward<_Fn>(__f),
1096 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
1098 transform_error(_Fn&& __f) const &
1100 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1101 using _Res = expected<_Tp, _Gr>;
1104 return _Res(in_place, _M_val);
1106 return _Res(__unexpect_inv{}, [&]() {
1107 return std::__invoke(std::forward<_Fn>(__f),
1112 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
1114 transform_error(_Fn&& __f) &&
1116 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1117 using _Res = expected<_Tp, _Gr>;
1120 return _Res(in_place, std::move(_M_val));
1122 return _Res(__unexpect_inv{}, [&]() {
1123 return std::__invoke(std::forward<_Fn>(__f),
1124 std::move(_M_unex));
1128 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1130 transform_error(_Fn&& __f) const &&
1132 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1133 using _Res = expected<_Tp, _Gr>;
1136 return _Res(in_place, std::move(_M_val));
1138 return _Res(__unexpect_inv{}, [&]() {
1139 return std::__invoke(std::forward<_Fn>(__f),
1140 std::move(_M_unex));
1144 // equality operators
1146 template<typename _Up, typename _Er2>
1147 requires (!is_void_v<_Up>)
1148 friend constexpr bool
1149 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1150 // FIXME: noexcept(noexcept(bool(*__x == *__y))
1151 // && noexcept(bool(__x.error() == __y.error())))
1153 if (__x.has_value())
1154 return __y.has_value() && bool(*__x == *__y);
1156 return !__y.has_value() && bool(__x.error() == __y.error());
1159 template<typename _Up>
1160 friend constexpr bool
1161 operator==(const expected& __x, const _Up& __v)
1162 // FIXME: noexcept(noexcept(bool(*__x == __v)))
1163 { return __x.has_value() && bool(*__x == __v); }
1165 template<typename _Er2>
1166 friend constexpr bool
1167 operator==(const expected& __x, const unexpected<_Er2>& __e)
1168 // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
1169 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1171 friend constexpr void
1172 swap(expected& __x, expected& __y)
1173 noexcept(noexcept(__x.swap(__y)))
1174 requires requires {__x.swap(__y);}
1178 template<typename, typename> friend class expected;
1180 template<typename _Vp>
1182 _M_assign_val(_Vp&& __v)
1185 _M_val = std::forward<_Vp>(__v);
1188 __expected::__reinit(__builtin_addressof(_M_val),
1189 __builtin_addressof(_M_unex),
1190 std::forward<_Vp>(__v));
1191 _M_has_value = true;
1195 template<typename _Vp>
1197 _M_assign_unex(_Vp&& __v)
1201 __expected::__reinit(__builtin_addressof(_M_unex),
1202 __builtin_addressof(_M_val),
1203 std::forward<_Vp>(__v));
1204 _M_has_value = false;
1207 _M_unex = std::forward<_Vp>(__v);
1210 // Swap two expected objects when only one has a value.
1211 // Precondition: this->_M_has_value && !__rhs._M_has_value
1213 _M_swap_val_unex(expected& __rhs)
1214 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1215 is_nothrow_move_constructible<_Tp>>)
1217 if constexpr (is_nothrow_move_constructible_v<_Er>)
1219 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1220 std::construct_at(__builtin_addressof(__rhs._M_val),
1221 std::move(_M_val)); // might throw
1222 __rhs._M_has_value = true;
1223 std::destroy_at(__builtin_addressof(_M_val));
1224 std::construct_at(__builtin_addressof(_M_unex),
1226 _M_has_value = false;
1230 __expected::_Guard<_Tp> __guard(_M_val);
1231 std::construct_at(__builtin_addressof(_M_unex),
1232 std::move(__rhs._M_unex)); // might throw
1233 _M_has_value = false;
1234 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1235 std::construct_at(__builtin_addressof(__rhs._M_val),
1237 __rhs._M_has_value = true;
1241 using __in_place_inv = __expected::__in_place_inv;
1242 using __unexpect_inv = __expected::__unexpect_inv;
1244 template<typename _Fn>
1246 expected(__in_place_inv, _Fn&& __fn)
1247 : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
1250 template<typename _Fn>
1252 expected(__unexpect_inv, _Fn&& __fn)
1253 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1264 // Partial specialization for std::expected<cv void, E>
1265 template<typename _Tp, typename _Er> requires is_void_v<_Tp>
1266 class expected<_Tp, _Er>
1268 static_assert( __expected::__can_be_unexpected<_Er> );
1270 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
1271 static constexpr bool __cons_from_expected
1272 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1273 is_constructible<_Unex, expected<_Up, _Err>>,
1274 is_constructible<_Unex, const expected<_Up, _Err>&>,
1275 is_constructible<_Unex, const expected<_Up, _Err>>
1278 template<typename _Up>
1279 static constexpr bool __same_val
1280 = is_same_v<typename _Up::value_type, _Tp>;
1282 template<typename _Up>
1283 static constexpr bool __same_err
1284 = is_same_v<typename _Up::error_type, _Er>;
1287 using value_type = _Tp;
1288 using error_type = _Er;
1289 using unexpected_type = unexpected<_Er>;
1291 template<typename _Up>
1292 using rebind = expected<_Up, error_type>;
1296 : _M_void(), _M_has_value(true)
1299 expected(const expected&) = default;
1302 expected(const expected& __x)
1303 noexcept(is_nothrow_copy_constructible_v<_Er>)
1304 requires is_copy_constructible_v<_Er>
1305 && (!is_trivially_copy_constructible_v<_Er>)
1306 : _M_void(), _M_has_value(__x._M_has_value)
1309 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1312 expected(expected&&) = default;
1315 expected(expected&& __x)
1316 noexcept(is_nothrow_move_constructible_v<_Er>)
1317 requires is_move_constructible_v<_Er>
1318 && (!is_trivially_move_constructible_v<_Er>)
1319 : _M_void(), _M_has_value(__x._M_has_value)
1322 std::construct_at(__builtin_addressof(_M_unex),
1323 std::move(__x)._M_unex);
1326 template<typename _Up, typename _Gr>
1327 requires is_void_v<_Up>
1328 && is_constructible_v<_Er, const _Gr&>
1329 && (!__cons_from_expected<_Up, _Gr>)
1330 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1331 expected(const expected<_Up, _Gr>& __x)
1332 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1333 : _M_void(), _M_has_value(__x._M_has_value)
1336 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1339 template<typename _Up, typename _Gr>
1340 requires is_void_v<_Up>
1341 && is_constructible_v<_Er, _Gr>
1342 && (!__cons_from_expected<_Up, _Gr>)
1343 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1344 expected(expected<_Up, _Gr>&& __x)
1345 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1346 : _M_void(), _M_has_value(__x._M_has_value)
1349 std::construct_at(__builtin_addressof(_M_unex),
1350 std::move(__x)._M_unex);
1353 template<typename _Gr = _Er>
1354 requires is_constructible_v<_Er, const _Gr&>
1355 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1356 expected(const unexpected<_Gr>& __u)
1357 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1358 : _M_unex(__u.error()), _M_has_value(false)
1361 template<typename _Gr = _Er>
1362 requires is_constructible_v<_Er, _Gr>
1363 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1364 expected(unexpected<_Gr>&& __u)
1365 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1366 : _M_unex(std::move(__u).error()), _M_has_value(false)
1370 expected(in_place_t) noexcept
1374 template<typename... _Args>
1375 requires is_constructible_v<_Er, _Args...>
1377 expected(unexpect_t, _Args&&... __args)
1378 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1379 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
1382 template<typename _Up, typename... _Args>
1383 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1385 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1386 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1388 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
1391 constexpr ~expected() = default;
1393 constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
1396 std::destroy_at(__builtin_addressof(_M_unex));
1401 expected& operator=(const expected&) = delete;
1404 operator=(const expected& __x)
1405 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1406 is_nothrow_copy_assignable<_Er>>)
1407 requires is_copy_constructible_v<_Er>
1408 && is_copy_assignable_v<_Er>
1410 if (__x._M_has_value)
1413 _M_assign_unex(__x._M_unex);
1418 operator=(expected&& __x)
1419 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1420 is_nothrow_move_assignable<_Er>>)
1421 requires is_move_constructible_v<_Er>
1422 && is_move_assignable_v<_Er>
1424 if (__x._M_has_value)
1427 _M_assign_unex(std::move(__x._M_unex));
1431 template<typename _Gr>
1432 requires is_constructible_v<_Er, const _Gr&>
1433 && is_assignable_v<_Er&, const _Gr&>
1435 operator=(const unexpected<_Gr>& __e)
1437 _M_assign_unex(__e.error());
1441 template<typename _Gr>
1442 requires is_constructible_v<_Er, _Gr>
1443 && is_assignable_v<_Er&, _Gr>
1445 operator=(unexpected<_Gr>&& __e)
1447 _M_assign_unex(std::move(__e.error()));
1458 std::destroy_at(__builtin_addressof(_M_unex));
1459 _M_has_value = true;
1466 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1467 is_nothrow_move_constructible<_Er>>)
1468 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1472 if (!__x._M_has_value)
1474 std::construct_at(__builtin_addressof(_M_unex),
1475 std::move(__x._M_unex)); // might throw
1476 std::destroy_at(__builtin_addressof(__x._M_unex));
1477 _M_has_value = false;
1478 __x._M_has_value = true;
1483 if (__x._M_has_value)
1485 std::construct_at(__builtin_addressof(__x._M_unex),
1486 std::move(_M_unex)); // might throw
1487 std::destroy_at(__builtin_addressof(_M_unex));
1488 _M_has_value = true;
1489 __x._M_has_value = false;
1494 swap(_M_unex, __x._M_unex);
1503 operator bool() const noexcept { return _M_has_value; }
1506 constexpr bool has_value() const noexcept { return _M_has_value; }
1509 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1514 if (_M_has_value) [[likely]]
1516 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1522 if (_M_has_value) [[likely]]
1524 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
1527 constexpr const _Er&
1528 error() const & noexcept
1530 __glibcxx_assert(!_M_has_value);
1537 __glibcxx_assert(!_M_has_value);
1541 constexpr const _Er&&
1542 error() const && noexcept
1544 __glibcxx_assert(!_M_has_value);
1545 return std::move(_M_unex);
1551 __glibcxx_assert(!_M_has_value);
1552 return std::move(_M_unex);
1555 template<typename _Gr = _Er>
1557 error_or(_Gr&& __e) const&
1559 static_assert( is_copy_constructible_v<_Er> );
1560 static_assert( is_convertible_v<_Gr, _Er> );
1563 return std::forward<_Gr>(__e);
1567 template<typename _Gr = _Er>
1569 error_or(_Gr&& __e) &&
1571 static_assert( is_move_constructible_v<_Er> );
1572 static_assert( is_convertible_v<_Gr, _Er> );
1575 return std::forward<_Gr>(__e);
1576 return std::move(_M_unex);
1579 // monadic operations
1581 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1583 and_then(_Fn&& __f) &
1585 using _Up = __expected::__result0<_Fn>;
1586 static_assert(__expected::__is_expected<_Up>);
1587 static_assert(is_same_v<typename _Up::error_type, _Er>);
1590 return std::__invoke(std::forward<_Fn>(__f));
1592 return _Up(unexpect, _M_unex);
1595 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1597 and_then(_Fn&& __f) const &
1599 using _Up = __expected::__result0<_Fn>;
1600 static_assert(__expected::__is_expected<_Up>);
1601 static_assert(is_same_v<typename _Up::error_type, _Er>);
1604 return std::__invoke(std::forward<_Fn>(__f));
1606 return _Up(unexpect, _M_unex);
1609 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1611 and_then(_Fn&& __f) &&
1613 using _Up = __expected::__result0<_Fn>;
1614 static_assert(__expected::__is_expected<_Up>);
1615 static_assert(is_same_v<typename _Up::error_type, _Er>);
1618 return std::__invoke(std::forward<_Fn>(__f));
1620 return _Up(unexpect, std::move(_M_unex));
1623 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1625 and_then(_Fn&& __f) const &&
1627 using _Up = __expected::__result0<_Fn>;
1628 static_assert(__expected::__is_expected<_Up>);
1629 static_assert(is_same_v<typename _Up::error_type, _Er>);
1632 return std::__invoke(std::forward<_Fn>(__f));
1634 return _Up(unexpect, std::move(_M_unex));
1637 template<typename _Fn>
1639 or_else(_Fn&& __f) &
1641 using _Gr = __expected::__result<_Fn, _Er&>;
1642 static_assert(__expected::__is_expected<_Gr>);
1643 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1648 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1651 template<typename _Fn>
1653 or_else(_Fn&& __f) const &
1655 using _Gr = __expected::__result<_Fn, const _Er&>;
1656 static_assert(__expected::__is_expected<_Gr>);
1657 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1662 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1665 template<typename _Fn>
1667 or_else(_Fn&& __f) &&
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>);
1676 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1679 template<typename _Fn>
1681 or_else(_Fn&& __f) const &&
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>);
1690 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1693 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1695 transform(_Fn&& __f) &
1697 using _Up = __expected::__result0_xform<_Fn>;
1698 using _Res = expected<_Up, _Er>;
1701 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1703 return _Res(unexpect, _M_unex);
1706 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1708 transform(_Fn&& __f) const &
1710 using _Up = __expected::__result0_xform<_Fn>;
1711 using _Res = expected<_Up, _Er>;
1714 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1716 return _Res(unexpect, _M_unex);
1719 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1721 transform(_Fn&& __f) &&
1723 using _Up = __expected::__result0_xform<_Fn>;
1724 using _Res = expected<_Up, _Er>;
1727 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1729 return _Res(unexpect, std::move(_M_unex));
1732 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1734 transform(_Fn&& __f) const &&
1736 using _Up = __expected::__result0_xform<_Fn>;
1737 using _Res = expected<_Up, _Er>;
1740 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1742 return _Res(unexpect, std::move(_M_unex));
1745 template<typename _Fn>
1747 transform_error(_Fn&& __f) &
1749 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1750 using _Res = expected<_Tp, _Gr>;
1755 return _Res(__unexpect_inv{}, [&]() {
1756 return std::__invoke(std::forward<_Fn>(__f),
1761 template<typename _Fn>
1763 transform_error(_Fn&& __f) const &
1765 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1766 using _Res = expected<_Tp, _Gr>;
1771 return _Res(__unexpect_inv{}, [&]() {
1772 return std::__invoke(std::forward<_Fn>(__f),
1777 template<typename _Fn>
1779 transform_error(_Fn&& __f) &&
1781 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1782 using _Res = expected<_Tp, _Gr>;
1787 return _Res(__unexpect_inv{}, [&]() {
1788 return std::__invoke(std::forward<_Fn>(__f),
1789 std::move(_M_unex));
1793 template<typename _Fn>
1795 transform_error(_Fn&& __f) const &&
1797 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1798 using _Res = expected<_Tp, _Gr>;
1803 return _Res(__unexpect_inv{}, [&]() {
1804 return std::__invoke(std::forward<_Fn>(__f),
1805 std::move(_M_unex));
1809 // equality operators
1811 template<typename _Up, typename _Er2>
1812 requires is_void_v<_Up>
1813 friend constexpr bool
1814 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1815 // FIXME: noexcept(noexcept(bool(__x.error() == __y.error())))
1817 if (__x.has_value())
1818 return __y.has_value();
1820 return !__y.has_value() && bool(__x.error() == __y.error());
1823 template<typename _Er2>
1824 friend constexpr bool
1825 operator==(const expected& __x, const unexpected<_Er2>& __e)
1826 // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
1827 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1829 friend constexpr void
1830 swap(expected& __x, expected& __y)
1831 noexcept(noexcept(__x.swap(__y)))
1832 requires requires { __x.swap(__y); }
1836 template<typename, typename> friend class expected;
1838 template<typename _Vp>
1840 _M_assign_unex(_Vp&& __v)
1844 std::construct_at(__builtin_addressof(_M_unex),
1845 std::forward<_Vp>(__v));
1846 _M_has_value = false;
1849 _M_unex = std::forward<_Vp>(__v);
1852 using __in_place_inv = __expected::__in_place_inv;
1853 using __unexpect_inv = __expected::__unexpect_inv;
1855 template<typename _Fn>
1857 expected(__in_place_inv, _Fn&& __fn)
1858 : _M_void(), _M_has_value(true)
1859 { std::forward<_Fn>(__fn)(); }
1861 template<typename _Fn>
1863 expected(__unexpect_inv, _Fn&& __fn)
1864 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1876_GLIBCXX_END_NAMESPACE_VERSION
1879#endif // __cpp_lib_expected
1880#endif // _GLIBCXX_EXPECTED