29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
33#pragma GCC system_header
36#define __glibcxx_want_expected
37#define __glibcxx_want_freestanding_expected
38#define __glibcxx_want_constrained_equality
41#ifdef __cpp_lib_expected
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<
typename _Tp,
typename _Er>
70 template<
typename _Er>
77 template<
typename _Er>
78 class bad_expected_access;
81 class bad_expected_access<void> :
public exception
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;
95 what() const noexcept
override
96 {
return "bad access to std::expected without expected value"; }
99 template<
typename _Er>
100 class bad_expected_access :
public bad_expected_access<void> {
103 bad_expected_access(_Er __e) : _M_unex(std::
move(__e)) { }
114 error() const & noexcept
124 error() const && noexcept
137 explicit unexpect_t() =
default;
144 inline constexpr unexpect_t unexpect{};
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;
154 template<
typename _Tp>
155 constexpr bool __is_unexpected =
false;
156 template<
typename _Tp>
157 constexpr bool __is_unexpected<unexpected<_Tp>> =
true;
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&&>>;
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>);
175 struct __in_place_inv { };
176 struct __unexpect_inv { };
180 template<
typename _Er>
183 static_assert( __expected::__can_be_unexpected<_Er> );
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>
195 noexcept(is_nothrow_constructible_v<_Er, _Err>)
196 : _M_unex(std::
forward<_Err>(__e))
199 template<
typename... _Args>
200 requires is_constructible_v<_Er, _Args...>
203 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
204 : _M_unex(std::
forward<_Args>(__args)...)
207 template<
typename _Up,
typename... _Args>
208 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
210 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
211 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
213 : _M_unex(__il, std::
forward<_Args>(__args)...)
222 error() const & noexcept {
return _M_unex; }
226 error() &
noexcept {
return _M_unex; }
229 constexpr const _Er&&
230 error() const && noexcept {
return std::move(_M_unex); }
234 error() &&
noexcept {
return std::move(_M_unex); }
237 swap(
unexpected& __other)
noexcept(is_nothrow_swappable_v<_Er>)
238 requires is_swappable_v<_Er>
241 swap(_M_unex, __other._M_unex);
244 template<
typename _Err>
246 friend constexpr bool
248 {
return __x._M_unex == __y.error(); }
250 friend constexpr void
252 requires is_swappable_v<_Er>
264 template<
typename _Tp>
267 static_assert( is_nothrow_move_constructible_v<_Tp> );
271 : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::
move(__x))
272 { std::destroy_at(_M_guarded); }
277 if (_M_guarded) [[unlikely]]
278 std::construct_at(_M_guarded,
std::move(_M_tmp));
281 _Guard(
const _Guard&) =
delete;
282 _Guard& operator=(
const _Guard&) =
delete;
287 _M_guarded =
nullptr;
297 template<
typename _Tp,
typename _Up,
typename _Vp>
299 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
300 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
302 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
304 std::destroy_at(__oldval);
307 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
310 std::destroy_at(__oldval);
311 std::construct_at(__newval,
std::move(__tmp));
315 _Guard<_Up> __guard(*__oldval);
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>>;
334 template<
typename _Tp,
typename _Er>
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> );
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>>
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>>
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>>
381 template<
typename _Up>
382 static constexpr bool __same_val
383 = is_same_v<typename _Up::value_type, _Tp>;
385 template<
typename _Up>
386 static constexpr bool __same_err
387 = is_same_v<typename _Up::error_type, _Er>;
390 using value_type = _Tp;
392 using unexpected_type = unexpected<_Er>;
394 template<
typename _Up>
395 using rebind = expected<_Up, error_type>;
399 noexcept(is_nothrow_default_constructible_v<_Tp>)
400 requires is_default_constructible_v<_Tp>
401 : _M_val(), _M_has_value(true)
404 expected(
const expected&) =
default;
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)
416 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
418 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
421 expected(expected&&) =
default;
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)
433 std::construct_at(__builtin_addressof(_M_val),
436 std::construct_at(__builtin_addressof(_M_unex),
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)
451 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
453 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
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)
467 std::construct_at(__builtin_addressof(_M_val),
470 std::construct_at(__builtin_addressof(_M_unex),
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>)
483 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
484 : _M_val(std::
forward<_Up>(__v)), _M_has_value(true)
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)
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)
503 template<
typename... _Args>
504 requires is_constructible_v<_Tp, _Args...>
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)
511 template<
typename _Up,
typename... _Args>
512 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
514 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
515 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
517 : _M_val(__il, std::
forward<_Args>(__args)...), _M_has_value(true)
520 template<
typename... _Args>
521 requires is_constructible_v<_Er, _Args...>
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)
528 template<
typename _Up,
typename... _Args>
529 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
531 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
532 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
534 : _M_unex(__il, std::
forward<_Args>(__args)...), _M_has_value(false)
537 constexpr ~expected() =
default;
539 constexpr ~expected()
540 requires (!is_trivially_destructible_v<_Tp>)
541 || (!is_trivially_destructible_v<_Er>)
544 std::destroy_at(__builtin_addressof(_M_val));
546 std::destroy_at(__builtin_addressof(_M_unex));
551 expected& operator=(
const expected&) =
delete;
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>)
564 if (__x._M_has_value)
565 this->_M_assign_val(__x._M_val);
567 this->_M_assign_unex(__x._M_unex);
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>)
582 if (__x._M_has_value)
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>)
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>)
610 operator=(
const unexpected<_Gr>& __e)
612 _M_assign_unex(__e.error());
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>)
623 operator=(unexpected<_Gr>&& __e)
631 template<
typename... _Args>
632 requires is_nothrow_constructible_v<_Tp, _Args...>
634 emplace(_Args&&... __args)
noexcept
637 std::destroy_at(__builtin_addressof(_M_val));
640 std::destroy_at(__builtin_addressof(_M_unex));
643 std::construct_at(__builtin_addressof(_M_val),
648 template<
typename _Up,
typename... _Args>
649 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
652 emplace(initializer_list<_Up> __il, _Args&&... __args)
noexcept
655 std::destroy_at(__builtin_addressof(_M_val));
658 std::destroy_at(__builtin_addressof(_M_unex));
661 std::construct_at(__builtin_addressof(_M_val),
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>)
681 if (__x._M_has_value)
684 swap(_M_val, __x._M_val);
687 this->_M_swap_val_unex(__x);
691 if (__x._M_has_value)
692 __x._M_swap_val_unex(*
this);
696 swap(_M_unex, __x._M_unex);
705 operator->() const noexcept
707 __glibcxx_assert(_M_has_value);
708 return __builtin_addressof(_M_val);
713 operator->() noexcept
715 __glibcxx_assert(_M_has_value);
716 return __builtin_addressof(_M_val);
723 __glibcxx_assert(_M_has_value);
731 __glibcxx_assert(_M_has_value);
736 constexpr const _Tp&&
739 __glibcxx_assert(_M_has_value);
747 __glibcxx_assert(_M_has_value);
753 operator bool() const noexcept {
return _M_has_value; }
756 constexpr bool has_value() const noexcept {
return _M_has_value; }
761 static_assert( is_copy_constructible_v<_Er> );
762 if (_M_has_value) [[likely]]
764 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
770 static_assert( is_copy_constructible_v<_Er> );
771 if (_M_has_value) [[likely]]
773 const auto& __unex = _M_unex;
774 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
777 constexpr const _Tp&&
780 static_assert( is_copy_constructible_v<_Er> );
781 static_assert( is_constructible_v<_Er, const _Er&&> );
782 if (_M_has_value) [[likely]]
784 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
790 static_assert( is_copy_constructible_v<_Er> );
791 static_assert( is_constructible_v<_Er, _Er&&> );
792 if (_M_has_value) [[likely]]
794 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
798 error() const & noexcept
800 __glibcxx_assert(!_M_has_value);
807 __glibcxx_assert(!_M_has_value);
811 constexpr const _Er&&
812 error() const && noexcept
814 __glibcxx_assert(!_M_has_value);
821 __glibcxx_assert(!_M_has_value);
825 template<
typename _Up = remove_cv_t<_Tp>>
827 value_or(_Up&& __v)
const &
828 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
829 is_nothrow_convertible<_Up, _Tp>>)
831 static_assert( is_copy_constructible_v<_Tp> );
832 static_assert( is_convertible_v<_Up, _Tp> );
839 template<
typename _Up = remove_cv_t<_Tp>>
841 value_or(_Up&& __v) &&
842 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
843 is_nothrow_convertible<_Up, _Tp>>)
845 static_assert( is_move_constructible_v<_Tp> );
846 static_assert( is_convertible_v<_Up, _Tp> );
853 template<
typename _Gr = _Er>
855 error_or(_Gr&& __e)
const&
857 static_assert( is_copy_constructible_v<_Er> );
858 static_assert( is_convertible_v<_Gr, _Er> );
865 template<
typename _Gr = _Er>
867 error_or(_Gr&& __e) &&
869 static_assert( is_move_constructible_v<_Er> );
870 static_assert( is_convertible_v<_Gr, _Er> );
879 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
881 and_then(_Fn&& __f) &
883 using _Up = __expected::__result<_Fn, _Tp&>;
884 static_assert(__expected::__is_expected<_Up>,
885 "the function passed to std::expected<T, E>::and_then "
886 "must return a std::expected");
887 static_assert(is_same_v<typename _Up::error_type, _Er>,
888 "the function passed to std::expected<T, E>::and_then "
889 "must return a std::expected with the same error_type");
894 return _Up(unexpect, _M_unex);
897 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
899 and_then(_Fn&& __f)
const &
901 using _Up = __expected::__result<_Fn, const _Tp&>;
902 static_assert(__expected::__is_expected<_Up>,
903 "the function passed to std::expected<T, E>::and_then "
904 "must return a std::expected");
905 static_assert(is_same_v<typename _Up::error_type, _Er>,
906 "the function passed to std::expected<T, E>::and_then "
907 "must return a std::expected with the same error_type");
912 return _Up(unexpect, _M_unex);
915 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
917 and_then(_Fn&& __f) &&
919 using _Up = __expected::__result<_Fn, _Tp&&>;
920 static_assert(__expected::__is_expected<_Up>,
921 "the function passed to std::expected<T, E>::and_then "
922 "must return a std::expected");
923 static_assert(is_same_v<typename _Up::error_type, _Er>,
924 "the function passed to std::expected<T, E>::and_then "
925 "must return a std::expected with the same error_type");
930 return _Up(unexpect,
std::move(_M_unex));
934 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
936 and_then(_Fn&& __f)
const &&
938 using _Up = __expected::__result<_Fn, const _Tp&&>;
939 static_assert(__expected::__is_expected<_Up>,
940 "the function passed to std::expected<T, E>::and_then "
941 "must return a std::expected");
942 static_assert(is_same_v<typename _Up::error_type, _Er>,
943 "the function passed to std::expected<T, E>::and_then "
944 "must return a std::expected with the same error_type");
949 return _Up(unexpect,
std::move(_M_unex));
952 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
956 using _Gr = __expected::__result<_Fn, _Er&>;
957 static_assert(__expected::__is_expected<_Gr>,
958 "the function passed to std::expected<T, E>::or_else "
959 "must return a std::expected");
960 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
961 "the function passed to std::expected<T, E>::or_else "
962 "must return a std::expected with the same value_type");
965 return _Gr(in_place, _M_val);
970 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
972 or_else(_Fn&& __f)
const &
974 using _Gr = __expected::__result<_Fn, const _Er&>;
975 static_assert(__expected::__is_expected<_Gr>,
976 "the function passed to std::expected<T, E>::or_else "
977 "must return a std::expected");
978 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
979 "the function passed to std::expected<T, E>::or_else "
980 "must return a std::expected with the same value_type");
983 return _Gr(in_place, _M_val);
989 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
991 or_else(_Fn&& __f) &&
993 using _Gr = __expected::__result<_Fn, _Er&&>;
994 static_assert(__expected::__is_expected<_Gr>,
995 "the function passed to std::expected<T, E>::or_else "
996 "must return a std::expected");
997 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
998 "the function passed to std::expected<T, E>::or_else "
999 "must return a std::expected with the same value_type");
1002 return _Gr(in_place,
std::move(_M_val));
1007 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1009 or_else(_Fn&& __f)
const &&
1011 using _Gr = __expected::__result<_Fn, const _Er&&>;
1012 static_assert(__expected::__is_expected<_Gr>,
1013 "the function passed to std::expected<T, E>::or_else "
1014 "must return a std::expected");
1015 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1016 "the function passed to std::expected<T, E>::or_else "
1017 "must return a std::expected with the same value_type");
1020 return _Gr(in_place,
std::move(_M_val));
1025 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1027 transform(_Fn&& __f) &
1029 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1030 using _Res = expected<_Up, _Er>;
1033 return _Res(__in_place_inv{}, [&]() {
1038 return _Res(unexpect, _M_unex);
1041 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1043 transform(_Fn&& __f)
const &
1045 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1046 using _Res = expected<_Up, _Er>;
1049 return _Res(__in_place_inv{}, [&]() {
1054 return _Res(unexpect, _M_unex);
1057 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1059 transform(_Fn&& __f) &&
1061 using _Up = __expected::__result_xform<_Fn, _Tp>;
1062 using _Res = expected<_Up, _Er>;
1065 return _Res(__in_place_inv{}, [&]() {
1070 return _Res(unexpect,
std::move(_M_unex));
1073 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1075 transform(_Fn&& __f)
const &&
1077 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1078 using _Res = expected<_Up, _Er>;
1081 return _Res(__in_place_inv{}, [&]() {
1086 return _Res(unexpect,
std::move(_M_unex));
1089 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
1091 transform_error(_Fn&& __f) &
1093 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1094 using _Res = expected<_Tp, _Gr>;
1097 return _Res(in_place, _M_val);
1099 return _Res(__unexpect_inv{}, [&]() {
1105 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
1107 transform_error(_Fn&& __f)
const &
1109 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1110 using _Res = expected<_Tp, _Gr>;
1113 return _Res(in_place, _M_val);
1115 return _Res(__unexpect_inv{}, [&]() {
1121 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
1123 transform_error(_Fn&& __f) &&
1125 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1126 using _Res = expected<_Tp, _Gr>;
1129 return _Res(in_place,
std::move(_M_val));
1131 return _Res(__unexpect_inv{}, [&]() {
1137 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1139 transform_error(_Fn&& __f)
const &&
1141 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1142 using _Res = expected<_Tp, _Gr>;
1145 return _Res(in_place,
std::move(_M_val));
1147 return _Res(__unexpect_inv{}, [&]() {
1155 template<
typename _Up,
typename _Er2>
1156 requires (!is_void_v<_Up>)
1157 &&
requires (
const _Tp& __t,
const _Up& __u,
1158 const _Er& __e,
const _Er2& __e2) {
1159 { __t == __u } -> convertible_to<bool>;
1160 { __e == __e2 } -> convertible_to<bool>;
1162 friend constexpr bool
1163 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1164 noexcept(
noexcept(bool(*__x == *__y))
1165 &&
noexcept(bool(__x.error() == __y.error())))
1167 if (__x.has_value())
1168 return __y.has_value() && bool(*__x == *__y);
1170 return !__y.has_value() && bool(__x.error() == __y.error());
1173 template<
typename _Up, same_as<_Tp> _Vp>
1174 requires (!__expected::__is_expected<_Up>)
1175 &&
requires (
const _Tp& __t,
const _Up& __u) {
1176 { __t == __u } -> convertible_to<bool>;
1178 friend constexpr bool
1179 operator==(
const expected<_Vp, _Er>& __x,
const _Up& __v)
1180 noexcept(
noexcept(bool(*__x == __v)))
1181 {
return __x.has_value() && bool(*__x == __v); }
1183 template<
typename _Er2>
1184 requires requires (
const _Er& __e,
const _Er2& __e2) {
1185 { __e == __e2 } -> convertible_to<bool>;
1187 friend constexpr bool
1188 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1189 noexcept(
noexcept(bool(__x.error() == __e.error())))
1190 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1192 friend constexpr void
1193 swap(expected& __x, expected& __y)
1194 noexcept(
noexcept(__x.swap(__y)))
1195 requires requires {__x.swap(__y);}
1199 template<
typename,
typename>
friend class expected;
1201 template<
typename _Vp>
1203 _M_assign_val(_Vp&& __v)
1209 __expected::__reinit(__builtin_addressof(_M_val),
1210 __builtin_addressof(_M_unex),
1212 _M_has_value =
true;
1216 template<
typename _Vp>
1218 _M_assign_unex(_Vp&& __v)
1222 __expected::__reinit(__builtin_addressof(_M_unex),
1223 __builtin_addressof(_M_val),
1225 _M_has_value =
false;
1234 _M_swap_val_unex(expected& __rhs)
1235 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1236 is_nothrow_move_constructible<_Tp>>)
1238 if constexpr (is_nothrow_move_constructible_v<_Er>)
1240 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1241 std::construct_at(__builtin_addressof(__rhs._M_val),
1243 __rhs._M_has_value =
true;
1244 std::destroy_at(__builtin_addressof(_M_val));
1245 std::construct_at(__builtin_addressof(_M_unex),
1247 _M_has_value =
false;
1251 __expected::_Guard<_Tp> __guard(_M_val);
1252 std::construct_at(__builtin_addressof(_M_unex),
1254 _M_has_value =
false;
1255 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1256 std::construct_at(__builtin_addressof(__rhs._M_val),
1258 __rhs._M_has_value =
true;
1262 using __in_place_inv = __expected::__in_place_inv;
1263 using __unexpect_inv = __expected::__unexpect_inv;
1265 template<
typename _Fn>
1267 expected(__in_place_inv, _Fn&& __fn)
1268 : _M_val(std::
forward<_Fn>(__fn)()), _M_has_value(true)
1271 template<
typename _Fn>
1273 expected(__unexpect_inv, _Fn&& __fn)
1274 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1278 remove_cv_t<_Tp> _M_val;
1286 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1287 class expected<_Tp, _Er>
1289 static_assert( __expected::__can_be_unexpected<_Er> );
1291 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1292 static constexpr bool __cons_from_expected
1293 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1294 is_constructible<_Unex, expected<_Up, _Err>>,
1295 is_constructible<_Unex, const expected<_Up, _Err>&>,
1296 is_constructible<_Unex, const expected<_Up, _Err>>
1299 template<
typename _Up>
1300 static constexpr bool __same_val
1301 = is_same_v<typename _Up::value_type, _Tp>;
1303 template<
typename _Up>
1304 static constexpr bool __same_err
1305 = is_same_v<typename _Up::error_type, _Er>;
1308 using value_type = _Tp;
1310 using unexpected_type = unexpected<_Er>;
1312 template<
typename _Up>
1313 using rebind = expected<_Up, error_type>;
1317 : _M_void(), _M_has_value(true)
1320 expected(
const expected&) =
default;
1323 expected(
const expected& __x)
1324 noexcept(is_nothrow_copy_constructible_v<_Er>)
1325 requires is_copy_constructible_v<_Er>
1326 && (!is_trivially_copy_constructible_v<_Er>)
1327 : _M_void(), _M_has_value(__x._M_has_value)
1330 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1333 expected(expected&&) =
default;
1336 expected(expected&& __x)
1337 noexcept(is_nothrow_move_constructible_v<_Er>)
1338 requires is_move_constructible_v<_Er>
1339 && (!is_trivially_move_constructible_v<_Er>)
1340 : _M_void(), _M_has_value(__x._M_has_value)
1343 std::construct_at(__builtin_addressof(_M_unex),
1347 template<
typename _Up,
typename _Gr>
1348 requires is_void_v<_Up>
1349 && is_constructible_v<_Er, const _Gr&>
1350 && (!__cons_from_expected<_Up, _Gr>)
1351 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1352 expected(
const expected<_Up, _Gr>& __x)
1353 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1354 : _M_void(), _M_has_value(__x._M_has_value)
1357 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1360 template<
typename _Up,
typename _Gr>
1361 requires is_void_v<_Up>
1362 && is_constructible_v<_Er, _Gr>
1363 && (!__cons_from_expected<_Up, _Gr>)
1364 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1365 expected(expected<_Up, _Gr>&& __x)
1366 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1367 : _M_void(), _M_has_value(__x._M_has_value)
1370 std::construct_at(__builtin_addressof(_M_unex),
1374 template<
typename _Gr = _Er>
1375 requires is_constructible_v<_Er, const _Gr&>
1376 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1377 expected(
const unexpected<_Gr>& __u)
1378 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1379 : _M_unex(__u.error()), _M_has_value(false)
1382 template<
typename _Gr = _Er>
1383 requires is_constructible_v<_Er, _Gr>
1384 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1385 expected(unexpected<_Gr>&& __u)
1386 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1387 : _M_unex(std::
move(__u).error()), _M_has_value(false)
1391 expected(in_place_t) noexcept
1395 template<
typename... _Args>
1396 requires is_constructible_v<_Er, _Args...>
1398 expected(unexpect_t, _Args&&... __args)
1399 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1400 : _M_unex(std::
forward<_Args>(__args)...), _M_has_value(false)
1403 template<
typename _Up,
typename... _Args>
1404 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1406 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1407 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1409 : _M_unex(__il, std::
forward<_Args>(__args)...), _M_has_value(false)
1412 constexpr ~expected() =
default;
1414 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1417 std::destroy_at(__builtin_addressof(_M_unex));
1422 expected& operator=(
const expected&) =
delete;
1425 operator=(
const expected& __x)
1426 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1427 is_nothrow_copy_assignable<_Er>>)
1428 requires is_copy_constructible_v<_Er>
1429 && is_copy_assignable_v<_Er>
1431 if (__x._M_has_value)
1434 _M_assign_unex(__x._M_unex);
1439 operator=(expected&& __x)
1440 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1441 is_nothrow_move_assignable<_Er>>)
1442 requires is_move_constructible_v<_Er>
1443 && is_move_assignable_v<_Er>
1445 if (__x._M_has_value)
1452 template<
typename _Gr>
1453 requires is_constructible_v<_Er, const _Gr&>
1454 && is_assignable_v<_Er&, const _Gr&>
1456 operator=(
const unexpected<_Gr>& __e)
1458 _M_assign_unex(__e.error());
1462 template<
typename _Gr>
1463 requires is_constructible_v<_Er, _Gr>
1464 && is_assignable_v<_Er&, _Gr>
1466 operator=(unexpected<_Gr>&& __e)
1479 std::destroy_at(__builtin_addressof(_M_unex));
1480 _M_has_value =
true;
1487 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1488 is_nothrow_move_constructible<_Er>>)
1489 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1493 if (!__x._M_has_value)
1495 std::construct_at(__builtin_addressof(_M_unex),
1497 std::destroy_at(__builtin_addressof(__x._M_unex));
1498 _M_has_value =
false;
1499 __x._M_has_value =
true;
1504 if (__x._M_has_value)
1506 std::construct_at(__builtin_addressof(__x._M_unex),
1508 std::destroy_at(__builtin_addressof(_M_unex));
1509 _M_has_value =
true;
1510 __x._M_has_value =
false;
1515 swap(_M_unex, __x._M_unex);
1524 operator bool() const noexcept {
return _M_has_value; }
1527 constexpr bool has_value() const noexcept {
return _M_has_value; }
1530 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1535 static_assert( is_copy_constructible_v<_Er> );
1536 if (_M_has_value) [[likely]]
1538 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1544 static_assert( is_copy_constructible_v<_Er> );
1545 static_assert( is_move_constructible_v<_Er> );
1546 if (_M_has_value) [[likely]]
1548 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1551 constexpr const _Er&
1552 error() const & noexcept
1554 __glibcxx_assert(!_M_has_value);
1561 __glibcxx_assert(!_M_has_value);
1565 constexpr const _Er&&
1566 error() const && noexcept
1568 __glibcxx_assert(!_M_has_value);
1575 __glibcxx_assert(!_M_has_value);
1579 template<
typename _Gr = _Er>
1581 error_or(_Gr&& __e)
const&
1583 static_assert( is_copy_constructible_v<_Er> );
1584 static_assert( is_convertible_v<_Gr, _Er> );
1591 template<
typename _Gr = _Er>
1593 error_or(_Gr&& __e) &&
1595 static_assert( is_move_constructible_v<_Er> );
1596 static_assert( is_convertible_v<_Gr, _Er> );
1605 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1607 and_then(_Fn&& __f) &
1609 using _Up = __expected::__result0<_Fn>;
1610 static_assert(__expected::__is_expected<_Up>);
1611 static_assert(is_same_v<typename _Up::error_type, _Er>);
1616 return _Up(unexpect, _M_unex);
1619 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1621 and_then(_Fn&& __f)
const &
1623 using _Up = __expected::__result0<_Fn>;
1624 static_assert(__expected::__is_expected<_Up>);
1625 static_assert(is_same_v<typename _Up::error_type, _Er>);
1630 return _Up(unexpect, _M_unex);
1633 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1635 and_then(_Fn&& __f) &&
1637 using _Up = __expected::__result0<_Fn>;
1638 static_assert(__expected::__is_expected<_Up>);
1639 static_assert(is_same_v<typename _Up::error_type, _Er>);
1644 return _Up(unexpect,
std::move(_M_unex));
1647 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1649 and_then(_Fn&& __f)
const &&
1651 using _Up = __expected::__result0<_Fn>;
1652 static_assert(__expected::__is_expected<_Up>);
1653 static_assert(is_same_v<typename _Up::error_type, _Er>);
1658 return _Up(unexpect,
std::move(_M_unex));
1661 template<
typename _Fn>
1663 or_else(_Fn&& __f) &
1665 using _Gr = __expected::__result<_Fn, _Er&>;
1666 static_assert(__expected::__is_expected<_Gr>);
1667 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1675 template<
typename _Fn>
1677 or_else(_Fn&& __f)
const &
1679 using _Gr = __expected::__result<_Fn, const _Er&>;
1680 static_assert(__expected::__is_expected<_Gr>);
1681 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1689 template<
typename _Fn>
1691 or_else(_Fn&& __f) &&
1693 using _Gr = __expected::__result<_Fn, _Er&&>;
1694 static_assert(__expected::__is_expected<_Gr>);
1695 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1703 template<
typename _Fn>
1705 or_else(_Fn&& __f)
const &&
1707 using _Gr = __expected::__result<_Fn, const _Er&&>;
1708 static_assert(__expected::__is_expected<_Gr>);
1709 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1717 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1719 transform(_Fn&& __f) &
1721 using _Up = __expected::__result0_xform<_Fn>;
1722 using _Res = expected<_Up, _Er>;
1727 return _Res(unexpect, _M_unex);
1730 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1732 transform(_Fn&& __f)
const &
1734 using _Up = __expected::__result0_xform<_Fn>;
1735 using _Res = expected<_Up, _Er>;
1740 return _Res(unexpect, _M_unex);
1743 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1745 transform(_Fn&& __f) &&
1747 using _Up = __expected::__result0_xform<_Fn>;
1748 using _Res = expected<_Up, _Er>;
1753 return _Res(unexpect,
std::move(_M_unex));
1756 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1758 transform(_Fn&& __f)
const &&
1760 using _Up = __expected::__result0_xform<_Fn>;
1761 using _Res = expected<_Up, _Er>;
1766 return _Res(unexpect,
std::move(_M_unex));
1769 template<
typename _Fn>
1771 transform_error(_Fn&& __f) &
1773 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1774 using _Res = expected<_Tp, _Gr>;
1779 return _Res(__unexpect_inv{}, [&]() {
1785 template<
typename _Fn>
1787 transform_error(_Fn&& __f)
const &
1789 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1790 using _Res = expected<_Tp, _Gr>;
1795 return _Res(__unexpect_inv{}, [&]() {
1801 template<
typename _Fn>
1803 transform_error(_Fn&& __f) &&
1805 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1806 using _Res = expected<_Tp, _Gr>;
1811 return _Res(__unexpect_inv{}, [&]() {
1817 template<
typename _Fn>
1819 transform_error(_Fn&& __f)
const &&
1821 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1822 using _Res = expected<_Tp, _Gr>;
1827 return _Res(__unexpect_inv{}, [&]() {
1835 template<
typename _Up,
typename _Er2>
1836 requires is_void_v<_Up>
1837 &&
requires (
const _Er& __e,
const _Er2& __e2) {
1838 { __e == __e2 } -> convertible_to<bool>;
1840 friend constexpr bool
1841 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1842 noexcept(
noexcept(bool(__x.error() == __y.error())))
1844 if (__x.has_value())
1845 return __y.has_value();
1847 return !__y.has_value() && bool(__x.error() == __y.error());
1850 template<
typename _Er2>
1851 requires requires (
const _Er& __e,
const _Er2& __e2) {
1852 { __e == __e2 } -> convertible_to<bool>;
1854 friend constexpr bool
1855 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1856 noexcept(
noexcept(bool(__x.error() == __e.error())))
1857 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1859 friend constexpr void
1860 swap(expected& __x, expected& __y)
1861 noexcept(
noexcept(__x.swap(__y)))
1862 requires requires { __x.swap(__y); }
1866 template<
typename,
typename>
friend class expected;
1868 template<
typename _Vp>
1870 _M_assign_unex(_Vp&& __v)
1874 std::construct_at(__builtin_addressof(_M_unex),
1876 _M_has_value =
false;
1882 using __in_place_inv = __expected::__in_place_inv;
1883 using __unexpect_inv = __expected::__unexpect_inv;
1885 template<
typename _Fn>
1887 expected(__in_place_inv, _Fn&& __fn)
1888 : _M_void(), _M_has_value(true)
1891 template<
typename _Fn>
1893 expected(__unexpect_inv, _Fn&& __fn)
1894 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1906_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.
Base class for all library exceptions.