29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
32#pragma GCC system_header
34#if __cplusplus > 202002L && __cpp_concepts >= 202002L
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
53#define __cpp_lib_expected 202211L
59 template<
typename _Tp,
typename _Er>
66 template<
typename _Er>
73 template<
typename _Er>
74 class bad_expected_access;
77 class bad_expected_access<void> :
public exception
80 bad_expected_access() noexcept { }
81 bad_expected_access(
const bad_expected_access&) =
default;
82 bad_expected_access(bad_expected_access&&) =
default;
83 bad_expected_access& operator=(
const bad_expected_access&) =
default;
84 bad_expected_access& operator=(bad_expected_access&&) =
default;
85 ~bad_expected_access() =
default;
91 what() const noexcept
override
92 {
return "bad access to std::expected without expected value"; }
95 template<
typename _Er>
96 class bad_expected_access :
public bad_expected_access<void> {
99 bad_expected_access(_Er __e) : _M_unex(
std::
move(__e)) { }
110 error() const & noexcept
120 error() const && noexcept
133 explicit unexpect_t() =
default;
140 inline constexpr unexpect_t unexpect{};
145 template<
typename _Tp>
146 constexpr bool __is_expected =
false;
147 template<
typename _Tp,
typename _Er>
148 constexpr bool __is_expected<expected<_Tp, _Er>> =
true;
150 template<
typename _Tp>
151 constexpr bool __is_unexpected =
false;
152 template<
typename _Tp>
153 constexpr bool __is_unexpected<unexpected<_Tp>> =
true;
155 template<
typename _Fn,
typename _Tp>
156 using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
157 template<
typename _Fn>
158 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
160 template<
typename _Er>
161 concept __can_be_unexpected
162 = is_object_v<_Er> && (!is_array_v<_Er>)
163 && (!__expected::__is_unexpected<_Er>)
164 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
167 struct __in_place_inv { };
168 struct __unexpect_inv { };
172 template<
typename _Er>
175 static_assert( __expected::__can_be_unexpected<_Er> );
181 template<
typename _Err = _Er>
182 requires (!is_same_v<remove_cvref_t<_Err>,
unexpected>)
183 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
184 && is_constructible_v<_Er, _Err>
187 noexcept(is_nothrow_constructible_v<_Er, _Err>)
188 : _M_unex(std::forward<_Err>(__e))
191 template<
typename... _Args>
192 requires is_constructible_v<_Er, _Args...>
195 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
196 : _M_unex(std::forward<_Args>(__args)...)
199 template<
typename _Up,
typename... _Args>
200 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
202 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
203 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
205 : _M_unex(__il, std::forward<_Args>(__args)...)
214 error() const & noexcept {
return _M_unex; }
218 error() &
noexcept {
return _M_unex; }
221 constexpr const _Er&&
222 error() const && noexcept {
return std::move(_M_unex); }
226 error() &&
noexcept {
return std::move(_M_unex); }
230 requires is_swappable_v<_Er>
233 swap(_M_unex, __other._M_unex);
236 template<
typename _Err>
238 friend constexpr bool
239 operator==(
const unexpected& __x,
const unexpected<_Err>& __y)
240 {
return __x._M_unex == __y.error(); }
242 friend constexpr void
244 requires is_swappable_v<_Er>
251 template<
typename _Er>
unexpected(_Er) -> unexpected<_Er>;
256 template<
typename _Tp>
259 static_assert( is_nothrow_move_constructible_v<_Tp> );
263 : _M_guarded(__builtin_addressof(__x)), _M_tmp(
std::
move(__x))
264 { std::destroy_at(_M_guarded); }
269 if (_M_guarded) [[unlikely]]
270 std::construct_at(_M_guarded,
std::move(_M_tmp));
273 _Guard(
const _Guard&) =
delete;
274 _Guard& operator=(
const _Guard&) =
delete;
279 _M_guarded =
nullptr;
289 template<
typename _Tp,
typename _Up,
typename _Vp>
291 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
292 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
294 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
296 std::destroy_at(__oldval);
297 std::construct_at(__newval, std::forward<_Vp>(__arg));
299 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
301 _Tp __tmp(std::forward<_Vp>(__arg));
302 std::destroy_at(__oldval);
303 std::construct_at(__newval,
std::move(__tmp));
307 _Guard<_Up> __guard(*__oldval);
308 std::construct_at(__newval, std::forward<_Vp>(__arg));
315 template<
typename _Tp,
typename _Er>
318 static_assert( ! is_reference_v<_Tp> );
319 static_assert( ! is_function_v<_Tp> );
320 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
321 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
322 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
323 static_assert( __expected::__can_be_unexpected<_Er> );
325 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
326 static constexpr bool __cons_from_expected
327 = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
328 is_constructible<_Tp, expected<_Up, _Err>>,
329 is_constructible<_Tp, const expected<_Up, _Err>&>,
330 is_constructible<_Tp, const expected<_Up, _Err>>,
331 is_convertible<expected<_Up, _Err>&, _Tp>,
332 is_convertible<expected<_Up, _Err>, _Tp>,
333 is_convertible<const expected<_Up, _Err>&, _Tp>,
334 is_convertible<const expected<_Up, _Err>, _Tp>,
335 is_constructible<_Unex, expected<_Up, _Err>&>,
336 is_constructible<_Unex, expected<_Up, _Err>>,
337 is_constructible<_Unex, const expected<_Up, _Err>&>,
338 is_constructible<_Unex, const expected<_Up, _Err>>
341 template<
typename _Up,
typename _Err>
342 constexpr static bool __explicit_conv
343 = __or_v<__not_<is_convertible<_Up, _Tp>>,
344 __not_<is_convertible<_Err, _Er>>
347 template<
typename _Up>
348 static constexpr bool __same_val
349 = is_same_v<typename _Up::value_type, _Tp>;
351 template<
typename _Up>
352 static constexpr bool __same_err
353 = is_same_v<typename _Up::error_type, _Er>;
356 using value_type = _Tp;
358 using unexpected_type = unexpected<_Er>;
360 template<
typename _Up>
361 using rebind = expected<_Up, error_type>;
365 noexcept(is_nothrow_default_constructible_v<_Tp>)
366 requires is_default_constructible_v<_Tp>
367 : _M_val(), _M_has_value(true)
370 expected(
const expected&) =
default;
373 expected(
const expected& __x)
374 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
375 is_nothrow_copy_constructible<_Er>>)
376 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
377 && (!is_trivially_copy_constructible_v<_Tp>
378 || !is_trivially_copy_constructible_v<_Er>)
379 : _M_has_value(__x._M_has_value)
382 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
384 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
387 expected(expected&&) =
default;
390 expected(expected&& __x)
391 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
392 is_nothrow_move_constructible<_Er>>)
393 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
394 && (!is_trivially_move_constructible_v<_Tp>
395 || !is_trivially_move_constructible_v<_Er>)
396 : _M_has_value(__x._M_has_value)
399 std::construct_at(__builtin_addressof(_M_val),
402 std::construct_at(__builtin_addressof(_M_unex),
406 template<
typename _Up,
typename _Gr>
407 requires is_constructible_v<_Tp, const _Up&>
408 && is_constructible_v<_Er, const _Gr&>
409 && (!__cons_from_expected<_Up, _Gr>)
410 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
411 expected(
const expected<_Up, _Gr>& __x)
412 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
413 is_nothrow_constructible<_Er, const _Gr&>>)
414 : _M_has_value(__x._M_has_value)
417 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
419 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
422 template<
typename _Up,
typename _Gr>
423 requires is_constructible_v<_Tp, _Up>
424 && is_constructible_v<_Er, _Gr>
425 && (!__cons_from_expected<_Up, _Gr>)
426 constexpr explicit(__explicit_conv<_Up, _Gr>)
427 expected(expected<_Up, _Gr>&& __x)
428 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
429 is_nothrow_constructible<_Er, _Gr>>)
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 = _Tp>
441 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
442 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
443 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
444 && is_constructible_v<_Tp, _Up>
445 constexpr explicit(!is_convertible_v<_Up, _Tp>)
447 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
448 : _M_val(std::forward<_Up>(__v)), _M_has_value(
true)
451 template<
typename _Gr = _Er>
452 requires is_constructible_v<_Er, const _Gr&>
453 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
454 expected(
const unexpected<_Gr>& __u)
455 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
456 : _M_unex(__u.error()), _M_has_value(
false)
459 template<
typename _Gr = _Er>
460 requires is_constructible_v<_Er, _Gr>
461 constexpr explicit(!is_convertible_v<_Gr, _Er>)
462 expected(unexpected<_Gr>&& __u)
463 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
464 : _M_unex(
std::move(__u).error()), _M_has_value(
false)
467 template<
typename... _Args>
468 requires is_constructible_v<_Tp, _Args...>
470 expected(in_place_t, _Args&&... __args)
471 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
472 : _M_val(std::forward<_Args>(__args)...), _M_has_value(
true)
475 template<
typename _Up,
typename... _Args>
476 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
478 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
479 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
481 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(
true)
484 template<
typename... _Args>
485 requires is_constructible_v<_Er, _Args...>
487 expected(unexpect_t, _Args&&... __args)
488 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
489 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(
false)
492 template<
typename _Up,
typename... _Args>
493 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
495 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
496 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
498 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(
false)
501 constexpr ~expected() =
default;
503 constexpr ~expected()
504 requires (!is_trivially_destructible_v<_Tp>)
505 || (!is_trivially_destructible_v<_Er>)
508 std::destroy_at(__builtin_addressof(_M_val));
510 std::destroy_at(__builtin_addressof(_M_unex));
515 expected& operator=(
const expected&) =
delete;
518 operator=(
const expected& __x)
519 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
520 is_nothrow_copy_constructible<_Er>,
521 is_nothrow_copy_assignable<_Tp>,
522 is_nothrow_copy_assignable<_Er>>)
523 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
524 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
525 && (is_nothrow_move_constructible_v<_Tp>
526 || is_nothrow_move_constructible_v<_Er>)
528 if (__x._M_has_value)
529 this->_M_assign_val(__x._M_val);
531 this->_M_assign_unex(__x._M_unex);
536 operator=(expected&& __x)
537 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
538 is_nothrow_move_constructible<_Er>,
539 is_nothrow_move_assignable<_Tp>,
540 is_nothrow_move_assignable<_Er>>)
541 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
542 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
543 && (is_nothrow_move_constructible_v<_Tp>
544 || is_nothrow_move_constructible_v<_Er>)
546 if (__x._M_has_value)
553 template<
typename _Up = _Tp>
554 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
555 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
556 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
557 && (is_nothrow_constructible_v<_Tp, _Up>
558 || is_nothrow_move_constructible_v<_Tp>
559 || is_nothrow_move_constructible_v<_Er>)
563 _M_assign_val(std::forward<_Up>(__v));
567 template<
typename _Gr>
568 requires is_constructible_v<_Er, const _Gr&>
569 && is_assignable_v<_Er&, const _Gr&>
570 && (is_nothrow_constructible_v<_Er, const _Gr&>
571 || is_nothrow_move_constructible_v<_Tp>
572 || is_nothrow_move_constructible_v<_Er>)
574 operator=(
const unexpected<_Gr>& __e)
576 _M_assign_unex(__e.error());
580 template<
typename _Gr>
581 requires is_constructible_v<_Er, _Gr>
582 && is_assignable_v<_Er&, _Gr>
583 && (is_nothrow_constructible_v<_Er, _Gr>
584 || is_nothrow_move_constructible_v<_Tp>
585 || is_nothrow_move_constructible_v<_Er>)
587 operator=(unexpected<_Gr>&& __e)
595 template<
typename... _Args>
596 requires is_nothrow_constructible_v<_Tp, _Args...>
598 emplace(_Args&&... __args)
noexcept
601 std::destroy_at(__builtin_addressof(_M_val));
604 std::destroy_at(__builtin_addressof(_M_unex));
607 std::construct_at(__builtin_addressof(_M_val),
608 std::forward<_Args>(__args)...);
612 template<
typename _Up,
typename... _Args>
613 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
616 emplace(initializer_list<_Up> __il, _Args&&... __args)
noexcept
619 std::destroy_at(__builtin_addressof(_M_val));
622 std::destroy_at(__builtin_addressof(_M_unex));
625 std::construct_at(__builtin_addressof(_M_val),
626 __il, std::forward<_Args>(__args)...);
633 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
634 is_nothrow_move_constructible<_Er>,
635 is_nothrow_swappable<_Tp&>,
636 is_nothrow_swappable<_Er&>>)
637 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
638 && is_move_constructible_v<_Tp>
639 && is_move_constructible_v<_Er>
640 && (is_nothrow_move_constructible_v<_Tp>
641 || is_nothrow_move_constructible_v<_Er>)
645 if (__x._M_has_value)
648 swap(_M_val, __x._M_val);
651 this->_M_swap_val_unex(__x);
655 if (__x._M_has_value)
656 __x._M_swap_val_unex(*
this);
660 swap(_M_unex, __x._M_unex);
669 operator->() const noexcept
671 __glibcxx_assert(_M_has_value);
672 return __builtin_addressof(_M_val);
677 operator->() noexcept
679 __glibcxx_assert(_M_has_value);
680 return __builtin_addressof(_M_val);
687 __glibcxx_assert(_M_has_value);
695 __glibcxx_assert(_M_has_value);
700 constexpr const _Tp&&
703 __glibcxx_assert(_M_has_value);
711 __glibcxx_assert(_M_has_value);
717 operator bool() const noexcept {
return _M_has_value; }
720 constexpr bool has_value() const noexcept {
return _M_has_value; }
725 if (_M_has_value) [[likely]]
727 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
733 if (_M_has_value) [[likely]]
735 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
738 constexpr const _Tp&&
741 if (_M_has_value) [[likely]]
743 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
750 if (_M_has_value) [[likely]]
752 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
757 error() const & noexcept
759 __glibcxx_assert(!_M_has_value);
766 __glibcxx_assert(!_M_has_value);
770 constexpr const _Er&&
771 error() const && noexcept
773 __glibcxx_assert(!_M_has_value);
780 __glibcxx_assert(!_M_has_value);
784 template<
typename _Up>
786 value_or(_Up&& __v)
const &
787 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
788 is_nothrow_convertible<_Up, _Tp>>)
790 static_assert( is_copy_constructible_v<_Tp> );
791 static_assert( is_convertible_v<_Up, _Tp> );
795 return static_cast<_Tp
>(std::forward<_Up>(__v));
798 template<
typename _Up>
800 value_or(_Up&& __v) &&
801 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
802 is_nothrow_convertible<_Up, _Tp>>)
804 static_assert( is_move_constructible_v<_Tp> );
805 static_assert( is_convertible_v<_Up, _Tp> );
809 return static_cast<_Tp
>(std::forward<_Up>(__v));
812 template<
typename _Gr = _Er>
814 error_or(_Gr&& __e)
const&
816 static_assert( is_copy_constructible_v<_Er> );
817 static_assert( is_convertible_v<_Gr, _Er> );
820 return std::forward<_Gr>(__e);
824 template<
typename _Gr = _Er>
826 error_or(_Gr&& __e) &&
828 static_assert( is_move_constructible_v<_Er> );
829 static_assert( is_convertible_v<_Gr, _Er> );
832 return std::forward<_Gr>(__e);
838 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
840 and_then(_Fn&& __f) &
842 using _Up = __expected::__result<_Fn, _Tp&>;
843 static_assert(__expected::__is_expected<_Up>);
844 static_assert(is_same_v<typename _Up::error_type, _Er>);
849 return _Up(unexpect, error());
852 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
854 and_then(_Fn&& __f)
const &
856 using _Up = __expected::__result<_Fn, const _Tp&>;
857 static_assert(__expected::__is_expected<_Up>);
858 static_assert(is_same_v<typename _Up::error_type, _Er>);
863 return _Up(unexpect, error());
866 template<
typename _Fn>
requires is_move_constructible_v<_Er>
868 and_then(_Fn&& __f) &&
870 using _Up = __expected::__result<_Fn, _Tp&&>;
871 static_assert(__expected::__is_expected<_Up>);
872 static_assert(is_same_v<typename _Up::error_type, _Er>);
877 return _Up(unexpect,
std::move(error()));
881 template<
typename _Fn>
requires is_move_constructible_v<_Er>
883 and_then(_Fn&& __f)
const &&
885 using _Up = __expected::__result<_Fn, const _Tp&&>;
886 static_assert(__expected::__is_expected<_Up>);
887 static_assert(is_same_v<typename _Up::error_type, _Er>);
892 return _Up(unexpect,
std::move(error()));
895 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
899 using _Gr = __expected::__result<_Fn, _Er&>;
900 static_assert(__expected::__is_expected<_Gr>);
901 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
904 return _Gr(in_place, value());
909 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
911 or_else(_Fn&& __f)
const &
913 using _Gr = __expected::__result<_Fn, const _Er&>;
914 static_assert(__expected::__is_expected<_Gr>);
915 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
918 return _Gr(in_place, value());
924 template<
typename _Fn>
requires is_move_constructible_v<_Er>
926 or_else(_Fn&& __f) &&
928 using _Gr = __expected::__result<_Fn, _Er&&>;
929 static_assert(__expected::__is_expected<_Gr>);
930 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
933 return _Gr(in_place,
std::move(value()));
938 template<
typename _Fn>
requires is_move_constructible_v<_Er>
940 or_else(_Fn&& __f)
const &&
942 using _Gr = __expected::__result<_Fn, const _Er&&>;
943 static_assert(__expected::__is_expected<_Gr>);
944 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
947 return _Gr(in_place,
std::move(value()));
952 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
954 transform(_Fn&& __f) &
956 using _Up = __expected::__result<_Fn, _Tp&>;
957 using _Res = expected<_Up, _Er>;
960 return _Res(__in_place_inv{}, [&]() {
965 return _Res(unexpect,
std::move(error()));
968 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
970 transform(_Fn&& __f)
const &
972 using _Up = __expected::__result<_Fn, const _Tp&>;
973 using _Res = expected<_Up, _Er>;
976 return _Res(__in_place_inv{}, [&]() {
981 return _Res(unexpect,
std::move(error()));
984 template<
typename _Fn>
requires is_move_constructible_v<_Er>
986 transform(_Fn&& __f) &&
988 using _Up = __expected::__result<_Fn, _Tp>;
989 using _Res = expected<_Up, _Er>;
992 return _Res(__in_place_inv{}, [&]() {
997 return _Res(unexpect,
std::move(error()));
1000 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1002 transform(_Fn&& __f)
const &&
1004 using _Up = __expected::__result<_Fn, const _Tp>;
1005 using _Res = expected<_Up, _Er>;
1008 return _Res(__in_place_inv{}, [&]() {
1013 return _Res(unexpect,
std::move(error()));
1016 template<
typename _Fn>
requires is_copy_constructible_v<_Tp>
1018 transform_error(_Fn&& __f) &
1020 using _Gr = __expected::__result<_Fn, _Er&>;
1021 using _Res = expected<_Tp, _Gr>;
1024 return _Res(in_place, value());
1026 return _Res(__unexpect_inv{}, [&]() {
1032 template<
typename _Fn>
requires is_copy_constructible_v<_Tp>
1034 transform_error(_Fn&& __f)
const &
1036 using _Gr = __expected::__result<_Fn, const _Er&>;
1037 using _Res = expected<_Tp, _Gr>;
1040 return _Res(in_place, value());
1042 return _Res(__unexpect_inv{}, [&]() {
1048 template<
typename _Fn>
requires is_move_constructible_v<_Tp>
1050 transform_error(_Fn&& __f) &&
1052 using _Gr = __expected::__result<_Fn, _Er&&>;
1053 using _Res = expected<_Tp, _Gr>;
1056 return _Res(in_place,
std::move(value()));
1058 return _Res(__unexpect_inv{}, [&]() {
1064 template<
typename _Fn>
requires is_move_constructible_v<_Tp>
1066 transform_error(_Fn&& __f)
const &&
1068 using _Gr = __expected::__result<_Fn, const _Er&&>;
1069 using _Res = expected<_Tp, _Gr>;
1072 return _Res(in_place,
std::move(value()));
1074 return _Res(__unexpect_inv{}, [&]() {
1082 template<
typename _Up,
typename _Er2>
1083 requires (!is_void_v<_Up>)
1084 friend constexpr bool
1085 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1089 if (__x.has_value())
1090 return __y.has_value() && bool(*__x == *__y);
1092 return !__y.has_value() && bool(__x.error() == __y.error());
1095 template<
typename _Up>
1096 friend constexpr bool
1097 operator==(
const expected& __x,
const _Up& __v)
1099 {
return __x.has_value() && bool(*__x == __v); }
1101 template<
typename _Er2>
1102 friend constexpr bool
1103 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1105 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1107 friend constexpr void
1108 swap(expected& __x, expected& __y)
1109 noexcept(
noexcept(__x.swap(__y)))
1110 requires requires {__x.swap(__y);}
1114 template<
typename,
typename>
friend class expected;
1116 template<
typename _Vp>
1118 _M_assign_val(_Vp&& __v)
1121 _M_val = std::forward<_Vp>(__v);
1124 __expected::__reinit(__builtin_addressof(_M_val),
1125 __builtin_addressof(_M_unex),
1126 std::forward<_Vp>(__v));
1127 _M_has_value =
true;
1131 template<
typename _Vp>
1133 _M_assign_unex(_Vp&& __v)
1137 __expected::__reinit(__builtin_addressof(_M_unex),
1138 __builtin_addressof(_M_val),
1139 std::forward<_Vp>(__v));
1140 _M_has_value =
false;
1143 _M_unex = std::forward<_Vp>(__v);
1149 _M_swap_val_unex(expected& __rhs)
1150 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1151 is_nothrow_move_constructible<_Tp>>)
1153 if constexpr (is_nothrow_move_constructible_v<_Er>)
1155 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1156 std::construct_at(__builtin_addressof(__rhs._M_val),
1158 __rhs._M_has_value =
true;
1159 std::destroy_at(__builtin_addressof(_M_val));
1160 std::construct_at(__builtin_addressof(_M_unex),
1162 _M_has_value =
false;
1166 __expected::_Guard<_Tp> __guard(_M_val);
1167 std::construct_at(__builtin_addressof(_M_unex),
1169 _M_has_value =
false;
1170 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1171 std::construct_at(__builtin_addressof(__rhs._M_val),
1173 __rhs._M_has_value =
true;
1177 using __in_place_inv = __expected::__in_place_inv;
1178 using __unexpect_inv = __expected::__unexpect_inv;
1180 template<
typename _Fn>
1182 expected(__in_place_inv, _Fn&& __fn)
1183 : _M_val(
std::
forward<_Fn>(__fn)()), _M_has_value(true)
1186 template<
typename _Fn>
1188 expected(__unexpect_inv, _Fn&& __fn)
1189 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1201 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1202 class expected<_Tp, _Er>
1204 static_assert( __expected::__can_be_unexpected<_Er> );
1206 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1207 static constexpr bool __cons_from_expected
1208 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1209 is_constructible<_Unex, expected<_Up, _Err>>,
1210 is_constructible<_Unex, const expected<_Up, _Err>&>,
1211 is_constructible<_Unex, const expected<_Up, _Err>>
1214 template<
typename _Up>
1215 static constexpr bool __same_val
1216 = is_same_v<typename _Up::value_type, _Tp>;
1218 template<
typename _Up>
1219 static constexpr bool __same_err
1220 = is_same_v<typename _Up::error_type, _Er>;
1223 using value_type = _Tp;
1225 using unexpected_type = unexpected<_Er>;
1227 template<
typename _Up>
1228 using rebind = expected<_Up, error_type>;
1232 : _M_void(), _M_has_value(true)
1235 expected(
const expected&) =
default;
1238 expected(
const expected& __x)
1239 noexcept(is_nothrow_copy_constructible_v<_Er>)
1240 requires is_copy_constructible_v<_Er>
1241 && (!is_trivially_copy_constructible_v<_Er>)
1242 : _M_void(), _M_has_value(__x._M_has_value)
1245 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1248 expected(expected&&) =
default;
1251 expected(expected&& __x)
1252 noexcept(is_nothrow_move_constructible_v<_Er>)
1253 requires is_move_constructible_v<_Er>
1254 && (!is_trivially_move_constructible_v<_Er>)
1255 : _M_void(), _M_has_value(__x._M_has_value)
1258 std::construct_at(__builtin_addressof(_M_unex),
1262 template<
typename _Up,
typename _Gr>
1263 requires is_void_v<_Up>
1264 && is_constructible_v<_Er, const _Gr&>
1265 && (!__cons_from_expected<_Up, _Gr>)
1266 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1267 expected(
const expected<_Up, _Gr>& __x)
1268 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1269 : _M_void(), _M_has_value(__x._M_has_value)
1272 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1275 template<
typename _Up,
typename _Gr>
1276 requires is_void_v<_Up>
1277 && is_constructible_v<_Er, _Gr>
1278 && (!__cons_from_expected<_Up, _Gr>)
1279 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1280 expected(expected<_Up, _Gr>&& __x)
1281 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1282 : _M_void(), _M_has_value(__x._M_has_value)
1285 std::construct_at(__builtin_addressof(_M_unex),
1289 template<
typename _Gr = _Er>
1290 requires is_constructible_v<_Er, const _Gr&>
1291 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1292 expected(
const unexpected<_Gr>& __u)
1293 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1294 : _M_unex(__u.error()), _M_has_value(
false)
1297 template<
typename _Gr = _Er>
1298 requires is_constructible_v<_Er, _Gr>
1299 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1300 expected(unexpected<_Gr>&& __u)
1301 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1302 : _M_unex(
std::move(__u).error()), _M_has_value(
false)
1305 template<
typename... _Args>
1307 expected(in_place_t) noexcept
1311 template<
typename... _Args>
1312 requires is_constructible_v<_Er, _Args...>
1314 expected(unexpect_t, _Args&&... __args)
1315 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1316 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(
false)
1319 template<
typename _Up,
typename... _Args>
1320 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1322 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1323 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1325 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(
false)
1328 constexpr ~expected() =
default;
1330 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1333 std::destroy_at(__builtin_addressof(_M_unex));
1338 expected& operator=(
const expected&) =
delete;
1341 operator=(
const expected& __x)
1342 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1343 is_nothrow_copy_assignable<_Er>>)
1344 requires is_copy_constructible_v<_Er>
1345 && is_copy_assignable_v<_Er>
1347 if (__x._M_has_value)
1350 _M_assign_unex(__x._M_unex);
1355 operator=(expected&& __x)
1356 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1357 is_nothrow_move_assignable<_Er>>)
1358 requires is_move_constructible_v<_Er>
1359 && is_move_assignable_v<_Er>
1361 if (__x._M_has_value)
1368 template<
typename _Gr>
1369 requires is_constructible_v<_Er, const _Gr&>
1370 && is_assignable_v<_Er&, const _Gr&>
1372 operator=(
const unexpected<_Gr>& __e)
1374 _M_assign_unex(__e.error());
1378 template<
typename _Gr>
1379 requires is_constructible_v<_Er, _Gr>
1380 && is_assignable_v<_Er&, _Gr>
1382 operator=(unexpected<_Gr>&& __e)
1395 std::destroy_at(__builtin_addressof(_M_unex));
1396 _M_has_value =
true;
1403 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1404 is_nothrow_move_constructible<_Er>>)
1405 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1409 if (!__x._M_has_value)
1411 std::construct_at(__builtin_addressof(_M_unex),
1413 std::destroy_at(__builtin_addressof(__x._M_unex));
1414 _M_has_value =
false;
1415 __x._M_has_value =
true;
1420 if (__x._M_has_value)
1422 std::construct_at(__builtin_addressof(__x._M_unex),
1424 std::destroy_at(__builtin_addressof(_M_unex));
1425 _M_has_value =
true;
1426 __x._M_has_value =
false;
1431 swap(_M_unex, __x._M_unex);
1440 operator bool() const noexcept {
return _M_has_value; }
1443 constexpr bool has_value() const noexcept {
return _M_has_value; }
1446 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1451 if (_M_has_value) [[likely]]
1453 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1459 if (_M_has_value) [[likely]]
1461 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1464 constexpr const _Er&
1465 error() const & noexcept
1467 __glibcxx_assert(!_M_has_value);
1474 __glibcxx_assert(!_M_has_value);
1478 constexpr const _Er&&
1479 error() const && noexcept
1481 __glibcxx_assert(!_M_has_value);
1488 __glibcxx_assert(!_M_has_value);
1492 template<
typename _Gr = _Er>
1494 error_or(_Gr&& __e)
const&
1496 static_assert( is_copy_constructible_v<_Er> );
1497 static_assert( is_convertible_v<_Gr, _Er> );
1500 return std::forward<_Gr>(__e);
1504 template<
typename _Gr = _Er>
1506 error_or(_Gr&& __e) &&
1508 static_assert( is_move_constructible_v<_Er> );
1509 static_assert( is_convertible_v<_Gr, _Er> );
1512 return std::forward<_Gr>(__e);
1518 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1520 and_then(_Fn&& __f) &
1522 using _Up = __expected::__result0<_Fn>;
1523 static_assert(__expected::__is_expected<_Up>);
1524 static_assert(is_same_v<typename _Up::error_type, _Er>);
1529 return _Up(unexpect, error());
1532 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1534 and_then(_Fn&& __f)
const &
1536 using _Up = __expected::__result0<_Fn>;
1537 static_assert(__expected::__is_expected<_Up>);
1538 static_assert(is_same_v<typename _Up::error_type, _Er>);
1543 return _Up(unexpect, error());
1546 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1548 and_then(_Fn&& __f) &&
1550 using _Up = __expected::__result0<_Fn>;
1551 static_assert(__expected::__is_expected<_Up>);
1552 static_assert(is_same_v<typename _Up::error_type, _Er>);
1557 return _Up(unexpect,
std::move(error()));
1560 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1562 and_then(_Fn&& __f)
const &&
1564 using _Up = __expected::__result0<_Fn>;
1565 static_assert(__expected::__is_expected<_Up>);
1566 static_assert(is_same_v<typename _Up::error_type, _Er>);
1571 return _Up(unexpect,
std::move(error()));
1574 template<
typename _Fn>
1576 or_else(_Fn&& __f) &
1578 using _Gr = __expected::__result<_Fn, _Er&>;
1579 static_assert(__expected::__is_expected<_Gr>);
1580 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1588 template<
typename _Fn>
1590 or_else(_Fn&& __f)
const &
1592 using _Gr = __expected::__result<_Fn, const _Er&>;
1593 static_assert(__expected::__is_expected<_Gr>);
1594 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1602 template<
typename _Fn>
1604 or_else(_Fn&& __f) &&
1606 using _Gr = __expected::__result<_Fn, _Er&&>;
1607 static_assert(__expected::__is_expected<_Gr>);
1608 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1616 template<
typename _Fn>
1618 or_else(_Fn&& __f)
const &&
1620 using _Gr = __expected::__result<_Fn, const _Er&&>;
1621 static_assert(__expected::__is_expected<_Gr>);
1622 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1630 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1632 transform(_Fn&& __f) &
1634 using _Up = __expected::__result0<_Fn>;
1635 using _Res = expected<_Up, _Er>;
1638 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1640 return _Res(unexpect, error());
1643 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1645 transform(_Fn&& __f)
const &
1647 using _Up = __expected::__result0<_Fn>;
1648 using _Res = expected<_Up, _Er>;
1651 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1653 return _Res(unexpect, error());
1656 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1658 transform(_Fn&& __f) &&
1660 using _Up = __expected::__result0<_Fn>;
1661 using _Res = expected<_Up, _Er>;
1664 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1666 return _Res(unexpect,
std::move(error()));
1669 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1671 transform(_Fn&& __f)
const &&
1673 using _Up = __expected::__result0<_Fn>;
1674 using _Res = expected<_Up, _Er>;
1677 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1679 return _Res(unexpect,
std::move(error()));
1682 template<
typename _Fn>
1684 transform_error(_Fn&& __f) &
1686 using _Gr = __expected::__result<_Fn, _Er&>;
1687 using _Res = expected<_Tp, _Gr>;
1692 return _Res(__unexpect_inv{}, [&]() {
1698 template<
typename _Fn>
1700 transform_error(_Fn&& __f)
const &
1702 using _Gr = __expected::__result<_Fn, const _Er&>;
1703 using _Res = expected<_Tp, _Gr>;
1708 return _Res(__unexpect_inv{}, [&]() {
1714 template<
typename _Fn>
1716 transform_error(_Fn&& __f) &&
1718 using _Gr = __expected::__result<_Fn, _Er&&>;
1719 using _Res = expected<_Tp, _Gr>;
1724 return _Res(__unexpect_inv{}, [&]() {
1730 template<
typename _Fn>
1732 transform_error(_Fn&& __f)
const &&
1734 using _Gr = __expected::__result<_Fn, const _Er&&>;
1735 using _Res = expected<_Tp, _Gr>;
1740 return _Res(__unexpect_inv{}, [&]() {
1748 template<
typename _Up,
typename _Er2>
1749 requires is_void_v<_Up>
1750 friend constexpr bool
1751 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1754 if (__x.has_value())
1755 return __y.has_value();
1757 return !__y.has_value() && bool(__x.error() == __y.error());
1760 template<
typename _Er2>
1761 friend constexpr bool
1762 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1764 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1766 friend constexpr void
1767 swap(expected& __x, expected& __y)
1768 noexcept(
noexcept(__x.swap(__y)))
1769 requires requires { __x.swap(__y); }
1773 template<
typename,
typename>
friend class expected;
1775 template<
typename _Vp>
1777 _M_assign_unex(_Vp&& __v)
1781 std::construct_at(__builtin_addressof(_M_unex),
1782 std::forward<_Vp>(__v));
1783 _M_has_value =
false;
1786 _M_unex = std::forward<_Vp>(__v);
1789 using __in_place_inv = __expected::__in_place_inv;
1790 using __unexpect_inv = __expected::__unexpect_inv;
1792 template<
typename _Fn>
1794 expected(__in_place_inv, _Fn&& __fn)
1795 : _M_void(), _M_has_value(true)
1796 { std::forward<_Fn>(__fn)(); }
1798 template<
typename _Fn>
1800 expected(__unexpect_inv, _Fn&& __fn)
1801 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1813_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.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.