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,
typename _Tp>
158 using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
159 template<
typename _Fn>
160 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
161 template<
typename _Fn>
162 using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
164 template<
typename _Er>
165 concept __can_be_unexpected
166 = is_object_v<_Er> && (!is_array_v<_Er>)
167 && (!__expected::__is_unexpected<_Er>)
168 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
171 struct __in_place_inv { };
172 struct __unexpect_inv { };
176 template<
typename _Er>
179 static_assert( __expected::__can_be_unexpected<_Er> );
185 template<
typename _Err = _Er>
186 requires (!is_same_v<remove_cvref_t<_Err>,
unexpected>)
187 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
188 && is_constructible_v<_Er, _Err>
191 noexcept(is_nothrow_constructible_v<_Er, _Err>)
192 : _M_unex(std::forward<_Err>(__e))
195 template<
typename... _Args>
196 requires is_constructible_v<_Er, _Args...>
199 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
200 : _M_unex(std::forward<_Args>(__args)...)
203 template<
typename _Up,
typename... _Args>
204 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
206 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
207 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
209 : _M_unex(__il, std::forward<_Args>(__args)...)
218 error() const & noexcept {
return _M_unex; }
222 error() &
noexcept {
return _M_unex; }
225 constexpr const _Er&&
226 error() const && noexcept {
return std::move(_M_unex); }
230 error() &&
noexcept {
return std::move(_M_unex); }
234 requires is_swappable_v<_Er>
237 swap(_M_unex, __other._M_unex);
240 template<
typename _Err>
242 friend constexpr bool
243 operator==(
const unexpected& __x,
const unexpected<_Err>& __y)
244 {
return __x._M_unex == __y.error(); }
246 friend constexpr void
248 requires is_swappable_v<_Er>
255 template<
typename _Er>
unexpected(_Er) -> unexpected<_Er>;
260 template<
typename _Tp>
263 static_assert( is_nothrow_move_constructible_v<_Tp> );
267 : _M_guarded(__builtin_addressof(__x)), _M_tmp(
std::
move(__x))
268 { std::destroy_at(_M_guarded); }
273 if (_M_guarded) [[unlikely]]
274 std::construct_at(_M_guarded,
std::move(_M_tmp));
277 _Guard(
const _Guard&) =
delete;
278 _Guard& operator=(
const _Guard&) =
delete;
283 _M_guarded =
nullptr;
293 template<
typename _Tp,
typename _Up,
typename _Vp>
295 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
296 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
298 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
300 std::destroy_at(__oldval);
301 std::construct_at(__newval, std::forward<_Vp>(__arg));
303 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
305 _Tp __tmp(std::forward<_Vp>(__arg));
306 std::destroy_at(__oldval);
307 std::construct_at(__newval,
std::move(__tmp));
311 _Guard<_Up> __guard(*__oldval);
312 std::construct_at(__newval, std::forward<_Vp>(__arg));
319 template<
typename _Tp,
typename _Er>
322 static_assert( ! is_reference_v<_Tp> );
323 static_assert( ! is_function_v<_Tp> );
324 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
325 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
326 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
327 static_assert( __expected::__can_be_unexpected<_Er> );
329 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
330 static constexpr bool __cons_from_expected
331 = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
332 is_constructible<_Tp, expected<_Up, _Err>>,
333 is_constructible<_Tp, const expected<_Up, _Err>&>,
334 is_constructible<_Tp, const expected<_Up, _Err>>,
335 is_convertible<expected<_Up, _Err>&, _Tp>,
336 is_convertible<expected<_Up, _Err>, _Tp>,
337 is_convertible<const expected<_Up, _Err>&, _Tp>,
338 is_convertible<const expected<_Up, _Err>, _Tp>,
339 is_constructible<_Unex, expected<_Up, _Err>&>,
340 is_constructible<_Unex, expected<_Up, _Err>>,
341 is_constructible<_Unex, const expected<_Up, _Err>&>,
342 is_constructible<_Unex, const expected<_Up, _Err>>
345 template<
typename _Up,
typename _Err>
346 constexpr static bool __explicit_conv
347 = __or_v<__not_<is_convertible<_Up, _Tp>>,
348 __not_<is_convertible<_Err, _Er>>
351 template<
typename _Up>
352 static constexpr bool __same_val
353 = is_same_v<typename _Up::value_type, _Tp>;
355 template<
typename _Up>
356 static constexpr bool __same_err
357 = is_same_v<typename _Up::error_type, _Er>;
360 using value_type = _Tp;
362 using unexpected_type = unexpected<_Er>;
364 template<
typename _Up>
365 using rebind = expected<_Up, error_type>;
369 noexcept(is_nothrow_default_constructible_v<_Tp>)
370 requires is_default_constructible_v<_Tp>
371 : _M_val(), _M_has_value(true)
374 expected(
const expected&) =
default;
377 expected(
const expected& __x)
378 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
379 is_nothrow_copy_constructible<_Er>>)
380 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
381 && (!is_trivially_copy_constructible_v<_Tp>
382 || !is_trivially_copy_constructible_v<_Er>)
383 : _M_has_value(__x._M_has_value)
386 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
388 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
391 expected(expected&&) =
default;
394 expected(expected&& __x)
395 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
396 is_nothrow_move_constructible<_Er>>)
397 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
398 && (!is_trivially_move_constructible_v<_Tp>
399 || !is_trivially_move_constructible_v<_Er>)
400 : _M_has_value(__x._M_has_value)
403 std::construct_at(__builtin_addressof(_M_val),
406 std::construct_at(__builtin_addressof(_M_unex),
410 template<
typename _Up,
typename _Gr>
411 requires is_constructible_v<_Tp, const _Up&>
412 && is_constructible_v<_Er, const _Gr&>
413 && (!__cons_from_expected<_Up, _Gr>)
414 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
415 expected(
const expected<_Up, _Gr>& __x)
416 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
417 is_nothrow_constructible<_Er, const _Gr&>>)
418 : _M_has_value(__x._M_has_value)
421 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
423 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
426 template<
typename _Up,
typename _Gr>
427 requires is_constructible_v<_Tp, _Up>
428 && is_constructible_v<_Er, _Gr>
429 && (!__cons_from_expected<_Up, _Gr>)
430 constexpr explicit(__explicit_conv<_Up, _Gr>)
431 expected(expected<_Up, _Gr>&& __x)
432 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
433 is_nothrow_constructible<_Er, _Gr>>)
434 : _M_has_value(__x._M_has_value)
437 std::construct_at(__builtin_addressof(_M_val),
440 std::construct_at(__builtin_addressof(_M_unex),
444 template<
typename _Up = _Tp>
445 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
446 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
447 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
448 && is_constructible_v<_Tp, _Up>
449 constexpr explicit(!is_convertible_v<_Up, _Tp>)
451 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
452 : _M_val(std::forward<_Up>(__v)), _M_has_value(
true)
455 template<
typename _Gr = _Er>
456 requires is_constructible_v<_Er, const _Gr&>
457 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
458 expected(
const unexpected<_Gr>& __u)
459 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
460 : _M_unex(__u.error()), _M_has_value(
false)
463 template<
typename _Gr = _Er>
464 requires is_constructible_v<_Er, _Gr>
465 constexpr explicit(!is_convertible_v<_Gr, _Er>)
466 expected(unexpected<_Gr>&& __u)
467 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
468 : _M_unex(
std::move(__u).error()), _M_has_value(
false)
471 template<
typename... _Args>
472 requires is_constructible_v<_Tp, _Args...>
474 expected(in_place_t, _Args&&... __args)
475 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
476 : _M_val(std::forward<_Args>(__args)...), _M_has_value(
true)
479 template<
typename _Up,
typename... _Args>
480 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
482 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
483 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
485 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(
true)
488 template<
typename... _Args>
489 requires is_constructible_v<_Er, _Args...>
491 expected(unexpect_t, _Args&&... __args)
492 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
493 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(
false)
496 template<
typename _Up,
typename... _Args>
497 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
499 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
500 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
502 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(
false)
505 constexpr ~expected() =
default;
507 constexpr ~expected()
508 requires (!is_trivially_destructible_v<_Tp>)
509 || (!is_trivially_destructible_v<_Er>)
512 std::destroy_at(__builtin_addressof(_M_val));
514 std::destroy_at(__builtin_addressof(_M_unex));
519 expected& operator=(
const expected&) =
delete;
522 operator=(
const expected& __x)
523 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
524 is_nothrow_copy_constructible<_Er>,
525 is_nothrow_copy_assignable<_Tp>,
526 is_nothrow_copy_assignable<_Er>>)
527 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
528 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
529 && (is_nothrow_move_constructible_v<_Tp>
530 || is_nothrow_move_constructible_v<_Er>)
532 if (__x._M_has_value)
533 this->_M_assign_val(__x._M_val);
535 this->_M_assign_unex(__x._M_unex);
540 operator=(expected&& __x)
541 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
542 is_nothrow_move_constructible<_Er>,
543 is_nothrow_move_assignable<_Tp>,
544 is_nothrow_move_assignable<_Er>>)
545 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
546 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
547 && (is_nothrow_move_constructible_v<_Tp>
548 || is_nothrow_move_constructible_v<_Er>)
550 if (__x._M_has_value)
557 template<
typename _Up = _Tp>
558 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
559 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
560 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
561 && (is_nothrow_constructible_v<_Tp, _Up>
562 || is_nothrow_move_constructible_v<_Tp>
563 || is_nothrow_move_constructible_v<_Er>)
567 _M_assign_val(std::forward<_Up>(__v));
571 template<
typename _Gr>
572 requires is_constructible_v<_Er, const _Gr&>
573 && is_assignable_v<_Er&, const _Gr&>
574 && (is_nothrow_constructible_v<_Er, const _Gr&>
575 || is_nothrow_move_constructible_v<_Tp>
576 || is_nothrow_move_constructible_v<_Er>)
578 operator=(
const unexpected<_Gr>& __e)
580 _M_assign_unex(__e.error());
584 template<
typename _Gr>
585 requires is_constructible_v<_Er, _Gr>
586 && is_assignable_v<_Er&, _Gr>
587 && (is_nothrow_constructible_v<_Er, _Gr>
588 || is_nothrow_move_constructible_v<_Tp>
589 || is_nothrow_move_constructible_v<_Er>)
591 operator=(unexpected<_Gr>&& __e)
599 template<
typename... _Args>
600 requires is_nothrow_constructible_v<_Tp, _Args...>
602 emplace(_Args&&... __args)
noexcept
605 std::destroy_at(__builtin_addressof(_M_val));
608 std::destroy_at(__builtin_addressof(_M_unex));
611 std::construct_at(__builtin_addressof(_M_val),
612 std::forward<_Args>(__args)...);
616 template<
typename _Up,
typename... _Args>
617 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
620 emplace(initializer_list<_Up> __il, _Args&&... __args)
noexcept
623 std::destroy_at(__builtin_addressof(_M_val));
626 std::destroy_at(__builtin_addressof(_M_unex));
629 std::construct_at(__builtin_addressof(_M_val),
630 __il, std::forward<_Args>(__args)...);
637 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
638 is_nothrow_move_constructible<_Er>,
639 is_nothrow_swappable<_Tp&>,
640 is_nothrow_swappable<_Er&>>)
641 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
642 && is_move_constructible_v<_Tp>
643 && is_move_constructible_v<_Er>
644 && (is_nothrow_move_constructible_v<_Tp>
645 || is_nothrow_move_constructible_v<_Er>)
649 if (__x._M_has_value)
652 swap(_M_val, __x._M_val);
655 this->_M_swap_val_unex(__x);
659 if (__x._M_has_value)
660 __x._M_swap_val_unex(*
this);
664 swap(_M_unex, __x._M_unex);
673 operator->() const noexcept
675 __glibcxx_assert(_M_has_value);
676 return __builtin_addressof(_M_val);
681 operator->() noexcept
683 __glibcxx_assert(_M_has_value);
684 return __builtin_addressof(_M_val);
691 __glibcxx_assert(_M_has_value);
699 __glibcxx_assert(_M_has_value);
704 constexpr const _Tp&&
707 __glibcxx_assert(_M_has_value);
715 __glibcxx_assert(_M_has_value);
721 operator bool() const noexcept {
return _M_has_value; }
724 constexpr bool has_value() const noexcept {
return _M_has_value; }
729 if (_M_has_value) [[likely]]
731 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
737 if (_M_has_value) [[likely]]
739 const auto& __unex = _M_unex;
740 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
743 constexpr const _Tp&&
746 if (_M_has_value) [[likely]]
748 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
755 if (_M_has_value) [[likely]]
757 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
762 error() const & noexcept
764 __glibcxx_assert(!_M_has_value);
771 __glibcxx_assert(!_M_has_value);
775 constexpr const _Er&&
776 error() const && noexcept
778 __glibcxx_assert(!_M_has_value);
785 __glibcxx_assert(!_M_has_value);
789 template<
typename _Up>
791 value_or(_Up&& __v)
const &
792 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
793 is_nothrow_convertible<_Up, _Tp>>)
795 static_assert( is_copy_constructible_v<_Tp> );
796 static_assert( is_convertible_v<_Up, _Tp> );
800 return static_cast<_Tp
>(std::forward<_Up>(__v));
803 template<
typename _Up>
805 value_or(_Up&& __v) &&
806 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
807 is_nothrow_convertible<_Up, _Tp>>)
809 static_assert( is_move_constructible_v<_Tp> );
810 static_assert( is_convertible_v<_Up, _Tp> );
814 return static_cast<_Tp
>(std::forward<_Up>(__v));
817 template<
typename _Gr = _Er>
819 error_or(_Gr&& __e)
const&
821 static_assert( is_copy_constructible_v<_Er> );
822 static_assert( is_convertible_v<_Gr, _Er> );
825 return std::forward<_Gr>(__e);
829 template<
typename _Gr = _Er>
831 error_or(_Gr&& __e) &&
833 static_assert( is_move_constructible_v<_Er> );
834 static_assert( is_convertible_v<_Gr, _Er> );
837 return std::forward<_Gr>(__e);
843 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
845 and_then(_Fn&& __f) &
847 using _Up = __expected::__result<_Fn, _Tp&>;
848 static_assert(__expected::__is_expected<_Up>);
849 static_assert(is_same_v<typename _Up::error_type, _Er>);
854 return _Up(unexpect, error());
857 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
859 and_then(_Fn&& __f)
const &
861 using _Up = __expected::__result<_Fn, const _Tp&>;
862 static_assert(__expected::__is_expected<_Up>);
863 static_assert(is_same_v<typename _Up::error_type, _Er>);
868 return _Up(unexpect, error());
871 template<
typename _Fn>
requires is_move_constructible_v<_Er>
873 and_then(_Fn&& __f) &&
875 using _Up = __expected::__result<_Fn, _Tp&&>;
876 static_assert(__expected::__is_expected<_Up>);
877 static_assert(is_same_v<typename _Up::error_type, _Er>);
882 return _Up(unexpect,
std::move(error()));
886 template<
typename _Fn>
requires is_move_constructible_v<_Er>
888 and_then(_Fn&& __f)
const &&
890 using _Up = __expected::__result<_Fn, const _Tp&&>;
891 static_assert(__expected::__is_expected<_Up>);
892 static_assert(is_same_v<typename _Up::error_type, _Er>);
897 return _Up(unexpect,
std::move(error()));
900 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
904 using _Gr = __expected::__result<_Fn, _Er&>;
905 static_assert(__expected::__is_expected<_Gr>);
906 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
909 return _Gr(in_place, value());
914 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
916 or_else(_Fn&& __f)
const &
918 using _Gr = __expected::__result<_Fn, const _Er&>;
919 static_assert(__expected::__is_expected<_Gr>);
920 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
923 return _Gr(in_place, value());
929 template<
typename _Fn>
requires is_move_constructible_v<_Er>
931 or_else(_Fn&& __f) &&
933 using _Gr = __expected::__result<_Fn, _Er&&>;
934 static_assert(__expected::__is_expected<_Gr>);
935 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
938 return _Gr(in_place,
std::move(value()));
943 template<
typename _Fn>
requires is_move_constructible_v<_Er>
945 or_else(_Fn&& __f)
const &&
947 using _Gr = __expected::__result<_Fn, const _Er&&>;
948 static_assert(__expected::__is_expected<_Gr>);
949 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
952 return _Gr(in_place,
std::move(value()));
957 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
959 transform(_Fn&& __f) &
961 using _Up = __expected::__result_xform<_Fn, _Tp&>;
962 using _Res = expected<_Up, _Er>;
965 return _Res(__in_place_inv{}, [&]() {
970 return _Res(unexpect,
std::move(error()));
973 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
975 transform(_Fn&& __f)
const &
977 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
978 using _Res = expected<_Up, _Er>;
981 return _Res(__in_place_inv{}, [&]() {
986 return _Res(unexpect,
std::move(error()));
989 template<
typename _Fn>
requires is_move_constructible_v<_Er>
991 transform(_Fn&& __f) &&
993 using _Up = __expected::__result_xform<_Fn, _Tp>;
994 using _Res = expected<_Up, _Er>;
997 return _Res(__in_place_inv{}, [&]() {
1002 return _Res(unexpect,
std::move(error()));
1005 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1007 transform(_Fn&& __f)
const &&
1009 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1010 using _Res = expected<_Up, _Er>;
1013 return _Res(__in_place_inv{}, [&]() {
1018 return _Res(unexpect,
std::move(error()));
1021 template<
typename _Fn>
requires is_copy_constructible_v<_Tp>
1023 transform_error(_Fn&& __f) &
1025 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1026 using _Res = expected<_Tp, _Gr>;
1029 return _Res(in_place, value());
1031 return _Res(__unexpect_inv{}, [&]() {
1037 template<
typename _Fn>
requires is_copy_constructible_v<_Tp>
1039 transform_error(_Fn&& __f)
const &
1041 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1042 using _Res = expected<_Tp, _Gr>;
1045 return _Res(in_place, value());
1047 return _Res(__unexpect_inv{}, [&]() {
1053 template<
typename _Fn>
requires is_move_constructible_v<_Tp>
1055 transform_error(_Fn&& __f) &&
1057 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1058 using _Res = expected<_Tp, _Gr>;
1061 return _Res(in_place,
std::move(value()));
1063 return _Res(__unexpect_inv{}, [&]() {
1069 template<
typename _Fn>
requires is_move_constructible_v<_Tp>
1071 transform_error(_Fn&& __f)
const &&
1073 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1074 using _Res = expected<_Tp, _Gr>;
1077 return _Res(in_place,
std::move(value()));
1079 return _Res(__unexpect_inv{}, [&]() {
1087 template<
typename _Up,
typename _Er2>
1088 requires (!is_void_v<_Up>)
1089 friend constexpr bool
1090 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1094 if (__x.has_value())
1095 return __y.has_value() && bool(*__x == *__y);
1097 return !__y.has_value() && bool(__x.error() == __y.error());
1100 template<
typename _Up>
1101 friend constexpr bool
1102 operator==(
const expected& __x,
const _Up& __v)
1104 {
return __x.has_value() && bool(*__x == __v); }
1106 template<
typename _Er2>
1107 friend constexpr bool
1108 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1110 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1112 friend constexpr void
1113 swap(expected& __x, expected& __y)
1114 noexcept(
noexcept(__x.swap(__y)))
1115 requires requires {__x.swap(__y);}
1119 template<
typename,
typename>
friend class expected;
1121 template<
typename _Vp>
1123 _M_assign_val(_Vp&& __v)
1126 _M_val = std::forward<_Vp>(__v);
1129 __expected::__reinit(__builtin_addressof(_M_val),
1130 __builtin_addressof(_M_unex),
1131 std::forward<_Vp>(__v));
1132 _M_has_value =
true;
1136 template<
typename _Vp>
1138 _M_assign_unex(_Vp&& __v)
1142 __expected::__reinit(__builtin_addressof(_M_unex),
1143 __builtin_addressof(_M_val),
1144 std::forward<_Vp>(__v));
1145 _M_has_value =
false;
1148 _M_unex = std::forward<_Vp>(__v);
1154 _M_swap_val_unex(expected& __rhs)
1155 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1156 is_nothrow_move_constructible<_Tp>>)
1158 if constexpr (is_nothrow_move_constructible_v<_Er>)
1160 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1161 std::construct_at(__builtin_addressof(__rhs._M_val),
1163 __rhs._M_has_value =
true;
1164 std::destroy_at(__builtin_addressof(_M_val));
1165 std::construct_at(__builtin_addressof(_M_unex),
1167 _M_has_value =
false;
1171 __expected::_Guard<_Tp> __guard(_M_val);
1172 std::construct_at(__builtin_addressof(_M_unex),
1174 _M_has_value =
false;
1175 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1176 std::construct_at(__builtin_addressof(__rhs._M_val),
1178 __rhs._M_has_value =
true;
1182 using __in_place_inv = __expected::__in_place_inv;
1183 using __unexpect_inv = __expected::__unexpect_inv;
1185 template<
typename _Fn>
1187 expected(__in_place_inv, _Fn&& __fn)
1188 : _M_val(
std::
forward<_Fn>(__fn)()), _M_has_value(true)
1191 template<
typename _Fn>
1193 expected(__unexpect_inv, _Fn&& __fn)
1194 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1206 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1207 class expected<_Tp, _Er>
1209 static_assert( __expected::__can_be_unexpected<_Er> );
1211 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1212 static constexpr bool __cons_from_expected
1213 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1214 is_constructible<_Unex, expected<_Up, _Err>>,
1215 is_constructible<_Unex, const expected<_Up, _Err>&>,
1216 is_constructible<_Unex, const expected<_Up, _Err>>
1219 template<
typename _Up>
1220 static constexpr bool __same_val
1221 = is_same_v<typename _Up::value_type, _Tp>;
1223 template<
typename _Up>
1224 static constexpr bool __same_err
1225 = is_same_v<typename _Up::error_type, _Er>;
1228 using value_type = _Tp;
1230 using unexpected_type = unexpected<_Er>;
1232 template<
typename _Up>
1233 using rebind = expected<_Up, error_type>;
1237 : _M_void(), _M_has_value(true)
1240 expected(
const expected&) =
default;
1243 expected(
const expected& __x)
1244 noexcept(is_nothrow_copy_constructible_v<_Er>)
1245 requires is_copy_constructible_v<_Er>
1246 && (!is_trivially_copy_constructible_v<_Er>)
1247 : _M_void(), _M_has_value(__x._M_has_value)
1250 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1253 expected(expected&&) =
default;
1256 expected(expected&& __x)
1257 noexcept(is_nothrow_move_constructible_v<_Er>)
1258 requires is_move_constructible_v<_Er>
1259 && (!is_trivially_move_constructible_v<_Er>)
1260 : _M_void(), _M_has_value(__x._M_has_value)
1263 std::construct_at(__builtin_addressof(_M_unex),
1267 template<
typename _Up,
typename _Gr>
1268 requires is_void_v<_Up>
1269 && is_constructible_v<_Er, const _Gr&>
1270 && (!__cons_from_expected<_Up, _Gr>)
1271 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1272 expected(
const expected<_Up, _Gr>& __x)
1273 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1274 : _M_void(), _M_has_value(__x._M_has_value)
1277 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1280 template<
typename _Up,
typename _Gr>
1281 requires is_void_v<_Up>
1282 && is_constructible_v<_Er, _Gr>
1283 && (!__cons_from_expected<_Up, _Gr>)
1284 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1285 expected(expected<_Up, _Gr>&& __x)
1286 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1287 : _M_void(), _M_has_value(__x._M_has_value)
1290 std::construct_at(__builtin_addressof(_M_unex),
1294 template<
typename _Gr = _Er>
1295 requires is_constructible_v<_Er, const _Gr&>
1296 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1297 expected(
const unexpected<_Gr>& __u)
1298 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1299 : _M_unex(__u.error()), _M_has_value(
false)
1302 template<
typename _Gr = _Er>
1303 requires is_constructible_v<_Er, _Gr>
1304 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1305 expected(unexpected<_Gr>&& __u)
1306 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1307 : _M_unex(
std::move(__u).error()), _M_has_value(
false)
1311 expected(in_place_t) noexcept
1315 template<
typename... _Args>
1316 requires is_constructible_v<_Er, _Args...>
1318 expected(unexpect_t, _Args&&... __args)
1319 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1320 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(
false)
1323 template<
typename _Up,
typename... _Args>
1324 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1326 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1327 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1329 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(
false)
1332 constexpr ~expected() =
default;
1334 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1337 std::destroy_at(__builtin_addressof(_M_unex));
1342 expected& operator=(
const expected&) =
delete;
1345 operator=(
const expected& __x)
1346 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1347 is_nothrow_copy_assignable<_Er>>)
1348 requires is_copy_constructible_v<_Er>
1349 && is_copy_assignable_v<_Er>
1351 if (__x._M_has_value)
1354 _M_assign_unex(__x._M_unex);
1359 operator=(expected&& __x)
1360 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1361 is_nothrow_move_assignable<_Er>>)
1362 requires is_move_constructible_v<_Er>
1363 && is_move_assignable_v<_Er>
1365 if (__x._M_has_value)
1372 template<
typename _Gr>
1373 requires is_constructible_v<_Er, const _Gr&>
1374 && is_assignable_v<_Er&, const _Gr&>
1376 operator=(
const unexpected<_Gr>& __e)
1378 _M_assign_unex(__e.error());
1382 template<
typename _Gr>
1383 requires is_constructible_v<_Er, _Gr>
1384 && is_assignable_v<_Er&, _Gr>
1386 operator=(unexpected<_Gr>&& __e)
1399 std::destroy_at(__builtin_addressof(_M_unex));
1400 _M_has_value =
true;
1407 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1408 is_nothrow_move_constructible<_Er>>)
1409 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1413 if (!__x._M_has_value)
1415 std::construct_at(__builtin_addressof(_M_unex),
1417 std::destroy_at(__builtin_addressof(__x._M_unex));
1418 _M_has_value =
false;
1419 __x._M_has_value =
true;
1424 if (__x._M_has_value)
1426 std::construct_at(__builtin_addressof(__x._M_unex),
1428 std::destroy_at(__builtin_addressof(_M_unex));
1429 _M_has_value =
true;
1430 __x._M_has_value =
false;
1435 swap(_M_unex, __x._M_unex);
1444 operator bool() const noexcept {
return _M_has_value; }
1447 constexpr bool has_value() const noexcept {
return _M_has_value; }
1450 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1455 if (_M_has_value) [[likely]]
1457 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1463 if (_M_has_value) [[likely]]
1465 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1468 constexpr const _Er&
1469 error() const & noexcept
1471 __glibcxx_assert(!_M_has_value);
1478 __glibcxx_assert(!_M_has_value);
1482 constexpr const _Er&&
1483 error() const && noexcept
1485 __glibcxx_assert(!_M_has_value);
1492 __glibcxx_assert(!_M_has_value);
1496 template<
typename _Gr = _Er>
1498 error_or(_Gr&& __e)
const&
1500 static_assert( is_copy_constructible_v<_Er> );
1501 static_assert( is_convertible_v<_Gr, _Er> );
1504 return std::forward<_Gr>(__e);
1508 template<
typename _Gr = _Er>
1510 error_or(_Gr&& __e) &&
1512 static_assert( is_move_constructible_v<_Er> );
1513 static_assert( is_convertible_v<_Gr, _Er> );
1516 return std::forward<_Gr>(__e);
1522 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1524 and_then(_Fn&& __f) &
1526 using _Up = __expected::__result0<_Fn>;
1527 static_assert(__expected::__is_expected<_Up>);
1528 static_assert(is_same_v<typename _Up::error_type, _Er>);
1533 return _Up(unexpect, error());
1536 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1538 and_then(_Fn&& __f)
const &
1540 using _Up = __expected::__result0<_Fn>;
1541 static_assert(__expected::__is_expected<_Up>);
1542 static_assert(is_same_v<typename _Up::error_type, _Er>);
1547 return _Up(unexpect, error());
1550 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1552 and_then(_Fn&& __f) &&
1554 using _Up = __expected::__result0<_Fn>;
1555 static_assert(__expected::__is_expected<_Up>);
1556 static_assert(is_same_v<typename _Up::error_type, _Er>);
1561 return _Up(unexpect,
std::move(error()));
1564 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1566 and_then(_Fn&& __f)
const &&
1568 using _Up = __expected::__result0<_Fn>;
1569 static_assert(__expected::__is_expected<_Up>);
1570 static_assert(is_same_v<typename _Up::error_type, _Er>);
1575 return _Up(unexpect,
std::move(error()));
1578 template<
typename _Fn>
1580 or_else(_Fn&& __f) &
1582 using _Gr = __expected::__result<_Fn, _Er&>;
1583 static_assert(__expected::__is_expected<_Gr>);
1584 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1592 template<
typename _Fn>
1594 or_else(_Fn&& __f)
const &
1596 using _Gr = __expected::__result<_Fn, const _Er&>;
1597 static_assert(__expected::__is_expected<_Gr>);
1598 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1606 template<
typename _Fn>
1608 or_else(_Fn&& __f) &&
1610 using _Gr = __expected::__result<_Fn, _Er&&>;
1611 static_assert(__expected::__is_expected<_Gr>);
1612 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1620 template<
typename _Fn>
1622 or_else(_Fn&& __f)
const &&
1624 using _Gr = __expected::__result<_Fn, const _Er&&>;
1625 static_assert(__expected::__is_expected<_Gr>);
1626 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1634 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1636 transform(_Fn&& __f) &
1638 using _Up = __expected::__result0_xform<_Fn>;
1639 using _Res = expected<_Up, _Er>;
1642 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1644 return _Res(unexpect, error());
1647 template<
typename _Fn>
requires is_copy_constructible_v<_Er>
1649 transform(_Fn&& __f)
const &
1651 using _Up = __expected::__result0_xform<_Fn>;
1652 using _Res = expected<_Up, _Er>;
1655 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1657 return _Res(unexpect, error());
1660 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1662 transform(_Fn&& __f) &&
1664 using _Up = __expected::__result0_xform<_Fn>;
1665 using _Res = expected<_Up, _Er>;
1668 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1670 return _Res(unexpect,
std::move(error()));
1673 template<
typename _Fn>
requires is_move_constructible_v<_Er>
1675 transform(_Fn&& __f)
const &&
1677 using _Up = __expected::__result0_xform<_Fn>;
1678 using _Res = expected<_Up, _Er>;
1681 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1683 return _Res(unexpect,
std::move(error()));
1686 template<
typename _Fn>
1688 transform_error(_Fn&& __f) &
1690 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1691 using _Res = expected<_Tp, _Gr>;
1696 return _Res(__unexpect_inv{}, [&]() {
1702 template<
typename _Fn>
1704 transform_error(_Fn&& __f)
const &
1706 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1707 using _Res = expected<_Tp, _Gr>;
1712 return _Res(__unexpect_inv{}, [&]() {
1718 template<
typename _Fn>
1720 transform_error(_Fn&& __f) &&
1722 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1723 using _Res = expected<_Tp, _Gr>;
1728 return _Res(__unexpect_inv{}, [&]() {
1734 template<
typename _Fn>
1736 transform_error(_Fn&& __f)
const &&
1738 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1739 using _Res = expected<_Tp, _Gr>;
1744 return _Res(__unexpect_inv{}, [&]() {
1752 template<
typename _Up,
typename _Er2>
1753 requires is_void_v<_Up>
1754 friend constexpr bool
1755 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1758 if (__x.has_value())
1759 return __y.has_value();
1761 return !__y.has_value() && bool(__x.error() == __y.error());
1764 template<
typename _Er2>
1765 friend constexpr bool
1766 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1768 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1770 friend constexpr void
1771 swap(expected& __x, expected& __y)
1772 noexcept(
noexcept(__x.swap(__y)))
1773 requires requires { __x.swap(__y); }
1777 template<
typename,
typename>
friend class expected;
1779 template<
typename _Vp>
1781 _M_assign_unex(_Vp&& __v)
1785 std::construct_at(__builtin_addressof(_M_unex),
1786 std::forward<_Vp>(__v));
1787 _M_has_value =
false;
1790 _M_unex = std::forward<_Vp>(__v);
1793 using __in_place_inv = __expected::__in_place_inv;
1794 using __unexpect_inv = __expected::__unexpect_inv;
1796 template<
typename _Fn>
1798 expected(__in_place_inv, _Fn&& __fn)
1799 : _M_void(), _M_has_value(true)
1800 { std::forward<_Fn>(__fn)(); }
1802 template<
typename _Fn>
1804 expected(__unexpect_inv, _Fn&& __fn)
1805 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1817_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.