29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
32#pragma GCC system_header
34#define __glibcxx_want_expected
35#define __glibcxx_want_freestanding_expected
38#ifdef __cpp_lib_expected
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
60 template<
typename _Tp,
typename _Er>
67 template<
typename _Er>
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&)
noexcept =
default;
83 bad_expected_access(bad_expected_access&&) noexcept = default;
84 bad_expected_access& operator=(const bad_expected_access&) noexcept = default;
85 bad_expected_access& operator=(bad_expected_access&&) noexcept = 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)) { }
111 error() const & noexcept
121 error() const && noexcept
134 explicit unexpect_t() =
default;
141 inline constexpr unexpect_t unexpect{};
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>);
172 struct __in_place_inv { };
173 struct __unexpect_inv { };
177 template<
typename _Er>
180 static_assert( __expected::__can_be_unexpected<_Er> );
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>
192 noexcept(is_nothrow_constructible_v<_Er, _Err>)
196 template<
typename... _Args>
197 requires is_constructible_v<_Er, _Args...>
200 noexcept(is_nothrow_constructible_v<_Er, _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)...)
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
249 requires is_swappable_v<_Er>
256 template<
typename _Er>
unexpected(_Er) -> unexpected<_Er>;
261 template<
typename _Tp>
264 static_assert( is_nothrow_move_constructible_v<_Tp> );
268 : _M_guarded(__builtin_addressof(__x)), _M_tmp(
std::
move(__x))
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;
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));
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));
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> );
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>>
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;
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),
433 std::construct_at(__builtin_addressof(_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),
467 std::construct_at(__builtin_addressof(_M_unex),
471 template<
typename _Up = remove_cv_t<_Tp>>
472 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
473 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
474 && is_constructible_v<_Tp, _Up>
475 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
476 && __expected::__not_constructing_bool_from_expected<_Tp, _Up>
477 constexpr explicit(!is_convertible_v<_Up, _Tp>)
479 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
480 : _M_val(
std::
forward<_Up>(__v)), _M_has_value(true)
483 template<
typename _Gr = _Er>
484 requires is_constructible_v<_Er, const _Gr&>
485 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
486 expected(
const unexpected<_Gr>& __u)
487 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
488 : _M_unex(__u.error()), _M_has_value(false)
491 template<
typename _Gr = _Er>
492 requires is_constructible_v<_Er, _Gr>
493 constexpr explicit(!is_convertible_v<_Gr, _Er>)
494 expected(unexpected<_Gr>&& __u)
495 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
496 : _M_unex(
std::
move(__u).error()), _M_has_value(false)
499 template<
typename... _Args>
500 requires is_constructible_v<_Tp, _Args...>
502 expected(in_place_t, _Args&&... __args)
503 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
504 : _M_val(
std::
forward<_Args>(__args)...), _M_has_value(true)
507 template<
typename _Up,
typename... _Args>
508 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
510 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
511 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
513 : _M_val(__il,
std::
forward<_Args>(__args)...), _M_has_value(true)
516 template<
typename... _Args>
517 requires is_constructible_v<_Er, _Args...>
519 expected(unexpect_t, _Args&&... __args)
520 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
521 : _M_unex(
std::
forward<_Args>(__args)...), _M_has_value(false)
524 template<
typename _Up,
typename... _Args>
525 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
527 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
528 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
530 : _M_unex(__il,
std::
forward<_Args>(__args)...), _M_has_value(false)
533 constexpr ~expected() =
default;
535 constexpr ~expected()
536 requires (!is_trivially_destructible_v<_Tp>)
537 || (!is_trivially_destructible_v<_Er>)
540 std::destroy_at(__builtin_addressof(_M_val));
542 std::destroy_at(__builtin_addressof(_M_unex));
547 expected& operator=(
const expected&) =
delete;
550 operator=(
const expected& __x)
551 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
552 is_nothrow_copy_constructible<_Er>,
553 is_nothrow_copy_assignable<_Tp>,
554 is_nothrow_copy_assignable<_Er>>)
555 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
556 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
557 && (is_nothrow_move_constructible_v<_Tp>
558 || is_nothrow_move_constructible_v<_Er>)
560 if (__x._M_has_value)
561 this->_M_assign_val(__x._M_val);
563 this->_M_assign_unex(__x._M_unex);
568 operator=(expected&& __x)
569 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
570 is_nothrow_move_constructible<_Er>,
571 is_nothrow_move_assignable<_Tp>,
572 is_nothrow_move_assignable<_Er>>)
573 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
574 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
575 && (is_nothrow_move_constructible_v<_Tp>
576 || is_nothrow_move_constructible_v<_Er>)
578 if (__x._M_has_value)
585 template<
typename _Up = remove_cv_t<_Tp>>
586 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
587 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
588 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
589 && (is_nothrow_constructible_v<_Tp, _Up>
590 || is_nothrow_move_constructible_v<_Tp>
591 || is_nothrow_move_constructible_v<_Er>)
595 _M_assign_val(std::forward<_Up>(__v));
599 template<
typename _Gr>
600 requires is_constructible_v<_Er, const _Gr&>
601 && is_assignable_v<_Er&, const _Gr&>
602 && (is_nothrow_constructible_v<_Er, const _Gr&>
603 || is_nothrow_move_constructible_v<_Tp>
604 || is_nothrow_move_constructible_v<_Er>)
606 operator=(
const unexpected<_Gr>& __e)
608 _M_assign_unex(__e.error());
612 template<
typename _Gr>
613 requires is_constructible_v<_Er, _Gr>
614 && is_assignable_v<_Er&, _Gr>
615 && (is_nothrow_constructible_v<_Er, _Gr>
616 || is_nothrow_move_constructible_v<_Tp>
617 || is_nothrow_move_constructible_v<_Er>)
619 operator=(unexpected<_Gr>&& __e)
627 template<
typename... _Args>
628 requires is_nothrow_constructible_v<_Tp, _Args...>
630 emplace(_Args&&... __args)
noexcept
633 std::destroy_at(__builtin_addressof(_M_val));
636 std::destroy_at(__builtin_addressof(_M_unex));
639 std::construct_at(__builtin_addressof(_M_val),
640 std::forward<_Args>(__args)...);
644 template<
typename _Up,
typename... _Args>
645 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
648 emplace(initializer_list<_Up> __il, _Args&&... __args)
noexcept
651 std::destroy_at(__builtin_addressof(_M_val));
654 std::destroy_at(__builtin_addressof(_M_unex));
657 std::construct_at(__builtin_addressof(_M_val),
658 __il, std::forward<_Args>(__args)...);
665 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
666 is_nothrow_move_constructible<_Er>,
667 is_nothrow_swappable<_Tp&>,
668 is_nothrow_swappable<_Er&>>)
669 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
670 && is_move_constructible_v<_Tp>
671 && is_move_constructible_v<_Er>
672 && (is_nothrow_move_constructible_v<_Tp>
673 || is_nothrow_move_constructible_v<_Er>)
677 if (__x._M_has_value)
680 swap(_M_val, __x._M_val);
683 this->_M_swap_val_unex(__x);
687 if (__x._M_has_value)
688 __x._M_swap_val_unex(*
this);
692 swap(_M_unex, __x._M_unex);
701 operator->() const noexcept
703 __glibcxx_assert(_M_has_value);
704 return __builtin_addressof(_M_val);
709 operator->() noexcept
711 __glibcxx_assert(_M_has_value);
712 return __builtin_addressof(_M_val);
719 __glibcxx_assert(_M_has_value);
727 __glibcxx_assert(_M_has_value);
732 constexpr const _Tp&&
735 __glibcxx_assert(_M_has_value);
743 __glibcxx_assert(_M_has_value);
749 operator bool() const noexcept {
return _M_has_value; }
752 constexpr bool has_value() const noexcept {
return _M_has_value; }
757 static_assert( is_copy_constructible_v<_Er> );
758 if (_M_has_value) [[likely]]
760 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
766 static_assert( is_copy_constructible_v<_Er> );
767 if (_M_has_value) [[likely]]
769 const auto& __unex = _M_unex;
770 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
773 constexpr const _Tp&&
776 static_assert( is_copy_constructible_v<_Er> );
777 static_assert( is_constructible_v<_Er, const _Er&&> );
778 if (_M_has_value) [[likely]]
780 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
786 static_assert( is_copy_constructible_v<_Er> );
787 static_assert( is_constructible_v<_Er, _Er&&> );
788 if (_M_has_value) [[likely]]
790 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
794 error() const & noexcept
796 __glibcxx_assert(!_M_has_value);
803 __glibcxx_assert(!_M_has_value);
807 constexpr const _Er&&
808 error() const && noexcept
810 __glibcxx_assert(!_M_has_value);
817 __glibcxx_assert(!_M_has_value);
821 template<
typename _Up = remove_cv_t<_Tp>>
823 value_or(_Up&& __v)
const &
824 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
825 is_nothrow_convertible<_Up, _Tp>>)
827 static_assert( is_copy_constructible_v<_Tp> );
828 static_assert( is_convertible_v<_Up, _Tp> );
832 return static_cast<_Tp
>(std::forward<_Up>(__v));
835 template<
typename _Up = remove_cv_t<_Tp>>
837 value_or(_Up&& __v) &&
838 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
839 is_nothrow_convertible<_Up, _Tp>>)
841 static_assert( is_move_constructible_v<_Tp> );
842 static_assert( is_convertible_v<_Up, _Tp> );
846 return static_cast<_Tp
>(std::forward<_Up>(__v));
849 template<
typename _Gr = _Er>
851 error_or(_Gr&& __e)
const&
853 static_assert( is_copy_constructible_v<_Er> );
854 static_assert( is_convertible_v<_Gr, _Er> );
857 return std::forward<_Gr>(__e);
861 template<
typename _Gr = _Er>
863 error_or(_Gr&& __e) &&
865 static_assert( is_move_constructible_v<_Er> );
866 static_assert( is_convertible_v<_Gr, _Er> );
869 return std::forward<_Gr>(__e);
875 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
877 and_then(_Fn&& __f) &
879 using _Up = __expected::__result<_Fn, _Tp&>;
880 static_assert(__expected::__is_expected<_Up>,
881 "the function passed to std::expected<T, E>::and_then "
882 "must return a std::expected");
883 static_assert(is_same_v<typename _Up::error_type, _Er>,
884 "the function passed to std::expected<T, E>::and_then "
885 "must return a std::expected with the same error_type");
890 return _Up(unexpect, _M_unex);
893 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
895 and_then(_Fn&& __f)
const &
897 using _Up = __expected::__result<_Fn, const _Tp&>;
898 static_assert(__expected::__is_expected<_Up>,
899 "the function passed to std::expected<T, E>::and_then "
900 "must return a std::expected");
901 static_assert(is_same_v<typename _Up::error_type, _Er>,
902 "the function passed to std::expected<T, E>::and_then "
903 "must return a std::expected with the same error_type");
908 return _Up(unexpect, _M_unex);
911 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
913 and_then(_Fn&& __f) &&
915 using _Up = __expected::__result<_Fn, _Tp&&>;
916 static_assert(__expected::__is_expected<_Up>,
917 "the function passed to std::expected<T, E>::and_then "
918 "must return a std::expected");
919 static_assert(is_same_v<typename _Up::error_type, _Er>,
920 "the function passed to std::expected<T, E>::and_then "
921 "must return a std::expected with the same error_type");
926 return _Up(unexpect,
std::move(_M_unex));
930 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
932 and_then(_Fn&& __f)
const &&
934 using _Up = __expected::__result<_Fn, const _Tp&&>;
935 static_assert(__expected::__is_expected<_Up>,
936 "the function passed to std::expected<T, E>::and_then "
937 "must return a std::expected");
938 static_assert(is_same_v<typename _Up::error_type, _Er>,
939 "the function passed to std::expected<T, E>::and_then "
940 "must return a std::expected with the same error_type");
945 return _Up(unexpect,
std::move(_M_unex));
948 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
952 using _Gr = __expected::__result<_Fn, _Er&>;
953 static_assert(__expected::__is_expected<_Gr>,
954 "the function passed to std::expected<T, E>::or_else "
955 "must return a std::expected");
956 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
957 "the function passed to std::expected<T, E>::or_else "
958 "must return a std::expected with the same value_type");
961 return _Gr(in_place, _M_val);
966 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
968 or_else(_Fn&& __f)
const &
970 using _Gr = __expected::__result<_Fn, const _Er&>;
971 static_assert(__expected::__is_expected<_Gr>,
972 "the function passed to std::expected<T, E>::or_else "
973 "must return a std::expected");
974 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
975 "the function passed to std::expected<T, E>::or_else "
976 "must return a std::expected with the same value_type");
979 return _Gr(in_place, _M_val);
985 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
987 or_else(_Fn&& __f) &&
989 using _Gr = __expected::__result<_Fn, _Er&&>;
990 static_assert(__expected::__is_expected<_Gr>,
991 "the function passed to std::expected<T, E>::or_else "
992 "must return a std::expected");
993 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
994 "the function passed to std::expected<T, E>::or_else "
995 "must return a std::expected with the same value_type");
1003 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1005 or_else(_Fn&& __f)
const &&
1007 using _Gr = __expected::__result<_Fn, const _Er&&>;
1008 static_assert(__expected::__is_expected<_Gr>,
1009 "the function passed to std::expected<T, E>::or_else "
1010 "must return a std::expected");
1011 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1012 "the function passed to std::expected<T, E>::or_else "
1013 "must return a std::expected with the same value_type");
1016 return _Gr(in_place,
std::move(_M_val));
1021 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1023 transform(_Fn&& __f) &
1025 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1026 using _Res = expected<_Up, _Er>;
1029 return _Res(__in_place_inv{}, [&]() {
1034 return _Res(unexpect, _M_unex);
1037 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1039 transform(_Fn&& __f)
const &
1041 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1042 using _Res = expected<_Up, _Er>;
1045 return _Res(__in_place_inv{}, [&]() {
1050 return _Res(unexpect, _M_unex);
1053 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1055 transform(_Fn&& __f) &&
1057 using _Up = __expected::__result_xform<_Fn, _Tp>;
1058 using _Res = expected<_Up, _Er>;
1061 return _Res(__in_place_inv{}, [&]() {
1066 return _Res(unexpect,
std::move(_M_unex));
1069 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1071 transform(_Fn&& __f)
const &&
1073 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1074 using _Res = expected<_Up, _Er>;
1077 return _Res(__in_place_inv{}, [&]() {
1082 return _Res(unexpect,
std::move(_M_unex));
1085 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
1087 transform_error(_Fn&& __f) &
1089 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1090 using _Res = expected<_Tp, _Gr>;
1093 return _Res(in_place, _M_val);
1095 return _Res(__unexpect_inv{}, [&]() {
1101 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
1103 transform_error(_Fn&& __f)
const &
1105 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1106 using _Res = expected<_Tp, _Gr>;
1109 return _Res(in_place, _M_val);
1111 return _Res(__unexpect_inv{}, [&]() {
1117 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
1119 transform_error(_Fn&& __f) &&
1121 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1122 using _Res = expected<_Tp, _Gr>;
1125 return _Res(in_place,
std::move(_M_val));
1127 return _Res(__unexpect_inv{}, [&]() {
1133 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1135 transform_error(_Fn&& __f)
const &&
1137 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1138 using _Res = expected<_Tp, _Gr>;
1141 return _Res(in_place,
std::move(_M_val));
1143 return _Res(__unexpect_inv{}, [&]() {
1151 template<
typename _Up,
typename _Er2>
1152 requires (!is_void_v<_Up>)
1153 friend constexpr bool
1154 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1158 if (__x.has_value())
1159 return __y.has_value() && bool(*__x == *__y);
1161 return !__y.has_value() && bool(__x.error() == __y.error());
1164 template<
typename _Up>
1165 friend constexpr bool
1166 operator==(
const expected& __x,
const _Up& __v)
1168 {
return __x.has_value() && bool(*__x == __v); }
1170 template<
typename _Er2>
1171 friend constexpr bool
1172 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1174 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1176 friend constexpr void
1177 swap(expected& __x, expected& __y)
1178 noexcept(
noexcept(__x.swap(__y)))
1179 requires requires {__x.swap(__y);}
1183 template<
typename,
typename>
friend class expected;
1185 template<
typename _Vp>
1187 _M_assign_val(_Vp&& __v)
1190 _M_val = std::forward<_Vp>(__v);
1193 __expected::__reinit(__builtin_addressof(_M_val),
1194 __builtin_addressof(_M_unex),
1195 std::forward<_Vp>(__v));
1196 _M_has_value =
true;
1200 template<
typename _Vp>
1202 _M_assign_unex(_Vp&& __v)
1206 __expected::__reinit(__builtin_addressof(_M_unex),
1207 __builtin_addressof(_M_val),
1208 std::forward<_Vp>(__v));
1209 _M_has_value =
false;
1212 _M_unex = std::forward<_Vp>(__v);
1218 _M_swap_val_unex(expected& __rhs)
1219 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1220 is_nothrow_move_constructible<_Tp>>)
1222 if constexpr (is_nothrow_move_constructible_v<_Er>)
1224 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1225 std::construct_at(__builtin_addressof(__rhs._M_val),
1227 __rhs._M_has_value =
true;
1228 std::destroy_at(__builtin_addressof(_M_val));
1229 std::construct_at(__builtin_addressof(_M_unex),
1231 _M_has_value =
false;
1235 __expected::_Guard<_Tp> __guard(_M_val);
1236 std::construct_at(__builtin_addressof(_M_unex),
1238 _M_has_value =
false;
1239 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1240 std::construct_at(__builtin_addressof(__rhs._M_val),
1242 __rhs._M_has_value =
true;
1246 using __in_place_inv = __expected::__in_place_inv;
1247 using __unexpect_inv = __expected::__unexpect_inv;
1249 template<
typename _Fn>
1251 expected(__in_place_inv, _Fn&& __fn)
1252 : _M_val(
std::
forward<_Fn>(__fn)()), _M_has_value(true)
1255 template<
typename _Fn>
1257 expected(__unexpect_inv, _Fn&& __fn)
1258 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1270 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1271 class expected<_Tp, _Er>
1273 static_assert( __expected::__can_be_unexpected<_Er> );
1275 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1276 static constexpr bool __cons_from_expected
1277 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1278 is_constructible<_Unex, expected<_Up, _Err>>,
1279 is_constructible<_Unex, const expected<_Up, _Err>&>,
1280 is_constructible<_Unex, const expected<_Up, _Err>>
1283 template<
typename _Up>
1284 static constexpr bool __same_val
1285 = is_same_v<typename _Up::value_type, _Tp>;
1287 template<
typename _Up>
1288 static constexpr bool __same_err
1289 = is_same_v<typename _Up::error_type, _Er>;
1292 using value_type = _Tp;
1294 using unexpected_type = unexpected<_Er>;
1296 template<
typename _Up>
1297 using rebind = expected<_Up, error_type>;
1301 : _M_void(), _M_has_value(true)
1304 expected(
const expected&) =
default;
1307 expected(
const expected& __x)
1308 noexcept(is_nothrow_copy_constructible_v<_Er>)
1309 requires is_copy_constructible_v<_Er>
1310 && (!is_trivially_copy_constructible_v<_Er>)
1311 : _M_void(), _M_has_value(__x._M_has_value)
1314 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1317 expected(expected&&) =
default;
1320 expected(expected&& __x)
1321 noexcept(is_nothrow_move_constructible_v<_Er>)
1322 requires is_move_constructible_v<_Er>
1323 && (!is_trivially_move_constructible_v<_Er>)
1324 : _M_void(), _M_has_value(__x._M_has_value)
1327 std::construct_at(__builtin_addressof(_M_unex),
1331 template<
typename _Up,
typename _Gr>
1332 requires is_void_v<_Up>
1333 && is_constructible_v<_Er, const _Gr&>
1334 && (!__cons_from_expected<_Up, _Gr>)
1335 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1336 expected(
const expected<_Up, _Gr>& __x)
1337 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1338 : _M_void(), _M_has_value(__x._M_has_value)
1341 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1344 template<
typename _Up,
typename _Gr>
1345 requires is_void_v<_Up>
1346 && is_constructible_v<_Er, _Gr>
1347 && (!__cons_from_expected<_Up, _Gr>)
1348 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1349 expected(expected<_Up, _Gr>&& __x)
1350 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1351 : _M_void(), _M_has_value(__x._M_has_value)
1354 std::construct_at(__builtin_addressof(_M_unex),
1358 template<
typename _Gr = _Er>
1359 requires is_constructible_v<_Er, const _Gr&>
1360 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1361 expected(
const unexpected<_Gr>& __u)
1362 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1363 : _M_unex(__u.error()), _M_has_value(false)
1366 template<
typename _Gr = _Er>
1367 requires is_constructible_v<_Er, _Gr>
1368 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1369 expected(unexpected<_Gr>&& __u)
1370 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1371 : _M_unex(
std::
move(__u).error()), _M_has_value(false)
1375 expected(in_place_t) noexcept
1379 template<
typename... _Args>
1380 requires is_constructible_v<_Er, _Args...>
1382 expected(unexpect_t, _Args&&... __args)
1383 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1384 : _M_unex(
std::
forward<_Args>(__args)...), _M_has_value(false)
1387 template<
typename _Up,
typename... _Args>
1388 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1390 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1391 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1393 : _M_unex(__il,
std::
forward<_Args>(__args)...), _M_has_value(false)
1396 constexpr ~expected() =
default;
1398 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1401 std::destroy_at(__builtin_addressof(_M_unex));
1406 expected& operator=(
const expected&) =
delete;
1409 operator=(
const expected& __x)
1410 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1411 is_nothrow_copy_assignable<_Er>>)
1412 requires is_copy_constructible_v<_Er>
1413 && is_copy_assignable_v<_Er>
1415 if (__x._M_has_value)
1418 _M_assign_unex(__x._M_unex);
1423 operator=(expected&& __x)
1424 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1425 is_nothrow_move_assignable<_Er>>)
1426 requires is_move_constructible_v<_Er>
1427 && is_move_assignable_v<_Er>
1429 if (__x._M_has_value)
1436 template<
typename _Gr>
1437 requires is_constructible_v<_Er, const _Gr&>
1438 && is_assignable_v<_Er&, const _Gr&>
1440 operator=(
const unexpected<_Gr>& __e)
1442 _M_assign_unex(__e.error());
1446 template<
typename _Gr>
1447 requires is_constructible_v<_Er, _Gr>
1448 && is_assignable_v<_Er&, _Gr>
1450 operator=(unexpected<_Gr>&& __e)
1463 std::destroy_at(__builtin_addressof(_M_unex));
1464 _M_has_value =
true;
1471 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1472 is_nothrow_move_constructible<_Er>>)
1473 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1477 if (!__x._M_has_value)
1479 std::construct_at(__builtin_addressof(_M_unex),
1481 std::destroy_at(__builtin_addressof(__x._M_unex));
1482 _M_has_value =
false;
1483 __x._M_has_value =
true;
1488 if (__x._M_has_value)
1490 std::construct_at(__builtin_addressof(__x._M_unex),
1492 std::destroy_at(__builtin_addressof(_M_unex));
1493 _M_has_value =
true;
1494 __x._M_has_value =
false;
1499 swap(_M_unex, __x._M_unex);
1508 operator bool() const noexcept {
return _M_has_value; }
1511 constexpr bool has_value() const noexcept {
return _M_has_value; }
1514 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1519 static_assert( is_copy_constructible_v<_Er> );
1520 if (_M_has_value) [[likely]]
1522 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1528 static_assert( is_copy_constructible_v<_Er> );
1529 if (_M_has_value) [[likely]]
1531 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1534 constexpr const _Er&
1535 error() const & noexcept
1537 __glibcxx_assert(!_M_has_value);
1544 __glibcxx_assert(!_M_has_value);
1548 constexpr const _Er&&
1549 error() const && noexcept
1551 __glibcxx_assert(!_M_has_value);
1558 __glibcxx_assert(!_M_has_value);
1562 template<
typename _Gr = _Er>
1564 error_or(_Gr&& __e)
const&
1566 static_assert( is_copy_constructible_v<_Er> );
1567 static_assert( is_convertible_v<_Gr, _Er> );
1570 return std::forward<_Gr>(__e);
1574 template<
typename _Gr = _Er>
1576 error_or(_Gr&& __e) &&
1578 static_assert( is_move_constructible_v<_Er> );
1579 static_assert( is_convertible_v<_Gr, _Er> );
1582 return std::forward<_Gr>(__e);
1588 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1590 and_then(_Fn&& __f) &
1592 using _Up = __expected::__result0<_Fn>;
1593 static_assert(__expected::__is_expected<_Up>);
1594 static_assert(is_same_v<typename _Up::error_type, _Er>);
1599 return _Up(unexpect, _M_unex);
1602 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1604 and_then(_Fn&& __f)
const &
1606 using _Up = __expected::__result0<_Fn>;
1607 static_assert(__expected::__is_expected<_Up>);
1608 static_assert(is_same_v<typename _Up::error_type, _Er>);
1613 return _Up(unexpect, _M_unex);
1616 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1618 and_then(_Fn&& __f) &&
1620 using _Up = __expected::__result0<_Fn>;
1621 static_assert(__expected::__is_expected<_Up>);
1622 static_assert(is_same_v<typename _Up::error_type, _Er>);
1627 return _Up(unexpect,
std::move(_M_unex));
1630 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1632 and_then(_Fn&& __f)
const &&
1634 using _Up = __expected::__result0<_Fn>;
1635 static_assert(__expected::__is_expected<_Up>);
1636 static_assert(is_same_v<typename _Up::error_type, _Er>);
1641 return _Up(unexpect,
std::move(_M_unex));
1644 template<
typename _Fn>
1646 or_else(_Fn&& __f) &
1648 using _Gr = __expected::__result<_Fn, _Er&>;
1649 static_assert(__expected::__is_expected<_Gr>);
1650 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1658 template<
typename _Fn>
1660 or_else(_Fn&& __f)
const &
1662 using _Gr = __expected::__result<_Fn, const _Er&>;
1663 static_assert(__expected::__is_expected<_Gr>);
1664 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1672 template<
typename _Fn>
1674 or_else(_Fn&& __f) &&
1676 using _Gr = __expected::__result<_Fn, _Er&&>;
1677 static_assert(__expected::__is_expected<_Gr>);
1678 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1686 template<
typename _Fn>
1688 or_else(_Fn&& __f)
const &&
1690 using _Gr = __expected::__result<_Fn, const _Er&&>;
1691 static_assert(__expected::__is_expected<_Gr>);
1692 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1700 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1702 transform(_Fn&& __f) &
1704 using _Up = __expected::__result0_xform<_Fn>;
1705 using _Res = expected<_Up, _Er>;
1708 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1710 return _Res(unexpect, _M_unex);
1713 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1715 transform(_Fn&& __f)
const &
1717 using _Up = __expected::__result0_xform<_Fn>;
1718 using _Res = expected<_Up, _Er>;
1721 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1723 return _Res(unexpect, _M_unex);
1726 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1728 transform(_Fn&& __f) &&
1730 using _Up = __expected::__result0_xform<_Fn>;
1731 using _Res = expected<_Up, _Er>;
1734 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1736 return _Res(unexpect,
std::move(_M_unex));
1739 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1741 transform(_Fn&& __f)
const &&
1743 using _Up = __expected::__result0_xform<_Fn>;
1744 using _Res = expected<_Up, _Er>;
1747 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1749 return _Res(unexpect,
std::move(_M_unex));
1752 template<
typename _Fn>
1754 transform_error(_Fn&& __f) &
1756 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1757 using _Res = expected<_Tp, _Gr>;
1762 return _Res(__unexpect_inv{}, [&]() {
1768 template<
typename _Fn>
1770 transform_error(_Fn&& __f)
const &
1772 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1773 using _Res = expected<_Tp, _Gr>;
1778 return _Res(__unexpect_inv{}, [&]() {
1784 template<
typename _Fn>
1786 transform_error(_Fn&& __f) &&
1788 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1789 using _Res = expected<_Tp, _Gr>;
1794 return _Res(__unexpect_inv{}, [&]() {
1800 template<
typename _Fn>
1802 transform_error(_Fn&& __f)
const &&
1804 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1805 using _Res = expected<_Tp, _Gr>;
1810 return _Res(__unexpect_inv{}, [&]() {
1818 template<
typename _Up,
typename _Er2>
1819 requires is_void_v<_Up>
1820 friend constexpr bool
1821 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1824 if (__x.has_value())
1825 return __y.has_value();
1827 return !__y.has_value() && bool(__x.error() == __y.error());
1830 template<
typename _Er2>
1831 friend constexpr bool
1832 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1834 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1836 friend constexpr void
1837 swap(expected& __x, expected& __y)
1838 noexcept(
noexcept(__x.swap(__y)))
1839 requires requires { __x.swap(__y); }
1843 template<
typename,
typename>
friend class expected;
1845 template<
typename _Vp>
1847 _M_assign_unex(_Vp&& __v)
1851 std::construct_at(__builtin_addressof(_M_unex),
1852 std::forward<_Vp>(__v));
1853 _M_has_value =
false;
1856 _M_unex = std::forward<_Vp>(__v);
1859 using __in_place_inv = __expected::__in_place_inv;
1860 using __unexpect_inv = __expected::__unexpect_inv;
1862 template<
typename _Fn>
1864 expected(__in_place_inv, _Fn&& __fn)
1865 : _M_void(), _M_has_value(true)
1866 { std::forward<_Fn>(__fn)(); }
1868 template<
typename _Fn>
1870 expected(__unexpect_inv, _Fn&& __fn)
1871 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1883_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.