29#ifndef _GLIBCXX_EXPECTED
30#define _GLIBCXX_EXPECTED
32#pragma GCC system_header
34#define __glibcxx_want_expected
35#define __glibcxx_want_freestanding_expected
38#ifdef __cpp_lib_expected
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
60 template<
typename _Tp,
typename _Er>
67 template<
typename _Er>
74 template<
typename _Er>
75 class bad_expected_access;
78 class bad_expected_access<void> :
public exception
81 bad_expected_access() noexcept { }
82 bad_expected_access(
const bad_expected_access&) =
default;
83 bad_expected_access(bad_expected_access&&) =
default;
84 bad_expected_access& operator=(
const bad_expected_access&) =
default;
85 bad_expected_access& operator=(bad_expected_access&&) =
default;
86 ~bad_expected_access() =
default;
92 what() const noexcept
override
93 {
return "bad access to std::expected without expected value"; }
96 template<
typename _Er>
97 class bad_expected_access :
public bad_expected_access<void> {
100 bad_expected_access(_Er __e) : _M_unex(
std::
move(__e)) { }
111 error() const & noexcept
121 error() const && noexcept
134 explicit unexpect_t() =
default;
141 inline constexpr unexpect_t unexpect{};
146 template<
typename _Tp>
147 constexpr bool __is_expected =
false;
148 template<
typename _Tp,
typename _Er>
149 constexpr bool __is_expected<expected<_Tp, _Er>> =
true;
151 template<
typename _Tp>
152 constexpr bool __is_unexpected =
false;
153 template<
typename _Tp>
154 constexpr bool __is_unexpected<unexpected<_Tp>> =
true;
156 template<
typename _Fn,
typename _Tp>
157 using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
158 template<
typename _Fn,
typename _Tp>
159 using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
160 template<
typename _Fn>
161 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
162 template<
typename _Fn>
163 using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
165 template<
typename _Er>
166 concept __can_be_unexpected
167 = is_object_v<_Er> && (!is_array_v<_Er>)
168 && (!__expected::__is_unexpected<_Er>)
169 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
172 struct __in_place_inv { };
173 struct __unexpect_inv { };
177 template<
typename _Er>
180 static_assert( __expected::__can_be_unexpected<_Er> );
186 template<
typename _Err = _Er>
187 requires (!is_same_v<remove_cvref_t<_Err>,
unexpected>)
188 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
189 && is_constructible_v<_Er, _Err>
192 noexcept(is_nothrow_constructible_v<_Er, _Err>)
196 template<
typename... _Args>
197 requires is_constructible_v<_Er, _Args...>
200 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
204 template<
typename _Up,
typename... _Args>
205 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
207 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
208 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
210 : _M_unex(__il,
std::
forward<_Args>(__args)...)
219 error() const & noexcept {
return _M_unex; }
223 error() &
noexcept {
return _M_unex; }
226 constexpr const _Er&&
227 error() const && noexcept {
return std::move(_M_unex); }
231 error() &&
noexcept {
return std::move(_M_unex); }
234 swap(
unexpected& __other)
noexcept(is_nothrow_swappable_v<_Er>)
235 requires is_swappable_v<_Er>
238 swap(_M_unex, __other._M_unex);
241 template<
typename _Err>
243 friend constexpr bool
244 operator==(
const unexpected& __x,
const unexpected<_Err>& __y)
245 {
return __x._M_unex == __y.error(); }
247 friend constexpr void
249 requires is_swappable_v<_Er>
256 template<
typename _Er>
unexpected(_Er) -> unexpected<_Er>;
261 template<
typename _Tp>
264 static_assert( is_nothrow_move_constructible_v<_Tp> );
268 : _M_guarded(__builtin_addressof(__x)), _M_tmp(
std::
move(__x))
269 { std::destroy_at(_M_guarded); }
274 if (_M_guarded) [[unlikely]]
275 std::construct_at(_M_guarded,
std::move(_M_tmp));
278 _Guard(
const _Guard&) =
delete;
279 _Guard& operator=(
const _Guard&) =
delete;
284 _M_guarded =
nullptr;
294 template<
typename _Tp,
typename _Up,
typename _Vp>
296 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
297 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
299 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
301 std::destroy_at(__oldval);
302 std::construct_at(__newval, std::forward<_Vp>(__arg));
304 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
306 _Tp __tmp(std::forward<_Vp>(__arg));
307 std::destroy_at(__oldval);
308 std::construct_at(__newval,
std::move(__tmp));
312 _Guard<_Up> __guard(*__oldval);
313 std::construct_at(__newval, std::forward<_Vp>(__arg));
320 template<
typename _Tp,
typename _Er>
323 static_assert( ! is_reference_v<_Tp> );
324 static_assert( ! is_function_v<_Tp> );
325 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
326 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
327 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
328 static_assert( __expected::__can_be_unexpected<_Er> );
330 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
331 static constexpr bool __cons_from_expected
332 = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
333 is_constructible<_Tp, expected<_Up, _Err>>,
334 is_constructible<_Tp, const expected<_Up, _Err>&>,
335 is_constructible<_Tp, const expected<_Up, _Err>>,
336 is_convertible<expected<_Up, _Err>&, _Tp>,
337 is_convertible<expected<_Up, _Err>, _Tp>,
338 is_convertible<const expected<_Up, _Err>&, _Tp>,
339 is_convertible<const expected<_Up, _Err>, _Tp>,
340 is_constructible<_Unex, expected<_Up, _Err>&>,
341 is_constructible<_Unex, expected<_Up, _Err>>,
342 is_constructible<_Unex, const expected<_Up, _Err>&>,
343 is_constructible<_Unex, const expected<_Up, _Err>>
346 template<
typename _Up,
typename _Err>
347 constexpr static bool __explicit_conv
348 = __or_v<__not_<is_convertible<_Up, _Tp>>,
349 __not_<is_convertible<_Err, _Er>>
352 template<
typename _Up>
353 static constexpr bool __same_val
354 = is_same_v<typename _Up::value_type, _Tp>;
356 template<
typename _Up>
357 static constexpr bool __same_err
358 = is_same_v<typename _Up::error_type, _Er>;
361 using value_type = _Tp;
363 using unexpected_type = unexpected<_Er>;
365 template<
typename _Up>
366 using rebind = expected<_Up, error_type>;
370 noexcept(is_nothrow_default_constructible_v<_Tp>)
371 requires is_default_constructible_v<_Tp>
372 : _M_val(), _M_has_value(true)
375 expected(
const expected&) =
default;
378 expected(
const expected& __x)
379 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
380 is_nothrow_copy_constructible<_Er>>)
381 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
382 && (!is_trivially_copy_constructible_v<_Tp>
383 || !is_trivially_copy_constructible_v<_Er>)
384 : _M_has_value(__x._M_has_value)
387 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
389 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
392 expected(expected&&) =
default;
395 expected(expected&& __x)
396 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
397 is_nothrow_move_constructible<_Er>>)
398 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
399 && (!is_trivially_move_constructible_v<_Tp>
400 || !is_trivially_move_constructible_v<_Er>)
401 : _M_has_value(__x._M_has_value)
404 std::construct_at(__builtin_addressof(_M_val),
407 std::construct_at(__builtin_addressof(_M_unex),
411 template<
typename _Up,
typename _Gr>
412 requires is_constructible_v<_Tp, const _Up&>
413 && is_constructible_v<_Er, const _Gr&>
414 && (!__cons_from_expected<_Up, _Gr>)
415 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
416 expected(
const expected<_Up, _Gr>& __x)
417 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
418 is_nothrow_constructible<_Er, const _Gr&>>)
419 : _M_has_value(__x._M_has_value)
422 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
424 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
427 template<
typename _Up,
typename _Gr>
428 requires is_constructible_v<_Tp, _Up>
429 && is_constructible_v<_Er, _Gr>
430 && (!__cons_from_expected<_Up, _Gr>)
431 constexpr explicit(__explicit_conv<_Up, _Gr>)
432 expected(expected<_Up, _Gr>&& __x)
433 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
434 is_nothrow_constructible<_Er, _Gr>>)
435 : _M_has_value(__x._M_has_value)
438 std::construct_at(__builtin_addressof(_M_val),
441 std::construct_at(__builtin_addressof(_M_unex),
445 template<
typename _Up = _Tp>
446 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
447 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
448 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
449 && is_constructible_v<_Tp, _Up>
450 constexpr explicit(!is_convertible_v<_Up, _Tp>)
452 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
453 : _M_val(
std::
forward<_Up>(__v)), _M_has_value(true)
456 template<
typename _Gr = _Er>
457 requires is_constructible_v<_Er, const _Gr&>
458 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
459 expected(
const unexpected<_Gr>& __u)
460 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
461 : _M_unex(__u.error()), _M_has_value(false)
464 template<
typename _Gr = _Er>
465 requires is_constructible_v<_Er, _Gr>
466 constexpr explicit(!is_convertible_v<_Gr, _Er>)
467 expected(unexpected<_Gr>&& __u)
468 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
469 : _M_unex(
std::
move(__u).error()), _M_has_value(false)
472 template<
typename... _Args>
473 requires is_constructible_v<_Tp, _Args...>
475 expected(in_place_t, _Args&&... __args)
476 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
477 : _M_val(
std::
forward<_Args>(__args)...), _M_has_value(true)
480 template<
typename _Up,
typename... _Args>
481 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
483 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
484 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
486 : _M_val(__il,
std::
forward<_Args>(__args)...), _M_has_value(true)
489 template<
typename... _Args>
490 requires is_constructible_v<_Er, _Args...>
492 expected(unexpect_t, _Args&&... __args)
493 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
494 : _M_unex(
std::
forward<_Args>(__args)...), _M_has_value(false)
497 template<
typename _Up,
typename... _Args>
498 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
500 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
501 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
503 : _M_unex(__il,
std::
forward<_Args>(__args)...), _M_has_value(false)
506 constexpr ~expected() =
default;
508 constexpr ~expected()
509 requires (!is_trivially_destructible_v<_Tp>)
510 || (!is_trivially_destructible_v<_Er>)
513 std::destroy_at(__builtin_addressof(_M_val));
515 std::destroy_at(__builtin_addressof(_M_unex));
520 expected& operator=(
const expected&) =
delete;
523 operator=(
const expected& __x)
524 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
525 is_nothrow_copy_constructible<_Er>,
526 is_nothrow_copy_assignable<_Tp>,
527 is_nothrow_copy_assignable<_Er>>)
528 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
529 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
530 && (is_nothrow_move_constructible_v<_Tp>
531 || is_nothrow_move_constructible_v<_Er>)
533 if (__x._M_has_value)
534 this->_M_assign_val(__x._M_val);
536 this->_M_assign_unex(__x._M_unex);
541 operator=(expected&& __x)
542 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
543 is_nothrow_move_constructible<_Er>,
544 is_nothrow_move_assignable<_Tp>,
545 is_nothrow_move_assignable<_Er>>)
546 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
547 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
548 && (is_nothrow_move_constructible_v<_Tp>
549 || is_nothrow_move_constructible_v<_Er>)
551 if (__x._M_has_value)
558 template<
typename _Up = _Tp>
559 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
560 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
561 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
562 && (is_nothrow_constructible_v<_Tp, _Up>
563 || is_nothrow_move_constructible_v<_Tp>
564 || is_nothrow_move_constructible_v<_Er>)
568 _M_assign_val(std::forward<_Up>(__v));
572 template<
typename _Gr>
573 requires is_constructible_v<_Er, const _Gr&>
574 && is_assignable_v<_Er&, const _Gr&>
575 && (is_nothrow_constructible_v<_Er, const _Gr&>
576 || is_nothrow_move_constructible_v<_Tp>
577 || is_nothrow_move_constructible_v<_Er>)
579 operator=(
const unexpected<_Gr>& __e)
581 _M_assign_unex(__e.error());
585 template<
typename _Gr>
586 requires is_constructible_v<_Er, _Gr>
587 && is_assignable_v<_Er&, _Gr>
588 && (is_nothrow_constructible_v<_Er, _Gr>
589 || is_nothrow_move_constructible_v<_Tp>
590 || is_nothrow_move_constructible_v<_Er>)
592 operator=(unexpected<_Gr>&& __e)
600 template<
typename... _Args>
601 requires is_nothrow_constructible_v<_Tp, _Args...>
603 emplace(_Args&&... __args)
noexcept
606 std::destroy_at(__builtin_addressof(_M_val));
609 std::destroy_at(__builtin_addressof(_M_unex));
612 std::construct_at(__builtin_addressof(_M_val),
613 std::forward<_Args>(__args)...);
617 template<
typename _Up,
typename... _Args>
618 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
621 emplace(initializer_list<_Up> __il, _Args&&... __args)
noexcept
624 std::destroy_at(__builtin_addressof(_M_val));
627 std::destroy_at(__builtin_addressof(_M_unex));
630 std::construct_at(__builtin_addressof(_M_val),
631 __il, std::forward<_Args>(__args)...);
638 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
639 is_nothrow_move_constructible<_Er>,
640 is_nothrow_swappable<_Tp&>,
641 is_nothrow_swappable<_Er&>>)
642 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
643 && is_move_constructible_v<_Tp>
644 && is_move_constructible_v<_Er>
645 && (is_nothrow_move_constructible_v<_Tp>
646 || is_nothrow_move_constructible_v<_Er>)
650 if (__x._M_has_value)
653 swap(_M_val, __x._M_val);
656 this->_M_swap_val_unex(__x);
660 if (__x._M_has_value)
661 __x._M_swap_val_unex(*
this);
665 swap(_M_unex, __x._M_unex);
674 operator->() const noexcept
676 __glibcxx_assert(_M_has_value);
677 return __builtin_addressof(_M_val);
682 operator->() noexcept
684 __glibcxx_assert(_M_has_value);
685 return __builtin_addressof(_M_val);
692 __glibcxx_assert(_M_has_value);
700 __glibcxx_assert(_M_has_value);
705 constexpr const _Tp&&
708 __glibcxx_assert(_M_has_value);
716 __glibcxx_assert(_M_has_value);
722 operator bool() const noexcept {
return _M_has_value; }
725 constexpr bool has_value() const noexcept {
return _M_has_value; }
730 if (_M_has_value) [[likely]]
732 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
738 if (_M_has_value) [[likely]]
740 const auto& __unex = _M_unex;
741 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
744 constexpr const _Tp&&
747 if (_M_has_value) [[likely]]
749 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
755 if (_M_has_value) [[likely]]
757 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
761 error() const & noexcept
763 __glibcxx_assert(!_M_has_value);
770 __glibcxx_assert(!_M_has_value);
774 constexpr const _Er&&
775 error() const && noexcept
777 __glibcxx_assert(!_M_has_value);
784 __glibcxx_assert(!_M_has_value);
788 template<
typename _Up>
790 value_or(_Up&& __v)
const &
791 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
792 is_nothrow_convertible<_Up, _Tp>>)
794 static_assert( is_copy_constructible_v<_Tp> );
795 static_assert( is_convertible_v<_Up, _Tp> );
799 return static_cast<_Tp
>(std::forward<_Up>(__v));
802 template<
typename _Up>
804 value_or(_Up&& __v) &&
805 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
806 is_nothrow_convertible<_Up, _Tp>>)
808 static_assert( is_move_constructible_v<_Tp> );
809 static_assert( is_convertible_v<_Up, _Tp> );
813 return static_cast<_Tp
>(std::forward<_Up>(__v));
816 template<
typename _Gr = _Er>
818 error_or(_Gr&& __e)
const&
820 static_assert( is_copy_constructible_v<_Er> );
821 static_assert( is_convertible_v<_Gr, _Er> );
824 return std::forward<_Gr>(__e);
828 template<
typename _Gr = _Er>
830 error_or(_Gr&& __e) &&
832 static_assert( is_move_constructible_v<_Er> );
833 static_assert( is_convertible_v<_Gr, _Er> );
836 return std::forward<_Gr>(__e);
842 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
844 and_then(_Fn&& __f) &
846 using _Up = __expected::__result<_Fn, _Tp&>;
847 static_assert(__expected::__is_expected<_Up>,
848 "the function passed to std::expected<T, E>::and_then "
849 "must return a std::expected");
850 static_assert(is_same_v<typename _Up::error_type, _Er>,
851 "the function passed to std::expected<T, E>::and_then "
852 "must return a std::expected with the same error_type");
857 return _Up(unexpect, _M_unex);
860 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
862 and_then(_Fn&& __f)
const &
864 using _Up = __expected::__result<_Fn, const _Tp&>;
865 static_assert(__expected::__is_expected<_Up>,
866 "the function passed to std::expected<T, E>::and_then "
867 "must return a std::expected");
868 static_assert(is_same_v<typename _Up::error_type, _Er>,
869 "the function passed to std::expected<T, E>::and_then "
870 "must return a std::expected with the same error_type");
875 return _Up(unexpect, _M_unex);
878 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
880 and_then(_Fn&& __f) &&
882 using _Up = __expected::__result<_Fn, _Tp&&>;
883 static_assert(__expected::__is_expected<_Up>,
884 "the function passed to std::expected<T, E>::and_then "
885 "must return a std::expected");
886 static_assert(is_same_v<typename _Up::error_type, _Er>,
887 "the function passed to std::expected<T, E>::and_then "
888 "must return a std::expected with the same error_type");
893 return _Up(unexpect,
std::move(_M_unex));
897 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
899 and_then(_Fn&& __f)
const &&
901 using _Up = __expected::__result<_Fn, const _Tp&&>;
902 static_assert(__expected::__is_expected<_Up>,
903 "the function passed to std::expected<T, E>::and_then "
904 "must return a std::expected");
905 static_assert(is_same_v<typename _Up::error_type, _Er>,
906 "the function passed to std::expected<T, E>::and_then "
907 "must return a std::expected with the same error_type");
912 return _Up(unexpect,
std::move(_M_unex));
915 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
919 using _Gr = __expected::__result<_Fn, _Er&>;
920 static_assert(__expected::__is_expected<_Gr>,
921 "the function passed to std::expected<T, E>::or_else "
922 "must return a std::expected");
923 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
924 "the function passed to std::expected<T, E>::or_else "
925 "must return a std::expected with the same value_type");
928 return _Gr(in_place, _M_val);
933 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
935 or_else(_Fn&& __f)
const &
937 using _Gr = __expected::__result<_Fn, const _Er&>;
938 static_assert(__expected::__is_expected<_Gr>,
939 "the function passed to std::expected<T, E>::or_else "
940 "must return a std::expected");
941 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
942 "the function passed to std::expected<T, E>::or_else "
943 "must return a std::expected with the same value_type");
946 return _Gr(in_place, _M_val);
952 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
954 or_else(_Fn&& __f) &&
956 using _Gr = __expected::__result<_Fn, _Er&&>;
957 static_assert(__expected::__is_expected<_Gr>,
958 "the function passed to std::expected<T, E>::or_else "
959 "must return a std::expected");
960 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
961 "the function passed to std::expected<T, E>::or_else "
962 "must return a std::expected with the same value_type");
970 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
972 or_else(_Fn&& __f)
const &&
974 using _Gr = __expected::__result<_Fn, const _Er&&>;
975 static_assert(__expected::__is_expected<_Gr>,
976 "the function passed to std::expected<T, E>::or_else "
977 "must return a std::expected");
978 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
979 "the function passed to std::expected<T, E>::or_else "
980 "must return a std::expected with the same value_type");
988 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
990 transform(_Fn&& __f) &
992 using _Up = __expected::__result_xform<_Fn, _Tp&>;
993 using _Res = expected<_Up, _Er>;
996 return _Res(__in_place_inv{}, [&]() {
1001 return _Res(unexpect, _M_unex);
1004 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1006 transform(_Fn&& __f)
const &
1008 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1009 using _Res = expected<_Up, _Er>;
1012 return _Res(__in_place_inv{}, [&]() {
1017 return _Res(unexpect, _M_unex);
1020 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1022 transform(_Fn&& __f) &&
1024 using _Up = __expected::__result_xform<_Fn, _Tp>;
1025 using _Res = expected<_Up, _Er>;
1028 return _Res(__in_place_inv{}, [&]() {
1033 return _Res(unexpect,
std::move(_M_unex));
1036 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1038 transform(_Fn&& __f)
const &&
1040 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1041 using _Res = expected<_Up, _Er>;
1044 return _Res(__in_place_inv{}, [&]() {
1049 return _Res(unexpect,
std::move(_M_unex));
1052 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp&>
1054 transform_error(_Fn&& __f) &
1056 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1057 using _Res = expected<_Tp, _Gr>;
1060 return _Res(in_place, _M_val);
1062 return _Res(__unexpect_inv{}, [&]() {
1068 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp&>
1070 transform_error(_Fn&& __f)
const &
1072 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1073 using _Res = expected<_Tp, _Gr>;
1076 return _Res(in_place, _M_val);
1078 return _Res(__unexpect_inv{}, [&]() {
1084 template<
typename _Fn>
requires is_constructible_v<_Tp, _Tp>
1086 transform_error(_Fn&& __f) &&
1088 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1089 using _Res = expected<_Tp, _Gr>;
1092 return _Res(in_place,
std::move(_M_val));
1094 return _Res(__unexpect_inv{}, [&]() {
1100 template<
typename _Fn>
requires is_constructible_v<_Tp, const _Tp>
1102 transform_error(_Fn&& __f)
const &&
1104 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1105 using _Res = expected<_Tp, _Gr>;
1108 return _Res(in_place,
std::move(_M_val));
1110 return _Res(__unexpect_inv{}, [&]() {
1118 template<
typename _Up,
typename _Er2>
1119 requires (!is_void_v<_Up>)
1120 friend constexpr bool
1121 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1125 if (__x.has_value())
1126 return __y.has_value() && bool(*__x == *__y);
1128 return !__y.has_value() && bool(__x.error() == __y.error());
1131 template<
typename _Up>
1132 friend constexpr bool
1133 operator==(
const expected& __x,
const _Up& __v)
1135 {
return __x.has_value() && bool(*__x == __v); }
1137 template<
typename _Er2>
1138 friend constexpr bool
1139 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1141 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1143 friend constexpr void
1144 swap(expected& __x, expected& __y)
1145 noexcept(
noexcept(__x.swap(__y)))
1146 requires requires {__x.swap(__y);}
1150 template<
typename,
typename>
friend class expected;
1152 template<
typename _Vp>
1154 _M_assign_val(_Vp&& __v)
1157 _M_val = std::forward<_Vp>(__v);
1160 __expected::__reinit(__builtin_addressof(_M_val),
1161 __builtin_addressof(_M_unex),
1162 std::forward<_Vp>(__v));
1163 _M_has_value =
true;
1167 template<
typename _Vp>
1169 _M_assign_unex(_Vp&& __v)
1173 __expected::__reinit(__builtin_addressof(_M_unex),
1174 __builtin_addressof(_M_val),
1175 std::forward<_Vp>(__v));
1176 _M_has_value =
false;
1179 _M_unex = std::forward<_Vp>(__v);
1185 _M_swap_val_unex(expected& __rhs)
1186 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1187 is_nothrow_move_constructible<_Tp>>)
1189 if constexpr (is_nothrow_move_constructible_v<_Er>)
1191 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1192 std::construct_at(__builtin_addressof(__rhs._M_val),
1194 __rhs._M_has_value =
true;
1195 std::destroy_at(__builtin_addressof(_M_val));
1196 std::construct_at(__builtin_addressof(_M_unex),
1198 _M_has_value =
false;
1202 __expected::_Guard<_Tp> __guard(_M_val);
1203 std::construct_at(__builtin_addressof(_M_unex),
1205 _M_has_value =
false;
1206 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1207 std::construct_at(__builtin_addressof(__rhs._M_val),
1209 __rhs._M_has_value =
true;
1213 using __in_place_inv = __expected::__in_place_inv;
1214 using __unexpect_inv = __expected::__unexpect_inv;
1216 template<
typename _Fn>
1218 expected(__in_place_inv, _Fn&& __fn)
1219 : _M_val(
std::
forward<_Fn>(__fn)()), _M_has_value(true)
1222 template<
typename _Fn>
1224 expected(__unexpect_inv, _Fn&& __fn)
1225 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1237 template<
typename _Tp,
typename _Er>
requires is_void_v<_Tp>
1238 class expected<_Tp, _Er>
1240 static_assert( __expected::__can_be_unexpected<_Er> );
1242 template<
typename _Up,
typename _Err,
typename _Unex = unexpected<_Er>>
1243 static constexpr bool __cons_from_expected
1244 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1245 is_constructible<_Unex, expected<_Up, _Err>>,
1246 is_constructible<_Unex, const expected<_Up, _Err>&>,
1247 is_constructible<_Unex, const expected<_Up, _Err>>
1250 template<
typename _Up>
1251 static constexpr bool __same_val
1252 = is_same_v<typename _Up::value_type, _Tp>;
1254 template<
typename _Up>
1255 static constexpr bool __same_err
1256 = is_same_v<typename _Up::error_type, _Er>;
1259 using value_type = _Tp;
1261 using unexpected_type = unexpected<_Er>;
1263 template<
typename _Up>
1264 using rebind = expected<_Up, error_type>;
1268 : _M_void(), _M_has_value(true)
1271 expected(
const expected&) =
default;
1274 expected(
const expected& __x)
1275 noexcept(is_nothrow_copy_constructible_v<_Er>)
1276 requires is_copy_constructible_v<_Er>
1277 && (!is_trivially_copy_constructible_v<_Er>)
1278 : _M_void(), _M_has_value(__x._M_has_value)
1281 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1284 expected(expected&&) =
default;
1287 expected(expected&& __x)
1288 noexcept(is_nothrow_move_constructible_v<_Er>)
1289 requires is_move_constructible_v<_Er>
1290 && (!is_trivially_move_constructible_v<_Er>)
1291 : _M_void(), _M_has_value(__x._M_has_value)
1294 std::construct_at(__builtin_addressof(_M_unex),
1298 template<
typename _Up,
typename _Gr>
1299 requires is_void_v<_Up>
1300 && is_constructible_v<_Er, const _Gr&>
1301 && (!__cons_from_expected<_Up, _Gr>)
1302 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1303 expected(
const expected<_Up, _Gr>& __x)
1304 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1305 : _M_void(), _M_has_value(__x._M_has_value)
1308 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1311 template<
typename _Up,
typename _Gr>
1312 requires is_void_v<_Up>
1313 && is_constructible_v<_Er, _Gr>
1314 && (!__cons_from_expected<_Up, _Gr>)
1315 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1316 expected(expected<_Up, _Gr>&& __x)
1317 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1318 : _M_void(), _M_has_value(__x._M_has_value)
1321 std::construct_at(__builtin_addressof(_M_unex),
1325 template<
typename _Gr = _Er>
1326 requires is_constructible_v<_Er, const _Gr&>
1327 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1328 expected(
const unexpected<_Gr>& __u)
1329 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1330 : _M_unex(__u.error()), _M_has_value(false)
1333 template<
typename _Gr = _Er>
1334 requires is_constructible_v<_Er, _Gr>
1335 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1336 expected(unexpected<_Gr>&& __u)
1337 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1338 : _M_unex(
std::
move(__u).error()), _M_has_value(false)
1342 expected(in_place_t) noexcept
1346 template<
typename... _Args>
1347 requires is_constructible_v<_Er, _Args...>
1349 expected(unexpect_t, _Args&&... __args)
1350 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1351 : _M_unex(
std::
forward<_Args>(__args)...), _M_has_value(false)
1354 template<
typename _Up,
typename... _Args>
1355 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1357 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1358 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1360 : _M_unex(__il,
std::
forward<_Args>(__args)...), _M_has_value(false)
1363 constexpr ~expected() =
default;
1365 constexpr ~expected()
requires (!is_trivially_destructible_v<_Er>)
1368 std::destroy_at(__builtin_addressof(_M_unex));
1373 expected& operator=(
const expected&) =
delete;
1376 operator=(
const expected& __x)
1377 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1378 is_nothrow_copy_assignable<_Er>>)
1379 requires is_copy_constructible_v<_Er>
1380 && is_copy_assignable_v<_Er>
1382 if (__x._M_has_value)
1385 _M_assign_unex(__x._M_unex);
1390 operator=(expected&& __x)
1391 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1392 is_nothrow_move_assignable<_Er>>)
1393 requires is_move_constructible_v<_Er>
1394 && is_move_assignable_v<_Er>
1396 if (__x._M_has_value)
1403 template<
typename _Gr>
1404 requires is_constructible_v<_Er, const _Gr&>
1405 && is_assignable_v<_Er&, const _Gr&>
1407 operator=(
const unexpected<_Gr>& __e)
1409 _M_assign_unex(__e.error());
1413 template<
typename _Gr>
1414 requires is_constructible_v<_Er, _Gr>
1415 && is_assignable_v<_Er&, _Gr>
1417 operator=(unexpected<_Gr>&& __e)
1430 std::destroy_at(__builtin_addressof(_M_unex));
1431 _M_has_value =
true;
1438 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1439 is_nothrow_move_constructible<_Er>>)
1440 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1444 if (!__x._M_has_value)
1446 std::construct_at(__builtin_addressof(_M_unex),
1448 std::destroy_at(__builtin_addressof(__x._M_unex));
1449 _M_has_value =
false;
1450 __x._M_has_value =
true;
1455 if (__x._M_has_value)
1457 std::construct_at(__builtin_addressof(__x._M_unex),
1459 std::destroy_at(__builtin_addressof(_M_unex));
1460 _M_has_value =
true;
1461 __x._M_has_value =
false;
1466 swap(_M_unex, __x._M_unex);
1475 operator bool() const noexcept {
return _M_has_value; }
1478 constexpr bool has_value() const noexcept {
return _M_has_value; }
1481 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1486 if (_M_has_value) [[likely]]
1488 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1494 if (_M_has_value) [[likely]]
1496 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
std::move(_M_unex)));
1499 constexpr const _Er&
1500 error() const & noexcept
1502 __glibcxx_assert(!_M_has_value);
1509 __glibcxx_assert(!_M_has_value);
1513 constexpr const _Er&&
1514 error() const && noexcept
1516 __glibcxx_assert(!_M_has_value);
1523 __glibcxx_assert(!_M_has_value);
1527 template<
typename _Gr = _Er>
1529 error_or(_Gr&& __e)
const&
1531 static_assert( is_copy_constructible_v<_Er> );
1532 static_assert( is_convertible_v<_Gr, _Er> );
1535 return std::forward<_Gr>(__e);
1539 template<
typename _Gr = _Er>
1541 error_or(_Gr&& __e) &&
1543 static_assert( is_move_constructible_v<_Er> );
1544 static_assert( is_convertible_v<_Gr, _Er> );
1547 return std::forward<_Gr>(__e);
1553 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1555 and_then(_Fn&& __f) &
1557 using _Up = __expected::__result0<_Fn>;
1558 static_assert(__expected::__is_expected<_Up>);
1559 static_assert(is_same_v<typename _Up::error_type, _Er>);
1564 return _Up(unexpect, _M_unex);
1567 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1569 and_then(_Fn&& __f)
const &
1571 using _Up = __expected::__result0<_Fn>;
1572 static_assert(__expected::__is_expected<_Up>);
1573 static_assert(is_same_v<typename _Up::error_type, _Er>);
1578 return _Up(unexpect, _M_unex);
1581 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1583 and_then(_Fn&& __f) &&
1585 using _Up = __expected::__result0<_Fn>;
1586 static_assert(__expected::__is_expected<_Up>);
1587 static_assert(is_same_v<typename _Up::error_type, _Er>);
1592 return _Up(unexpect,
std::move(_M_unex));
1595 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1597 and_then(_Fn&& __f)
const &&
1599 using _Up = __expected::__result0<_Fn>;
1600 static_assert(__expected::__is_expected<_Up>);
1601 static_assert(is_same_v<typename _Up::error_type, _Er>);
1606 return _Up(unexpect,
std::move(_M_unex));
1609 template<
typename _Fn>
1611 or_else(_Fn&& __f) &
1613 using _Gr = __expected::__result<_Fn, _Er&>;
1614 static_assert(__expected::__is_expected<_Gr>);
1615 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1623 template<
typename _Fn>
1625 or_else(_Fn&& __f)
const &
1627 using _Gr = __expected::__result<_Fn, const _Er&>;
1628 static_assert(__expected::__is_expected<_Gr>);
1629 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1637 template<
typename _Fn>
1639 or_else(_Fn&& __f) &&
1641 using _Gr = __expected::__result<_Fn, _Er&&>;
1642 static_assert(__expected::__is_expected<_Gr>);
1643 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1651 template<
typename _Fn>
1653 or_else(_Fn&& __f)
const &&
1655 using _Gr = __expected::__result<_Fn, const _Er&&>;
1656 static_assert(__expected::__is_expected<_Gr>);
1657 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1665 template<
typename _Fn>
requires is_constructible_v<_Er, _Er&>
1667 transform(_Fn&& __f) &
1669 using _Up = __expected::__result0_xform<_Fn>;
1670 using _Res = expected<_Up, _Er>;
1673 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1675 return _Res(unexpect, _M_unex);
1678 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er&>
1680 transform(_Fn&& __f)
const &
1682 using _Up = __expected::__result0_xform<_Fn>;
1683 using _Res = expected<_Up, _Er>;
1686 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1688 return _Res(unexpect, _M_unex);
1691 template<
typename _Fn>
requires is_constructible_v<_Er, _Er>
1693 transform(_Fn&& __f) &&
1695 using _Up = __expected::__result0_xform<_Fn>;
1696 using _Res = expected<_Up, _Er>;
1699 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1701 return _Res(unexpect,
std::move(_M_unex));
1704 template<
typename _Fn>
requires is_constructible_v<_Er, const _Er>
1706 transform(_Fn&& __f)
const &&
1708 using _Up = __expected::__result0_xform<_Fn>;
1709 using _Res = expected<_Up, _Er>;
1712 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1714 return _Res(unexpect,
std::move(_M_unex));
1717 template<
typename _Fn>
1719 transform_error(_Fn&& __f) &
1721 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1722 using _Res = expected<_Tp, _Gr>;
1727 return _Res(__unexpect_inv{}, [&]() {
1733 template<
typename _Fn>
1735 transform_error(_Fn&& __f)
const &
1737 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1738 using _Res = expected<_Tp, _Gr>;
1743 return _Res(__unexpect_inv{}, [&]() {
1749 template<
typename _Fn>
1751 transform_error(_Fn&& __f) &&
1753 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1754 using _Res = expected<_Tp, _Gr>;
1759 return _Res(__unexpect_inv{}, [&]() {
1765 template<
typename _Fn>
1767 transform_error(_Fn&& __f)
const &&
1769 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1770 using _Res = expected<_Tp, _Gr>;
1775 return _Res(__unexpect_inv{}, [&]() {
1783 template<
typename _Up,
typename _Er2>
1784 requires is_void_v<_Up>
1785 friend constexpr bool
1786 operator==(
const expected& __x,
const expected<_Up, _Er2>& __y)
1789 if (__x.has_value())
1790 return __y.has_value();
1792 return !__y.has_value() && bool(__x.error() == __y.error());
1795 template<
typename _Er2>
1796 friend constexpr bool
1797 operator==(
const expected& __x,
const unexpected<_Er2>& __e)
1799 {
return !__x.has_value() && bool(__x.error() == __e.error()); }
1801 friend constexpr void
1802 swap(expected& __x, expected& __y)
1803 noexcept(
noexcept(__x.swap(__y)))
1804 requires requires { __x.swap(__y); }
1808 template<
typename,
typename>
friend class expected;
1810 template<
typename _Vp>
1812 _M_assign_unex(_Vp&& __v)
1816 std::construct_at(__builtin_addressof(_M_unex),
1817 std::forward<_Vp>(__v));
1818 _M_has_value =
false;
1821 _M_unex = std::forward<_Vp>(__v);
1824 using __in_place_inv = __expected::__in_place_inv;
1825 using __unexpect_inv = __expected::__unexpect_inv;
1827 template<
typename _Fn>
1829 expected(__in_place_inv, _Fn&& __fn)
1830 : _M_void(), _M_has_value(true)
1831 { std::forward<_Fn>(__fn)(); }
1833 template<
typename _Fn>
1835 expected(__unexpect_inv, _Fn&& __fn)
1836 : _M_unex(
std::
forward<_Fn>(__fn)()), _M_has_value(false)
1848_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.