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);
827 template<
typename _Up = remove_cv_t<_Tp>>
828 constexpr remove_cv_t<_Tp>
829 value_or(_Up&& __v)
const &
830 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
831 is_nothrow_convertible<_Up, _Tp>>)
833 using _Xp = remove_cv_t<_Tp>;
834 static_assert( is_convertible_v<const _Tp&, _Xp> );
835 static_assert( is_convertible_v<_Up, _Tp> );
842 template<
typename _Up = remove_cv_t<_Tp>>
843 constexpr remove_cv_t<_Tp>
844 value_or(_Up&& __v) &&
845 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
846 is_nothrow_convertible<_Up, _Tp>>)
848 using _Xp = remove_cv_t<_Tp>;
849 static_assert( is_convertible_v<_Tp, _Xp> );
850 static_assert( is_convertible_v<_Up, _Xp> );
857 template<
typename _Gr = _Er>
859 error_or(_Gr&& __e)
const&
861 static_assert( is_copy_constructible_v<_Er> );
862 static_assert( is_convertible_v<_Gr, _Er> );
869 template<
typename _Gr = _Er>
871 error_or(_Gr&& __e) &&
873 static_assert( is_move_constructible_v<_Er> );
874 static_assert( is_convertible_v<_Gr, _Er> );
883 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
885 and_then(_Fn&& __f) &
887 using _Up = __expected::__result<_Fn, _Tp&>;
888 static_assert(__expected::__is_expected<_Up>,
889 "the function passed to std::expected<T, E>::and_then "
890 "must return a std::expected");
891 static_assert(is_same_v<typename _Up::error_type, _Er>,
892 "the function passed to std::expected<T, E>::and_then "
893 "must return a std::expected with the same error_type");
898 return _Up(unexpect, _M_unex);
901 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
903 and_then(_Fn&& __f)
const &
905 using _Up = __expected::__result<_Fn, const _Tp&>;
906 static_assert(__expected::__is_expected<_Up>,
907 "the function passed to std::expected<T, E>::and_then "
908 "must return a std::expected");
909 static_assert(is_same_v<typename _Up::error_type, _Er>,
910 "the function passed to std::expected<T, E>::and_then "
911 "must return a std::expected with the same error_type");
916 return _Up(unexpect, _M_unex);
919 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
921 and_then(_Fn&& __f) &&
923 using _Up = __expected::__result<_Fn, _Tp&&>;
924 static_assert(__expected::__is_expected<_Up>,
925 "the function passed to std::expected<T, E>::and_then "
926 "must return a std::expected");
927 static_assert(is_same_v<typename _Up::error_type, _Er>,
928 "the function passed to std::expected<T, E>::and_then "
929 "must return a std::expected with the same error_type");
934 return _Up(unexpect,
std::move(_M_unex));
938 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
940 and_then(_Fn&& __f)
const &&
942 using _Up = __expected::__result<_Fn, const _Tp&&>;
943 static_assert(__expected::__is_expected<_Up>,
944 "the function passed to std::expected<T, E>::and_then "
945 "must return a std::expected");
946 static_assert(is_same_v<typename _Up::error_type, _Er>,
947 "the function passed to std::expected<T, E>::and_then "
948 "must return a std::expected with the same error_type");
953 return _Up(unexpect,
std::move(_M_unex));
956 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
960 using _Gr = __expected::__result<_Fn, _Er&>;
961 static_assert(__expected::__is_expected<_Gr>,
962 "the function passed to std::expected<T, E>::or_else "
963 "must return a std::expected");
964 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
965 "the function passed to std::expected<T, E>::or_else "
966 "must return a std::expected with the same value_type");
969 return _Gr(in_place, _M_val);
974 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
976 or_else(_Fn&& __f)
const &
978 using _Gr = __expected::__result<_Fn, const _Er&>;
979 static_assert(__expected::__is_expected<_Gr>,
980 "the function passed to std::expected<T, E>::or_else "
981 "must return a std::expected");
982 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
983 "the function passed to std::expected<T, E>::or_else "
984 "must return a std::expected with the same value_type");
987 return _Gr(in_place, _M_val);
993 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
995 or_else(_Fn&& __f) &&
997 using _Gr = __expected::__result<_Fn, _Er&&>;
998 static_assert(__expected::__is_expected<_Gr>,
999 "the function passed to std::expected<T, E>::or_else "
1000 "must return a std::expected");
1001 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1002 "the function passed to std::expected<T, E>::or_else "
1003 "must return a std::expected with the same value_type");
1006 return _Gr(in_place,
std::move(_M_val));
1011 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1013 or_else(_Fn&& __f)
const &&
1015 using _Gr = __expected::__result<_Fn, const _Er&&>;
1016 static_assert(__expected::__is_expected<_Gr>,
1017 "the function passed to std::expected<T, E>::or_else "
1018 "must return a std::expected");
1019 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1020 "the function passed to std::expected<T, E>::or_else "
1021 "must return a std::expected with the same value_type");
1024 return _Gr(in_place,
std::move(_M_val));
1029 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1031 transform(_Fn&& __f) &
1033 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1034 using _Res = expected<_Up, _Er>;
1037 return _Res(__in_place_inv{}, [&]() {
1042 return _Res(unexpect, _M_unex);
1045 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1047 transform(_Fn&& __f)
const &
1049 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1050 using _Res = expected<_Up, _Er>;
1053 return _Res(__in_place_inv{}, [&]() {
1058 return _Res(unexpect, _M_unex);
1061 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1063 transform(_Fn&& __f) &&
1065 using _Up = __expected::__result_xform<_Fn, _Tp>;
1066 using _Res = expected<_Up, _Er>;
1069 return _Res(__in_place_inv{}, [&]() {
1074 return _Res(unexpect,
std::move(_M_unex));
1077 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1079 transform(_Fn&& __f)
const &&
1081 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1082 using _Res = expected<_Up, _Er>;
1085 return _Res(__in_place_inv{}, [&]() {
1090 return _Res(unexpect,
std::move(_M_unex));
1093 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
1095 transform_error(_Fn&& __f) &
1097 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1098 using _Res = expected<_Tp, _Gr>;
1101 return _Res(in_place, _M_val);
1103 return _Res(__unexpect_inv{}, [&]() {
1109 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
1111 transform_error(_Fn&& __f)
const &
1113 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1114 using _Res = expected<_Tp, _Gr>;
1117 return _Res(in_place, _M_val);
1119 return _Res(__unexpect_inv{}, [&]() {
1125 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
1127 transform_error(_Fn&& __f) &&
1129 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1130 using _Res = expected<_Tp, _Gr>;
1133 return _Res(in_place,
std::move(_M_val));
1135 return _Res(__unexpect_inv{}, [&]() {
1141 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1143 transform_error(_Fn&& __f)
const &&
1145 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1146 using _Res = expected<_Tp, _Gr>;
1149 return _Res(in_place,
std::move(_M_val));
1151 return _Res(__unexpect_inv{}, [&]() {
1159 template<
typename _Up,
typename _Er2>
1160 requires (!is_void_v<_Up>)
1161 &&
requires (
const _Tp& __t,
const _Up& __u,
1162 const _Er& __e,
const _Er2& __e2) {
1163 { __t == __u } -> convertible_to<bool>;
1164 { __e == __e2 } -> convertible_to<bool>;
1166 friend constexpr bool
1167 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1168 noexcept(
noexcept(bool(*__x == *__y))
1169 &&
noexcept(bool(__x.error() == __y.error())))
1171 if (__x.has_value())
1172 return __y.has_value() && bool(*__x == *__y);
1174 return !__y.has_value() && bool(__x.error() == __y.error());
1177 template<
typename _Up, same_as<_Tp> _Vp>
1178 requires (!__expected::__is_expected<_Up>)
1179 &&
requires (
const _Tp& __t,
const _Up& __u) {
1180 { __t == __u } -> convertible_to<bool>;
1182 friend constexpr bool
1183 operator==(
const expected<_Vp, _Er>& __x,
const _Up& __v)
1184 noexcept(
noexcept(bool(*__x == __v)))
1185 {
return __x.has_value() && bool(*__x == __v); }
1187 template<
typename _Er2>
1188 requires requires (
const _Er& __e,
const _Er2& __e2) {
1189 { __e == __e2 } -> convertible_to<bool>;
1191 friend constexpr bool
1192 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1193 noexcept(
noexcept(bool(__x.error() == __e.error())))
1194 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1196 friend constexpr void
1197 swap(expected& __x, expected& __y)
1198 noexcept(
noexcept(__x.swap(__y)))
1199 requires requires {__x.swap(__y);}
1203 template<
typename,
typename>
friend class expected;
1205 template<
typename _Vp>
1207 _M_assign_val(_Vp&& __v)
1213 __expected::__reinit(__builtin_addressof(_M_val),
1214 __builtin_addressof(_M_unex),
1216 _M_has_value =
true;
1220 template<
typename _Vp>
1222 _M_assign_unex(_Vp&& __v)
1226 __expected::__reinit(__builtin_addressof(_M_unex),
1227 __builtin_addressof(_M_val),
1229 _M_has_value =
false;
1238 _M_swap_val_unex(expected& __rhs)
1239 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1240 is_nothrow_move_constructible<_Tp>>)
1242 if constexpr (is_nothrow_move_constructible_v<_Er>)
1244 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1245 std::construct_at(__builtin_addressof(__rhs._M_val),
1247 __rhs._M_has_value =
true;
1248 std::destroy_at(__builtin_addressof(_M_val));
1249 std::construct_at(__builtin_addressof(_M_unex),
1251 _M_has_value =
false;
1255 __expected::_Guard<_Tp> __guard(_M_val);
1256 std::construct_at(__builtin_addressof(_M_unex),
1258 _M_has_value =
false;
1259 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1260 std::construct_at(__builtin_addressof(__rhs._M_val),
1262 __rhs._M_has_value =
true;
1266 using __in_place_inv = __expected::__in_place_inv;
1267 using __unexpect_inv = __expected::__unexpect_inv;
1269 template<
typename _Fn>
1271 expected(__in_place_inv, _Fn&& __fn)
1272 : _M_val(std::
forward<_Fn>(__fn)()), _M_has_value(true)
1275 template<
typename _Fn>
1277 expected(__unexpect_inv, _Fn&& __fn)
1278 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1282 remove_cv_t<_Tp> _M_val;
1290 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1291 class expected<_Tp, _Er>
1293 static_assert( __expected::__can_be_unexpected<_Er> );
1295 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1296 static constexpr bool __cons_from_expected
1297 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1298 is_constructible<_Unex, expected<_Up, _Err>>,
1299 is_constructible<_Unex, const expected<_Up, _Err>&>,
1300 is_constructible<_Unex, const expected<_Up, _Err>>
1303 template<
typename _Up>
1304 static constexpr bool __same_val
1305 = is_same_v<typename _Up::value_type, _Tp>;
1307 template<
typename _Up>
1308 static constexpr bool __same_err
1309 = is_same_v<typename _Up::error_type, _Er>;
1312 using value_type = _Tp;
1314 using unexpected_type = unexpected<_Er>;
1316 template<
typename _Up>
1317 using rebind = expected<_Up, error_type>;
1321 : _M_void(), _M_has_value(true)
1324 expected(
const expected&) =
default;
1327 expected(
const expected& __x)
1328 noexcept(is_nothrow_copy_constructible_v<_Er>)
1329 requires is_copy_constructible_v<_Er>
1330 && (!is_trivially_copy_constructible_v<_Er>)
1331 : _M_void(), _M_has_value(__x._M_has_value)
1334 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1337 expected(expected&&) =
default;
1340 expected(expected&& __x)
1341 noexcept(is_nothrow_move_constructible_v<_Er>)
1342 requires is_move_constructible_v<_Er>
1343 && (!is_trivially_move_constructible_v<_Er>)
1344 : _M_void(), _M_has_value(__x._M_has_value)
1347 std::construct_at(__builtin_addressof(_M_unex),
1351 template<
typename _Up,
typename _Gr>
1352 requires is_void_v<_Up>
1353 && is_constructible_v<_Er, const _Gr&>
1354 && (!__cons_from_expected<_Up, _Gr>)
1355 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1356 expected(
const expected<_Up, _Gr>& __x)
1357 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1358 : _M_void(), _M_has_value(__x._M_has_value)
1361 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1364 template<
typename _Up,
typename _Gr>
1365 requires is_void_v<_Up>
1366 && is_constructible_v<_Er, _Gr>
1367 && (!__cons_from_expected<_Up, _Gr>)
1368 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1369 expected(expected<_Up, _Gr>&& __x)
1370 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1371 : _M_void(), _M_has_value(__x._M_has_value)
1374 std::construct_at(__builtin_addressof(_M_unex),
1378 template<
typename _Gr = _Er>
1379 requires is_constructible_v<_Er, const _Gr&>
1380 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1381 expected(
const unexpected<_Gr>& __u)
1382 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1383 : _M_unex(__u.error()), _M_has_value(false)
1386 template<
typename _Gr = _Er>
1387 requires is_constructible_v<_Er, _Gr>
1388 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1389 expected(unexpected<_Gr>&& __u)
1390 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1391 : _M_unex(std::
move(__u).error()), _M_has_value(false)
1395 expected(in_place_t) noexcept
1399 template<
typename... _Args>
1400 requires is_constructible_v<_Er, _Args...>
1402 expected(unexpect_t, _Args&&... __args)
1403 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1404 : _M_unex(std::
forward<_Args>(__args)...), _M_has_value(false)
1407 template<
typename _Up,
typename... _Args>
1408 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1410 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1411 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1413 : _M_unex(__il, std::
forward<_Args>(__args)...), _M_has_value(false)
1416 constexpr ~expected() =
default;
1418 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1421 std::destroy_at(__builtin_addressof(_M_unex));
1426 expected& operator=(
const expected&) =
delete;
1429 operator=(
const expected& __x)
1430 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1431 is_nothrow_copy_assignable<_Er>>)
1432 requires is_copy_constructible_v<_Er>
1433 && is_copy_assignable_v<_Er>
1435 if (__x._M_has_value)
1438 _M_assign_unex(__x._M_unex);
1443 operator=(expected&& __x)
1444 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1445 is_nothrow_move_assignable<_Er>>)
1446 requires is_move_constructible_v<_Er>
1447 && is_move_assignable_v<_Er>
1449 if (__x._M_has_value)
1456 template<
typename _Gr>
1457 requires is_constructible_v<_Er, const _Gr&>
1458 && is_assignable_v<_Er&, const _Gr&>
1460 operator=(
const unexpected<_Gr>& __e)
1462 _M_assign_unex(__e.error());
1466 template<
typename _Gr>
1467 requires is_constructible_v<_Er, _Gr>
1468 && is_assignable_v<_Er&, _Gr>
1470 operator=(unexpected<_Gr>&& __e)
1483 std::destroy_at(__builtin_addressof(_M_unex));
1484 _M_has_value =
true;
1491 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1492 is_nothrow_move_constructible<_Er>>)
1493 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1497 if (!__x._M_has_value)
1499 std::construct_at(__builtin_addressof(_M_unex),
1501 std::destroy_at(__builtin_addressof(__x._M_unex));
1502 _M_has_value =
false;
1503 __x._M_has_value =
true;
1508 if (__x._M_has_value)
1510 std::construct_at(__builtin_addressof(__x._M_unex),
1512 std::destroy_at(__builtin_addressof(_M_unex));
1513 _M_has_value =
true;
1514 __x._M_has_value =
false;
1519 swap(_M_unex, __x._M_unex);
1528 operator bool() const noexcept {
return _M_has_value; }
1531 constexpr bool has_value() const noexcept {
return _M_has_value; }
1534 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1539 static_assert( is_copy_constructible_v<_Er> );
1540 if (_M_has_value) [[likely]]
1542 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1548 static_assert( is_copy_constructible_v<_Er> );
1549 static_assert( is_move_constructible_v<_Er> );
1550 if (_M_has_value) [[likely]]
1552 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1555 constexpr const _Er&
1556 error() const & noexcept
1558 __glibcxx_assert(!_M_has_value);
1565 __glibcxx_assert(!_M_has_value);
1569 constexpr const _Er&&
1570 error() const && noexcept
1572 __glibcxx_assert(!_M_has_value);
1579 __glibcxx_assert(!_M_has_value);
1583 template<
typename _Gr = _Er>
1585 error_or(_Gr&& __e)
const&
1587 static_assert( is_copy_constructible_v<_Er> );
1588 static_assert( is_convertible_v<_Gr, _Er> );
1595 template<
typename _Gr = _Er>
1597 error_or(_Gr&& __e) &&
1599 static_assert( is_move_constructible_v<_Er> );
1600 static_assert( is_convertible_v<_Gr, _Er> );
1609 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1611 and_then(_Fn&& __f) &
1613 using _Up = __expected::__result0<_Fn>;
1614 static_assert(__expected::__is_expected<_Up>);
1615 static_assert(is_same_v<typename _Up::error_type, _Er>);
1620 return _Up(unexpect, _M_unex);
1623 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1625 and_then(_Fn&& __f)
const &
1627 using _Up = __expected::__result0<_Fn>;
1628 static_assert(__expected::__is_expected<_Up>);
1629 static_assert(is_same_v<typename _Up::error_type, _Er>);
1634 return _Up(unexpect, _M_unex);
1637 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1639 and_then(_Fn&& __f) &&
1641 using _Up = __expected::__result0<_Fn>;
1642 static_assert(__expected::__is_expected<_Up>);
1643 static_assert(is_same_v<typename _Up::error_type, _Er>);
1648 return _Up(unexpect,
std::move(_M_unex));
1651 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1653 and_then(_Fn&& __f)
const &&
1655 using _Up = __expected::__result0<_Fn>;
1656 static_assert(__expected::__is_expected<_Up>);
1657 static_assert(is_same_v<typename _Up::error_type, _Er>);
1662 return _Up(unexpect,
std::move(_M_unex));
1665 template<
typename _Fn>
1667 or_else(_Fn&& __f) &
1669 using _Gr = __expected::__result<_Fn, _Er&>;
1670 static_assert(__expected::__is_expected<_Gr>);
1671 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1679 template<
typename _Fn>
1681 or_else(_Fn&& __f)
const &
1683 using _Gr = __expected::__result<_Fn, const _Er&>;
1684 static_assert(__expected::__is_expected<_Gr>);
1685 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1693 template<
typename _Fn>
1695 or_else(_Fn&& __f) &&
1697 using _Gr = __expected::__result<_Fn, _Er&&>;
1698 static_assert(__expected::__is_expected<_Gr>);
1699 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1707 template<
typename _Fn>
1709 or_else(_Fn&& __f)
const &&
1711 using _Gr = __expected::__result<_Fn, const _Er&&>;
1712 static_assert(__expected::__is_expected<_Gr>);
1713 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1721 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1723 transform(_Fn&& __f) &
1725 using _Up = __expected::__result0_xform<_Fn>;
1726 using _Res = expected<_Up, _Er>;
1731 return _Res(unexpect, _M_unex);
1734 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1736 transform(_Fn&& __f)
const &
1738 using _Up = __expected::__result0_xform<_Fn>;
1739 using _Res = expected<_Up, _Er>;
1744 return _Res(unexpect, _M_unex);
1747 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1749 transform(_Fn&& __f) &&
1751 using _Up = __expected::__result0_xform<_Fn>;
1752 using _Res = expected<_Up, _Er>;
1757 return _Res(unexpect,
std::move(_M_unex));
1760 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1762 transform(_Fn&& __f)
const &&
1764 using _Up = __expected::__result0_xform<_Fn>;
1765 using _Res = expected<_Up, _Er>;
1770 return _Res(unexpect,
std::move(_M_unex));
1773 template<
typename _Fn>
1775 transform_error(_Fn&& __f) &
1777 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1778 using _Res = expected<_Tp, _Gr>;
1783 return _Res(__unexpect_inv{}, [&]() {
1789 template<
typename _Fn>
1791 transform_error(_Fn&& __f)
const &
1793 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1794 using _Res = expected<_Tp, _Gr>;
1799 return _Res(__unexpect_inv{}, [&]() {
1805 template<
typename _Fn>
1807 transform_error(_Fn&& __f) &&
1809 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1810 using _Res = expected<_Tp, _Gr>;
1815 return _Res(__unexpect_inv{}, [&]() {
1821 template<
typename _Fn>
1823 transform_error(_Fn&& __f)
const &&
1825 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1826 using _Res = expected<_Tp, _Gr>;
1831 return _Res(__unexpect_inv{}, [&]() {
1839 template<
typename _Up,
typename _Er2>
1840 requires is_void_v<_Up>
1841 &&
requires (
const _Er& __e,
const _Er2& __e2) {
1842 { __e == __e2 } -> convertible_to<bool>;
1844 friend constexpr bool
1845 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1846 noexcept(
noexcept(bool(__x.error() == __y.error())))
1848 if (__x.has_value())
1849 return __y.has_value();
1851 return !__y.has_value() && bool(__x.error() == __y.error());
1854 template<
typename _Er2>
1855 requires requires (
const _Er& __e,
const _Er2& __e2) {
1856 { __e == __e2 } -> convertible_to<bool>;
1858 friend constexpr bool
1859 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1860 noexcept(
noexcept(bool(__x.error() == __e.error())))
1861 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1863 friend constexpr void
1864 swap(expected& __x, expected& __y)
1865 noexcept(
noexcept(__x.swap(__y)))
1866 requires requires { __x.swap(__y); }
1870 template<
typename,
typename>
friend class expected;
1872 template<
typename _Vp>
1874 _M_assign_unex(_Vp&& __v)
1878 std::construct_at(__builtin_addressof(_M_unex),
1880 _M_has_value =
false;
1886 using __in_place_inv = __expected::__in_place_inv;
1887 using __unexpect_inv = __expected::__unexpect_inv;
1889 template<
typename _Fn>
1891 expected(__in_place_inv, _Fn&& __fn)
1892 : _M_void(), _M_has_value(true)
1895 template<
typename _Fn>
1897 expected(__unexpect_inv, _Fn&& __fn)
1898 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1910_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.