1// <expected> -*- C++ -*-
3// Copyright The GNU Toolchain Authors.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/expected
26 * This is a Standard C++ Library header.
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
39#include <bits/version.h>
41#ifdef __cpp_lib_expected // C++ >= 23 && __cpp_concepts >= 202002L
42#include <initializer_list>
43#include <bits/exception.h> // exception
44#include <bits/invoke.h> // __invoke
45#include <bits/stl_construct.h> // construct_at
46#include <bits/utility.h> // in_place_t
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup expected_values Expected values
54 * @addtogroup utilities
59 /// Discriminated union that holds an expected value or an error value.
63 template<typename _Tp, typename _Er>
66 /// Wrapper type used to pass an error value to a `std::expected`.
70 template<typename _Er>
73 /// Exception thrown by std::expected when the value() is not present.
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)) { }
105 // XXX const char* what() const noexcept override;
114 error() const & noexcept
120 { return std::move(_M_unex); }
124 error() const && noexcept
125 { return std::move(_M_unex); }
131 /// Tag type for constructing unexpected values in a std::expected
137 explicit unexpect_t() = default;
140 /// Tag for constructing unexpected values in a std::expected
144 inline constexpr unexpect_t unexpect{};
146/// @cond undocumented
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>);
174 // Tag types for in-place construction from an invocation result.
175 struct __in_place_inv { };
176 struct __unexpect_inv { };
180 template<typename _Er>
183 static_assert( __expected::__can_be_unexpected<_Er> );
186 constexpr unexpected(const unexpected&) = default;
187 constexpr unexpected(unexpected&&) = default;
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>
194 unexpected(_Err&& __e)
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...>
202 unexpected(in_place_t, _Args&&... __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)...)
216 constexpr unexpected& operator=(const unexpected&) = default;
217 constexpr unexpected& operator=(unexpected&&) = default;
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
247 operator==(const unexpected& __x, const unexpected<_Err>& __y)
248 { return __x._M_unex == __y.error(); }
250 friend constexpr void
251 swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
252 requires is_swappable_v<_Er>
259 template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
261/// @cond undocumented
264 template<typename _Tp>
267 static_assert( is_nothrow_move_constructible_v<_Tp> );
271 : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
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;
288 return std::move(_M_tmp);
296 // reinit-expected helper from [expected.object.assign]
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);
305 std::construct_at(__newval, std::forward<_Vp>(__arg));
307 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
309 _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
310 std::destroy_at(__oldval);
311 std::construct_at(__newval, std::move(__tmp));
315 _Guard<_Up> __guard(*__oldval);
316 std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
321 // _GLIBCXX_RESOLVE_LIB_DEFECTS
322 // 3836. std::expected<bool, E1> conversion constructor
323 // expected(const expected<U, G>&) should take precedence over
324 // expected(U&&) with operator bool
326 // If T is cv bool, remove_cvref_t<U> is not a specialization of expected.
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> );
344 // If T is not cv bool, converts-from-any-cvref<T, expected<U, G>> and
345 // is_constructible<unexpected<E>, cv expected<U, G> ref-qual> are false.
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>>
363 // _GLIBCXX_RESOLVE_LIB_DEFECTS
364 // If t is cv bool, we know it can be constructed from expected<U, G>,
365 // but we don't want to cause the expected(U&&) constructor to be used,
366 // so we only check the is_constructible<unexpected<E>, ...> cases.
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;
391 using error_type = _Er;
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),
434 std::move(__x)._M_val);
436 std::construct_at(__builtin_addressof(_M_unex),
437 std::move(__x)._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),
468 std::move(__x)._M_val);
470 std::construct_at(__builtin_addressof(_M_unex),
471 std::move(__x)._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)
583 _M_assign_val(std::move(__x._M_val));
585 _M_assign_unex(std::move(__x._M_unex));
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>)
599 _M_assign_val(std::forward<_Up>(__v));
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)
625 _M_assign_unex(std::move(__e).error());
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),
644 std::forward<_Args>(__args)...);
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),
662 __il, std::forward<_Args>(__args)...);
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);
721 operator*() const & noexcept
723 __glibcxx_assert(_M_has_value);
729 operator*() & noexcept
731 __glibcxx_assert(_M_has_value);
736 constexpr const _Tp&&
737 operator*() const && noexcept
739 __glibcxx_assert(_M_has_value);
740 return std::move(_M_val);
745 operator*() && noexcept
747 __glibcxx_assert(_M_has_value);
748 return std::move(_M_val);
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]]
783 return std::move(_M_val);
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]]
793 return std::move(_M_val);
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);
815 return std::move(_M_unex);
821 __glibcxx_assert(!_M_has_value);
822 return std::move(_M_unex);
825 template<typename _Up = remove_cv_t<_Tp>>
827 value_or(_Up&& __v) const &
828 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
829 is_nothrow_convertible<_Up, _Tp>>)
831 static_assert( is_copy_constructible_v<_Tp> );
832 static_assert( is_convertible_v<_Up, _Tp> );
836 return static_cast<_Tp>(std::forward<_Up>(__v));
839 template<typename _Up = remove_cv_t<_Tp>>
841 value_or(_Up&& __v) &&
842 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
843 is_nothrow_convertible<_Up, _Tp>>)
845 static_assert( is_move_constructible_v<_Tp> );
846 static_assert( is_convertible_v<_Up, _Tp> );
849 return std::move(_M_val);
850 return static_cast<_Tp>(std::forward<_Up>(__v));
853 template<typename _Gr = _Er>
855 error_or(_Gr&& __e) const&
857 static_assert( is_copy_constructible_v<_Er> );
858 static_assert( is_convertible_v<_Gr, _Er> );
861 return std::forward<_Gr>(__e);
865 template<typename _Gr = _Er>
867 error_or(_Gr&& __e) &&
869 static_assert( is_move_constructible_v<_Er> );
870 static_assert( is_convertible_v<_Gr, _Er> );
873 return std::forward<_Gr>(__e);
874 return std::move(_M_unex);
877 // monadic operations
879 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
881 and_then(_Fn&& __f) &
883 using _Up = __expected::__result<_Fn, _Tp&>;
884 static_assert(__expected::__is_expected<_Up>,
885 "the function passed to std::expected<T, E>::and_then "
886 "must return a std::expected");
887 static_assert(is_same_v<typename _Up::error_type, _Er>,
888 "the function passed to std::expected<T, E>::and_then "
889 "must return a std::expected with the same error_type");
892 return std::__invoke(std::forward<_Fn>(__f), _M_val);
894 return _Up(unexpect, _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");
910 return std::__invoke(std::forward<_Fn>(__f), _M_val);
912 return _Up(unexpect, _M_unex);
915 template<typename _Fn> requires is_constructible_v<_Er, _Er>
917 and_then(_Fn&& __f) &&
919 using _Up = __expected::__result<_Fn, _Tp&&>;
920 static_assert(__expected::__is_expected<_Up>,
921 "the function passed to std::expected<T, E>::and_then "
922 "must return a std::expected");
923 static_assert(is_same_v<typename _Up::error_type, _Er>,
924 "the function passed to std::expected<T, E>::and_then "
925 "must return a std::expected with the same error_type");
928 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
930 return _Up(unexpect, std::move(_M_unex));
934 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
936 and_then(_Fn&& __f) const &&
938 using _Up = __expected::__result<_Fn, const _Tp&&>;
939 static_assert(__expected::__is_expected<_Up>,
940 "the function passed to std::expected<T, E>::and_then "
941 "must return a std::expected");
942 static_assert(is_same_v<typename _Up::error_type, _Er>,
943 "the function passed to std::expected<T, E>::and_then "
944 "must return a std::expected with the same error_type");
947 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
949 return _Up(unexpect, std::move(_M_unex));
952 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
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");
965 return _Gr(in_place, _M_val);
967 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
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");
983 return _Gr(in_place, _M_val);
985 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
989 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
991 or_else(_Fn&& __f) &&
993 using _Gr = __expected::__result<_Fn, _Er&&>;
994 static_assert(__expected::__is_expected<_Gr>,
995 "the function passed to std::expected<T, E>::or_else "
996 "must return a std::expected");
997 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
998 "the function passed to std::expected<T, E>::or_else "
999 "must return a std::expected with the same value_type");
1002 return _Gr(in_place, std::move(_M_val));
1004 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1007 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1009 or_else(_Fn&& __f) const &&
1011 using _Gr = __expected::__result<_Fn, const _Er&&>;
1012 static_assert(__expected::__is_expected<_Gr>,
1013 "the function passed to std::expected<T, E>::or_else "
1014 "must return a std::expected");
1015 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1016 "the function passed to std::expected<T, E>::or_else "
1017 "must return a std::expected with the same value_type");
1020 return _Gr(in_place, std::move(_M_val));
1022 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1025 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1027 transform(_Fn&& __f) &
1029 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1030 using _Res = expected<_Up, _Er>;
1033 return _Res(__in_place_inv{}, [&]() {
1034 return std::__invoke(std::forward<_Fn>(__f),
1038 return _Res(unexpect, _M_unex);
1041 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1043 transform(_Fn&& __f) const &
1045 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1046 using _Res = expected<_Up, _Er>;
1049 return _Res(__in_place_inv{}, [&]() {
1050 return std::__invoke(std::forward<_Fn>(__f),
1054 return _Res(unexpect, _M_unex);
1057 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1059 transform(_Fn&& __f) &&
1061 using _Up = __expected::__result_xform<_Fn, _Tp>;
1062 using _Res = expected<_Up, _Er>;
1065 return _Res(__in_place_inv{}, [&]() {
1066 return std::__invoke(std::forward<_Fn>(__f),
1070 return _Res(unexpect, std::move(_M_unex));
1073 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1075 transform(_Fn&& __f) const &&
1077 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1078 using _Res = expected<_Up, _Er>;
1081 return _Res(__in_place_inv{}, [&]() {
1082 return std::__invoke(std::forward<_Fn>(__f),
1086 return _Res(unexpect, std::move(_M_unex));
1089 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
1091 transform_error(_Fn&& __f) &
1093 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1094 using _Res = expected<_Tp, _Gr>;
1097 return _Res(in_place, _M_val);
1099 return _Res(__unexpect_inv{}, [&]() {
1100 return std::__invoke(std::forward<_Fn>(__f),
1105 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
1107 transform_error(_Fn&& __f) const &
1109 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1110 using _Res = expected<_Tp, _Gr>;
1113 return _Res(in_place, _M_val);
1115 return _Res(__unexpect_inv{}, [&]() {
1116 return std::__invoke(std::forward<_Fn>(__f),
1121 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
1123 transform_error(_Fn&& __f) &&
1125 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1126 using _Res = expected<_Tp, _Gr>;
1129 return _Res(in_place, std::move(_M_val));
1131 return _Res(__unexpect_inv{}, [&]() {
1132 return std::__invoke(std::forward<_Fn>(__f),
1133 std::move(_M_unex));
1137 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1139 transform_error(_Fn&& __f) const &&
1141 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1142 using _Res = expected<_Tp, _Gr>;
1145 return _Res(in_place, std::move(_M_val));
1147 return _Res(__unexpect_inv{}, [&]() {
1148 return std::__invoke(std::forward<_Fn>(__f),
1149 std::move(_M_unex));
1153 // equality operators
1155 template<typename _Up, typename _Er2>
1156 requires (!is_void_v<_Up>)
1157 && requires (const _Tp& __t, const _Up& __u,
1158 const _Er& __e, const _Er2& __e2) {
1159 { __t == __u } -> convertible_to<bool>;
1160 { __e == __e2 } -> convertible_to<bool>;
1162 friend constexpr bool
1163 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1164 noexcept(noexcept(bool(*__x == *__y))
1165 && noexcept(bool(__x.error() == __y.error())))
1167 if (__x.has_value())
1168 return __y.has_value() && bool(*__x == *__y);
1170 return !__y.has_value() && bool(__x.error() == __y.error());
1173 template<typename _Up, same_as<_Tp> _Vp>
1174 requires (!__expected::__is_expected<_Up>)
1175 && requires (const _Tp& __t, const _Up& __u) {
1176 { __t == __u } -> convertible_to<bool>;
1178 friend constexpr bool
1179 operator==(const expected<_Vp, _Er>& __x, const _Up& __v)
1180 noexcept(noexcept(bool(*__x == __v)))
1181 { return __x.has_value() && bool(*__x == __v); }
1183 template<typename _Er2>
1184 requires requires (const _Er& __e, const _Er2& __e2) {
1185 { __e == __e2 } -> convertible_to<bool>;
1187 friend constexpr bool
1188 operator==(const expected& __x, const unexpected<_Er2>& __e)
1189 noexcept(noexcept(bool(__x.error() == __e.error())))
1190 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1192 friend constexpr void
1193 swap(expected& __x, expected& __y)
1194 noexcept(noexcept(__x.swap(__y)))
1195 requires requires {__x.swap(__y);}
1199 template<typename, typename> friend class expected;
1201 template<typename _Vp>
1203 _M_assign_val(_Vp&& __v)
1206 _M_val = std::forward<_Vp>(__v);
1209 __expected::__reinit(__builtin_addressof(_M_val),
1210 __builtin_addressof(_M_unex),
1211 std::forward<_Vp>(__v));
1212 _M_has_value = true;
1216 template<typename _Vp>
1218 _M_assign_unex(_Vp&& __v)
1222 __expected::__reinit(__builtin_addressof(_M_unex),
1223 __builtin_addressof(_M_val),
1224 std::forward<_Vp>(__v));
1225 _M_has_value = false;
1228 _M_unex = std::forward<_Vp>(__v);
1231 // Swap two expected objects when only one has a value.
1232 // Precondition: this->_M_has_value && !__rhs._M_has_value
1234 _M_swap_val_unex(expected& __rhs)
1235 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1236 is_nothrow_move_constructible<_Tp>>)
1238 if constexpr (is_nothrow_move_constructible_v<_Er>)
1240 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1241 std::construct_at(__builtin_addressof(__rhs._M_val),
1242 std::move(_M_val)); // might throw
1243 __rhs._M_has_value = true;
1244 std::destroy_at(__builtin_addressof(_M_val));
1245 std::construct_at(__builtin_addressof(_M_unex),
1247 _M_has_value = false;
1251 __expected::_Guard<_Tp> __guard(_M_val);
1252 std::construct_at(__builtin_addressof(_M_unex),
1253 std::move(__rhs._M_unex)); // might throw
1254 _M_has_value = false;
1255 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1256 std::construct_at(__builtin_addressof(__rhs._M_val),
1258 __rhs._M_has_value = true;
1262 using __in_place_inv = __expected::__in_place_inv;
1263 using __unexpect_inv = __expected::__unexpect_inv;
1265 template<typename _Fn>
1267 expected(__in_place_inv, _Fn&& __fn)
1268 : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
1271 template<typename _Fn>
1273 expected(__unexpect_inv, _Fn&& __fn)
1274 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1278 remove_cv_t<_Tp> _M_val;
1285 // Partial specialization for std::expected<cv void, E>
1286 template<typename _Tp, typename _Er> requires is_void_v<_Tp>
1287 class expected<_Tp, _Er>
1289 static_assert( __expected::__can_be_unexpected<_Er> );
1291 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
1292 static constexpr bool __cons_from_expected
1293 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1294 is_constructible<_Unex, expected<_Up, _Err>>,
1295 is_constructible<_Unex, const expected<_Up, _Err>&>,
1296 is_constructible<_Unex, const expected<_Up, _Err>>
1299 template<typename _Up>
1300 static constexpr bool __same_val
1301 = is_same_v<typename _Up::value_type, _Tp>;
1303 template<typename _Up>
1304 static constexpr bool __same_err
1305 = is_same_v<typename _Up::error_type, _Er>;
1308 using value_type = _Tp;
1309 using error_type = _Er;
1310 using unexpected_type = unexpected<_Er>;
1312 template<typename _Up>
1313 using rebind = expected<_Up, error_type>;
1317 : _M_void(), _M_has_value(true)
1320 expected(const expected&) = default;
1323 expected(const expected& __x)
1324 noexcept(is_nothrow_copy_constructible_v<_Er>)
1325 requires is_copy_constructible_v<_Er>
1326 && (!is_trivially_copy_constructible_v<_Er>)
1327 : _M_void(), _M_has_value(__x._M_has_value)
1330 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1333 expected(expected&&) = default;
1336 expected(expected&& __x)
1337 noexcept(is_nothrow_move_constructible_v<_Er>)
1338 requires is_move_constructible_v<_Er>
1339 && (!is_trivially_move_constructible_v<_Er>)
1340 : _M_void(), _M_has_value(__x._M_has_value)
1343 std::construct_at(__builtin_addressof(_M_unex),
1344 std::move(__x)._M_unex);
1347 template<typename _Up, typename _Gr>
1348 requires is_void_v<_Up>
1349 && is_constructible_v<_Er, const _Gr&>
1350 && (!__cons_from_expected<_Up, _Gr>)
1351 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1352 expected(const expected<_Up, _Gr>& __x)
1353 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1354 : _M_void(), _M_has_value(__x._M_has_value)
1357 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1360 template<typename _Up, typename _Gr>
1361 requires is_void_v<_Up>
1362 && is_constructible_v<_Er, _Gr>
1363 && (!__cons_from_expected<_Up, _Gr>)
1364 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1365 expected(expected<_Up, _Gr>&& __x)
1366 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1367 : _M_void(), _M_has_value(__x._M_has_value)
1370 std::construct_at(__builtin_addressof(_M_unex),
1371 std::move(__x)._M_unex);
1374 template<typename _Gr = _Er>
1375 requires is_constructible_v<_Er, const _Gr&>
1376 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1377 expected(const unexpected<_Gr>& __u)
1378 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1379 : _M_unex(__u.error()), _M_has_value(false)
1382 template<typename _Gr = _Er>
1383 requires is_constructible_v<_Er, _Gr>
1384 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1385 expected(unexpected<_Gr>&& __u)
1386 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1387 : _M_unex(std::move(__u).error()), _M_has_value(false)
1391 expected(in_place_t) noexcept
1395 template<typename... _Args>
1396 requires is_constructible_v<_Er, _Args...>
1398 expected(unexpect_t, _Args&&... __args)
1399 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1400 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
1403 template<typename _Up, typename... _Args>
1404 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1406 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1407 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1409 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
1412 constexpr ~expected() = default;
1414 constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
1417 std::destroy_at(__builtin_addressof(_M_unex));
1422 expected& operator=(const expected&) = delete;
1425 operator=(const expected& __x)
1426 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1427 is_nothrow_copy_assignable<_Er>>)
1428 requires is_copy_constructible_v<_Er>
1429 && is_copy_assignable_v<_Er>
1431 if (__x._M_has_value)
1434 _M_assign_unex(__x._M_unex);
1439 operator=(expected&& __x)
1440 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1441 is_nothrow_move_assignable<_Er>>)
1442 requires is_move_constructible_v<_Er>
1443 && is_move_assignable_v<_Er>
1445 if (__x._M_has_value)
1448 _M_assign_unex(std::move(__x._M_unex));
1452 template<typename _Gr>
1453 requires is_constructible_v<_Er, const _Gr&>
1454 && is_assignable_v<_Er&, const _Gr&>
1456 operator=(const unexpected<_Gr>& __e)
1458 _M_assign_unex(__e.error());
1462 template<typename _Gr>
1463 requires is_constructible_v<_Er, _Gr>
1464 && is_assignable_v<_Er&, _Gr>
1466 operator=(unexpected<_Gr>&& __e)
1468 _M_assign_unex(std::move(__e.error()));
1479 std::destroy_at(__builtin_addressof(_M_unex));
1480 _M_has_value = true;
1487 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1488 is_nothrow_move_constructible<_Er>>)
1489 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1493 if (!__x._M_has_value)
1495 std::construct_at(__builtin_addressof(_M_unex),
1496 std::move(__x._M_unex)); // might throw
1497 std::destroy_at(__builtin_addressof(__x._M_unex));
1498 _M_has_value = false;
1499 __x._M_has_value = true;
1504 if (__x._M_has_value)
1506 std::construct_at(__builtin_addressof(__x._M_unex),
1507 std::move(_M_unex)); // might throw
1508 std::destroy_at(__builtin_addressof(_M_unex));
1509 _M_has_value = true;
1510 __x._M_has_value = false;
1515 swap(_M_unex, __x._M_unex);
1524 operator bool() const noexcept { return _M_has_value; }
1527 constexpr bool has_value() const noexcept { return _M_has_value; }
1530 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1535 static_assert( is_copy_constructible_v<_Er> );
1536 if (_M_has_value) [[likely]]
1538 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1544 static_assert( is_copy_constructible_v<_Er> );
1545 static_assert( is_move_constructible_v<_Er> );
1546 if (_M_has_value) [[likely]]
1548 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
1551 constexpr const _Er&
1552 error() const & noexcept
1554 __glibcxx_assert(!_M_has_value);
1561 __glibcxx_assert(!_M_has_value);
1565 constexpr const _Er&&
1566 error() const && noexcept
1568 __glibcxx_assert(!_M_has_value);
1569 return std::move(_M_unex);
1575 __glibcxx_assert(!_M_has_value);
1576 return std::move(_M_unex);
1579 template<typename _Gr = _Er>
1581 error_or(_Gr&& __e) const&
1583 static_assert( is_copy_constructible_v<_Er> );
1584 static_assert( is_convertible_v<_Gr, _Er> );
1587 return std::forward<_Gr>(__e);
1591 template<typename _Gr = _Er>
1593 error_or(_Gr&& __e) &&
1595 static_assert( is_move_constructible_v<_Er> );
1596 static_assert( is_convertible_v<_Gr, _Er> );
1599 return std::forward<_Gr>(__e);
1600 return std::move(_M_unex);
1603 // monadic operations
1605 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1607 and_then(_Fn&& __f) &
1609 using _Up = __expected::__result0<_Fn>;
1610 static_assert(__expected::__is_expected<_Up>);
1611 static_assert(is_same_v<typename _Up::error_type, _Er>);
1614 return std::__invoke(std::forward<_Fn>(__f));
1616 return _Up(unexpect, _M_unex);
1619 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1621 and_then(_Fn&& __f) const &
1623 using _Up = __expected::__result0<_Fn>;
1624 static_assert(__expected::__is_expected<_Up>);
1625 static_assert(is_same_v<typename _Up::error_type, _Er>);
1628 return std::__invoke(std::forward<_Fn>(__f));
1630 return _Up(unexpect, _M_unex);
1633 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1635 and_then(_Fn&& __f) &&
1637 using _Up = __expected::__result0<_Fn>;
1638 static_assert(__expected::__is_expected<_Up>);
1639 static_assert(is_same_v<typename _Up::error_type, _Er>);
1642 return std::__invoke(std::forward<_Fn>(__f));
1644 return _Up(unexpect, std::move(_M_unex));
1647 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1649 and_then(_Fn&& __f) const &&
1651 using _Up = __expected::__result0<_Fn>;
1652 static_assert(__expected::__is_expected<_Up>);
1653 static_assert(is_same_v<typename _Up::error_type, _Er>);
1656 return std::__invoke(std::forward<_Fn>(__f));
1658 return _Up(unexpect, std::move(_M_unex));
1661 template<typename _Fn>
1663 or_else(_Fn&& __f) &
1665 using _Gr = __expected::__result<_Fn, _Er&>;
1666 static_assert(__expected::__is_expected<_Gr>);
1667 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1672 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1675 template<typename _Fn>
1677 or_else(_Fn&& __f) const &
1679 using _Gr = __expected::__result<_Fn, const _Er&>;
1680 static_assert(__expected::__is_expected<_Gr>);
1681 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1686 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1689 template<typename _Fn>
1691 or_else(_Fn&& __f) &&
1693 using _Gr = __expected::__result<_Fn, _Er&&>;
1694 static_assert(__expected::__is_expected<_Gr>);
1695 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1700 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1703 template<typename _Fn>
1705 or_else(_Fn&& __f) const &&
1707 using _Gr = __expected::__result<_Fn, const _Er&&>;
1708 static_assert(__expected::__is_expected<_Gr>);
1709 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1714 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1717 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1719 transform(_Fn&& __f) &
1721 using _Up = __expected::__result0_xform<_Fn>;
1722 using _Res = expected<_Up, _Er>;
1725 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1727 return _Res(unexpect, _M_unex);
1730 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1732 transform(_Fn&& __f) const &
1734 using _Up = __expected::__result0_xform<_Fn>;
1735 using _Res = expected<_Up, _Er>;
1738 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1740 return _Res(unexpect, _M_unex);
1743 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1745 transform(_Fn&& __f) &&
1747 using _Up = __expected::__result0_xform<_Fn>;
1748 using _Res = expected<_Up, _Er>;
1751 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1753 return _Res(unexpect, std::move(_M_unex));
1756 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1758 transform(_Fn&& __f) const &&
1760 using _Up = __expected::__result0_xform<_Fn>;
1761 using _Res = expected<_Up, _Er>;
1764 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1766 return _Res(unexpect, std::move(_M_unex));
1769 template<typename _Fn>
1771 transform_error(_Fn&& __f) &
1773 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1774 using _Res = expected<_Tp, _Gr>;
1779 return _Res(__unexpect_inv{}, [&]() {
1780 return std::__invoke(std::forward<_Fn>(__f),
1785 template<typename _Fn>
1787 transform_error(_Fn&& __f) const &
1789 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1790 using _Res = expected<_Tp, _Gr>;
1795 return _Res(__unexpect_inv{}, [&]() {
1796 return std::__invoke(std::forward<_Fn>(__f),
1801 template<typename _Fn>
1803 transform_error(_Fn&& __f) &&
1805 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1806 using _Res = expected<_Tp, _Gr>;
1811 return _Res(__unexpect_inv{}, [&]() {
1812 return std::__invoke(std::forward<_Fn>(__f),
1813 std::move(_M_unex));
1817 template<typename _Fn>
1819 transform_error(_Fn&& __f) const &&
1821 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1822 using _Res = expected<_Tp, _Gr>;
1827 return _Res(__unexpect_inv{}, [&]() {
1828 return std::__invoke(std::forward<_Fn>(__f),
1829 std::move(_M_unex));
1833 // equality operators
1835 template<typename _Up, typename _Er2>
1836 requires is_void_v<_Up>
1837 && requires (const _Er& __e, const _Er2& __e2) {
1838 { __e == __e2 } -> convertible_to<bool>;
1840 friend constexpr bool
1841 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1842 noexcept(noexcept(bool(__x.error() == __y.error())))
1844 if (__x.has_value())
1845 return __y.has_value();
1847 return !__y.has_value() && bool(__x.error() == __y.error());
1850 template<typename _Er2>
1851 requires requires (const _Er& __e, const _Er2& __e2) {
1852 { __e == __e2 } -> convertible_to<bool>;
1854 friend constexpr bool
1855 operator==(const expected& __x, const unexpected<_Er2>& __e)
1856 noexcept(noexcept(bool(__x.error() == __e.error())))
1857 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1859 friend constexpr void
1860 swap(expected& __x, expected& __y)
1861 noexcept(noexcept(__x.swap(__y)))
1862 requires requires { __x.swap(__y); }
1866 template<typename, typename> friend class expected;
1868 template<typename _Vp>
1870 _M_assign_unex(_Vp&& __v)
1874 std::construct_at(__builtin_addressof(_M_unex),
1875 std::forward<_Vp>(__v));
1876 _M_has_value = false;
1879 _M_unex = std::forward<_Vp>(__v);
1882 using __in_place_inv = __expected::__in_place_inv;
1883 using __unexpect_inv = __expected::__unexpect_inv;
1885 template<typename _Fn>
1887 expected(__in_place_inv, _Fn&& __fn)
1888 : _M_void(), _M_has_value(true)
1889 { std::forward<_Fn>(__fn)(); }
1891 template<typename _Fn>
1893 expected(__unexpect_inv, _Fn&& __fn)
1894 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1906_GLIBCXX_END_NAMESPACE_VERSION
1909#endif // __cpp_lib_expected
1910#endif // _GLIBCXX_EXPECTED