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>;
1167 friend constexpr bool
1168 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1169 noexcept(
noexcept(bool(*__x == *__y))
1170 &&
noexcept(bool(__x.error() == __y.error())))
1172 if (__x.has_value() != __y.has_value())
1174 if (__x.has_value())
1175 return *__x == *__y;
1176 return __x.error() == __y.error();
1179 template<
typename _Up, same_as<_Tp> _Vp>
1180 requires (!__expected::__is_expected<_Up>)
1181 &&
requires (
const _Tp& __t,
const _Up& __u) {
1182 { __t == __u } -> convertible_to<bool>;
1185 friend constexpr bool
1186 operator==(
const expected<_Vp, _Er>& __x,
const _Up& __v)
1187 noexcept(
noexcept(bool(*__x == __v)))
1189 if (__x.has_value())
1194 template<
typename _Er2>
1195 requires requires (
const _Er& __e,
const _Er2& __e2) {
1196 { __e == __e2 } -> convertible_to<bool>;
1199 friend constexpr bool
1200 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1201 noexcept(
noexcept(bool(__x.error() == __e.error())))
1203 if (!__x.has_value())
1204 return __x.error() == __e.error();
1208 friend constexpr void
1209 swap(expected& __x, expected& __y)
1210 noexcept(
noexcept(__x.swap(__y)))
1211 requires requires {__x.swap(__y);}
1215 template<
typename,
typename>
friend class expected;
1217 template<
typename _Vp>
1219 _M_assign_val(_Vp&& __v)
1225 __expected::__reinit(__builtin_addressof(_M_val),
1226 __builtin_addressof(_M_unex),
1228 _M_has_value =
true;
1232 template<
typename _Vp>
1234 _M_assign_unex(_Vp&& __v)
1238 __expected::__reinit(__builtin_addressof(_M_unex),
1239 __builtin_addressof(_M_val),
1241 _M_has_value =
false;
1250 _M_swap_val_unex(expected& __rhs)
1251 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1252 is_nothrow_move_constructible<_Tp>>)
1254 if constexpr (is_nothrow_move_constructible_v<_Er>)
1256 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1257 std::construct_at(__builtin_addressof(__rhs._M_val),
1259 __rhs._M_has_value =
true;
1260 std::destroy_at(__builtin_addressof(_M_val));
1261 std::construct_at(__builtin_addressof(_M_unex),
1263 _M_has_value =
false;
1267 __expected::_Guard<_Tp> __guard(_M_val);
1268 std::construct_at(__builtin_addressof(_M_unex),
1270 _M_has_value =
false;
1271 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1272 std::construct_at(__builtin_addressof(__rhs._M_val),
1274 __rhs._M_has_value =
true;
1278 using __in_place_inv = __expected::__in_place_inv;
1279 using __unexpect_inv = __expected::__unexpect_inv;
1281 template<
typename _Fn>
1283 expected(__in_place_inv, _Fn&& __fn)
1284 : _M_val(std::
forward<_Fn>(__fn)()), _M_has_value(true)
1287 template<
typename _Fn>
1289 expected(__unexpect_inv, _Fn&& __fn)
1290 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1294 remove_cv_t<_Tp> _M_val;
1302 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1303 class expected<_Tp, _Er>
1305 static_assert( __expected::__can_be_unexpected<_Er> );
1307 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1308 static constexpr bool __cons_from_expected
1309 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1310 is_constructible<_Unex, expected<_Up, _Err>>,
1311 is_constructible<_Unex, const expected<_Up, _Err>&>,
1312 is_constructible<_Unex, const expected<_Up, _Err>>
1315 template<
typename _Up>
1316 static constexpr bool __same_val
1317 = is_same_v<typename _Up::value_type, _Tp>;
1319 template<
typename _Up>
1320 static constexpr bool __same_err
1321 = is_same_v<typename _Up::error_type, _Er>;
1324 using value_type = _Tp;
1326 using unexpected_type = unexpected<_Er>;
1328 template<
typename _Up>
1329 using rebind = expected<_Up, error_type>;
1333 : _M_void(), _M_has_value(true)
1336 expected(
const expected&) =
default;
1339 expected(
const expected& __x)
1340 noexcept(is_nothrow_copy_constructible_v<_Er>)
1341 requires is_copy_constructible_v<_Er>
1342 && (!is_trivially_copy_constructible_v<_Er>)
1343 : _M_void(), _M_has_value(__x._M_has_value)
1346 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1349 expected(expected&&) =
default;
1352 expected(expected&& __x)
1353 noexcept(is_nothrow_move_constructible_v<_Er>)
1354 requires is_move_constructible_v<_Er>
1355 && (!is_trivially_move_constructible_v<_Er>)
1356 : _M_void(), _M_has_value(__x._M_has_value)
1359 std::construct_at(__builtin_addressof(_M_unex),
1363 template<
typename _Up,
typename _Gr>
1364 requires is_void_v<_Up>
1365 && is_constructible_v<_Er, const _Gr&>
1366 && (!__cons_from_expected<_Up, _Gr>)
1367 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1368 expected(
const expected<_Up, _Gr>& __x)
1369 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1370 : _M_void(), _M_has_value(__x._M_has_value)
1373 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1376 template<
typename _Up,
typename _Gr>
1377 requires is_void_v<_Up>
1378 && is_constructible_v<_Er, _Gr>
1379 && (!__cons_from_expected<_Up, _Gr>)
1380 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1381 expected(expected<_Up, _Gr>&& __x)
1382 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1383 : _M_void(), _M_has_value(__x._M_has_value)
1386 std::construct_at(__builtin_addressof(_M_unex),
1390 template<
typename _Gr = _Er>
1391 requires is_constructible_v<_Er, const _Gr&>
1392 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1393 expected(
const unexpected<_Gr>& __u)
1394 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1395 : _M_unex(__u.error()), _M_has_value(false)
1398 template<
typename _Gr = _Er>
1399 requires is_constructible_v<_Er, _Gr>
1400 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1401 expected(unexpected<_Gr>&& __u)
1402 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1403 : _M_unex(std::
move(__u).error()), _M_has_value(false)
1407 expected(in_place_t) noexcept
1411 template<
typename... _Args>
1412 requires is_constructible_v<_Er, _Args...>
1414 expected(unexpect_t, _Args&&... __args)
1415 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1416 : _M_unex(std::
forward<_Args>(__args)...), _M_has_value(false)
1419 template<
typename _Up,
typename... _Args>
1420 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1422 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1423 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1425 : _M_unex(__il, std::
forward<_Args>(__args)...), _M_has_value(false)
1428 constexpr ~expected() =
default;
1430 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1433 std::destroy_at(__builtin_addressof(_M_unex));
1438 expected& operator=(
const expected&) =
delete;
1441 operator=(
const expected& __x)
1442 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1443 is_nothrow_copy_assignable<_Er>>)
1444 requires is_copy_constructible_v<_Er>
1445 && is_copy_assignable_v<_Er>
1447 if (__x._M_has_value)
1450 _M_assign_unex(__x._M_unex);
1455 operator=(expected&& __x)
1456 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1457 is_nothrow_move_assignable<_Er>>)
1458 requires is_move_constructible_v<_Er>
1459 && is_move_assignable_v<_Er>
1461 if (__x._M_has_value)
1468 template<
typename _Gr>
1469 requires is_constructible_v<_Er, const _Gr&>
1470 && is_assignable_v<_Er&, const _Gr&>
1472 operator=(
const unexpected<_Gr>& __e)
1474 _M_assign_unex(__e.error());
1478 template<
typename _Gr>
1479 requires is_constructible_v<_Er, _Gr>
1480 && is_assignable_v<_Er&, _Gr>
1482 operator=(unexpected<_Gr>&& __e)
1495 std::destroy_at(__builtin_addressof(_M_unex));
1496 _M_has_value =
true;
1503 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1504 is_nothrow_move_constructible<_Er>>)
1505 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1509 if (!__x._M_has_value)
1511 std::construct_at(__builtin_addressof(_M_unex),
1513 std::destroy_at(__builtin_addressof(__x._M_unex));
1514 _M_has_value =
false;
1515 __x._M_has_value =
true;
1520 if (__x._M_has_value)
1522 std::construct_at(__builtin_addressof(__x._M_unex),
1524 std::destroy_at(__builtin_addressof(_M_unex));
1525 _M_has_value =
true;
1526 __x._M_has_value =
false;
1531 swap(_M_unex, __x._M_unex);
1540 operator bool() const noexcept {
return _M_has_value; }
1543 constexpr bool has_value() const noexcept {
return _M_has_value; }
1546 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1551 static_assert( is_copy_constructible_v<_Er> );
1552 if (_M_has_value) [[likely]]
1554 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1560 static_assert( is_copy_constructible_v<_Er> );
1561 static_assert( is_move_constructible_v<_Er> );
1562 if (_M_has_value) [[likely]]
1564 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1567 constexpr const _Er&
1568 error() const & noexcept
1570 __glibcxx_assert(!_M_has_value);
1577 __glibcxx_assert(!_M_has_value);
1581 constexpr const _Er&&
1582 error() const && noexcept
1584 __glibcxx_assert(!_M_has_value);
1591 __glibcxx_assert(!_M_has_value);
1595 template<
typename _Gr = _Er>
1597 error_or(_Gr&& __e)
const&
1599 static_assert( is_copy_constructible_v<_Er> );
1600 static_assert( is_convertible_v<_Gr, _Er> );
1607 template<
typename _Gr = _Er>
1609 error_or(_Gr&& __e) &&
1611 static_assert( is_move_constructible_v<_Er> );
1612 static_assert( is_convertible_v<_Gr, _Er> );
1621 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1623 and_then(_Fn&& __f) &
1625 using _Up = __expected::__result0<_Fn>;
1626 static_assert(__expected::__is_expected<_Up>);
1627 static_assert(is_same_v<typename _Up::error_type, _Er>);
1632 return _Up(unexpect, _M_unex);
1635 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1637 and_then(_Fn&& __f)
const &
1639 using _Up = __expected::__result0<_Fn>;
1640 static_assert(__expected::__is_expected<_Up>);
1641 static_assert(is_same_v<typename _Up::error_type, _Er>);
1646 return _Up(unexpect, _M_unex);
1649 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1651 and_then(_Fn&& __f) &&
1653 using _Up = __expected::__result0<_Fn>;
1654 static_assert(__expected::__is_expected<_Up>);
1655 static_assert(is_same_v<typename _Up::error_type, _Er>);
1660 return _Up(unexpect,
std::move(_M_unex));
1663 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1665 and_then(_Fn&& __f)
const &&
1667 using _Up = __expected::__result0<_Fn>;
1668 static_assert(__expected::__is_expected<_Up>);
1669 static_assert(is_same_v<typename _Up::error_type, _Er>);
1674 return _Up(unexpect,
std::move(_M_unex));
1677 template<
typename _Fn>
1679 or_else(_Fn&& __f) &
1681 using _Gr = __expected::__result<_Fn, _Er&>;
1682 static_assert(__expected::__is_expected<_Gr>);
1683 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1691 template<
typename _Fn>
1693 or_else(_Fn&& __f)
const &
1695 using _Gr = __expected::__result<_Fn, const _Er&>;
1696 static_assert(__expected::__is_expected<_Gr>);
1697 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1705 template<
typename _Fn>
1707 or_else(_Fn&& __f) &&
1709 using _Gr = __expected::__result<_Fn, _Er&&>;
1710 static_assert(__expected::__is_expected<_Gr>);
1711 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1719 template<
typename _Fn>
1721 or_else(_Fn&& __f)
const &&
1723 using _Gr = __expected::__result<_Fn, const _Er&&>;
1724 static_assert(__expected::__is_expected<_Gr>);
1725 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1733 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1735 transform(_Fn&& __f) &
1737 using _Up = __expected::__result0_xform<_Fn>;
1738 using _Res = expected<_Up, _Er>;
1743 return _Res(unexpect, _M_unex);
1746 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1748 transform(_Fn&& __f)
const &
1750 using _Up = __expected::__result0_xform<_Fn>;
1751 using _Res = expected<_Up, _Er>;
1756 return _Res(unexpect, _M_unex);
1759 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1761 transform(_Fn&& __f) &&
1763 using _Up = __expected::__result0_xform<_Fn>;
1764 using _Res = expected<_Up, _Er>;
1769 return _Res(unexpect,
std::move(_M_unex));
1772 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1774 transform(_Fn&& __f)
const &&
1776 using _Up = __expected::__result0_xform<_Fn>;
1777 using _Res = expected<_Up, _Er>;
1782 return _Res(unexpect,
std::move(_M_unex));
1785 template<
typename _Fn>
1787 transform_error(_Fn&& __f) &
1789 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1790 using _Res = expected<_Tp, _Gr>;
1795 return _Res(__unexpect_inv{}, [&]() {
1801 template<
typename _Fn>
1803 transform_error(_Fn&& __f)
const &
1805 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1806 using _Res = expected<_Tp, _Gr>;
1811 return _Res(__unexpect_inv{}, [&]() {
1817 template<
typename _Fn>
1819 transform_error(_Fn&& __f) &&
1821 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1822 using _Res = expected<_Tp, _Gr>;
1827 return _Res(__unexpect_inv{}, [&]() {
1833 template<
typename _Fn>
1835 transform_error(_Fn&& __f)
const &&
1837 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1838 using _Res = expected<_Tp, _Gr>;
1843 return _Res(__unexpect_inv{}, [&]() {
1851 template<
typename _Up,
typename _Er2>
1852 requires is_void_v<_Up>
1853 &&
requires (
const _Er& __e,
const _Er2& __e2) {
1854 { __e == __e2 } -> convertible_to<bool>;
1857 friend constexpr bool
1858 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1859 noexcept(
noexcept(bool(__x.error() == __y.error())))
1861 if (__x.has_value() != __y.has_value())
1863 if (__x.has_value())
1865 return __x.error() == __y.error();
1868 template<
typename _Er2>
1869 requires requires (
const _Er& __e,
const _Er2& __e2) {
1870 { __e == __e2 } -> convertible_to<bool>;
1873 friend constexpr bool
1874 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1875 noexcept(
noexcept(bool(__x.error() == __e.error())))
1877 if (!__x.has_value())
1878 return __x.error() == __e.error();
1882 friend constexpr void
1883 swap(expected& __x, expected& __y)
1884 noexcept(
noexcept(__x.swap(__y)))
1885 requires requires { __x.swap(__y); }
1889 template<
typename,
typename>
friend class expected;
1891 template<
typename _Vp>
1893 _M_assign_unex(_Vp&& __v)
1897 std::construct_at(__builtin_addressof(_M_unex),
1899 _M_has_value =
false;
1905 using __in_place_inv = __expected::__in_place_inv;
1906 using __unexpect_inv = __expected::__unexpect_inv;
1908 template<
typename _Fn>
1910 expected(__in_place_inv, _Fn&& __fn)
1911 : _M_void(), _M_has_value(true)
1914 template<
typename _Fn>
1916 expected(__unexpect_inv, _Fn&& __fn)
1917 : _M_unex(std::
forward<_Fn>(__fn)()), _M_has_value(false)
1929_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.