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_constructible_v<_Tp, _Up>
478 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
479 && __expected::__not_constructing_bool_from_expected<_Tp, _Up>
480 constexpr explicit(!is_convertible_v<_Up, _Tp>)
482 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
483 : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
486 template<typename _Gr = _Er>
487 requires is_constructible_v<_Er, const _Gr&>
488 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
489 expected(const unexpected<_Gr>& __u)
490 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
491 : _M_unex(__u.error()), _M_has_value(false)
494 template<typename _Gr = _Er>
495 requires is_constructible_v<_Er, _Gr>
496 constexpr explicit(!is_convertible_v<_Gr, _Er>)
497 expected(unexpected<_Gr>&& __u)
498 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
499 : _M_unex(std::move(__u).error()), _M_has_value(false)
502 template<typename... _Args>
503 requires is_constructible_v<_Tp, _Args...>
505 expected(in_place_t, _Args&&... __args)
506 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
507 : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
510 template<typename _Up, typename... _Args>
511 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
513 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
514 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
516 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
519 template<typename... _Args>
520 requires is_constructible_v<_Er, _Args...>
522 expected(unexpect_t, _Args&&... __args)
523 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
524 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
527 template<typename _Up, typename... _Args>
528 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
530 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
531 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
533 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
536 constexpr ~expected() = default;
538 constexpr ~expected()
539 requires (!is_trivially_destructible_v<_Tp>)
540 || (!is_trivially_destructible_v<_Er>)
543 std::destroy_at(__builtin_addressof(_M_val));
545 std::destroy_at(__builtin_addressof(_M_unex));
550 expected& operator=(const expected&) = delete;
553 operator=(const expected& __x)
554 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
555 is_nothrow_copy_constructible<_Er>,
556 is_nothrow_copy_assignable<_Tp>,
557 is_nothrow_copy_assignable<_Er>>)
558 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
559 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
560 && (is_nothrow_move_constructible_v<_Tp>
561 || is_nothrow_move_constructible_v<_Er>)
563 if (__x._M_has_value)
564 this->_M_assign_val(__x._M_val);
566 this->_M_assign_unex(__x._M_unex);
571 operator=(expected&& __x)
572 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
573 is_nothrow_move_constructible<_Er>,
574 is_nothrow_move_assignable<_Tp>,
575 is_nothrow_move_assignable<_Er>>)
576 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
577 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
578 && (is_nothrow_move_constructible_v<_Tp>
579 || is_nothrow_move_constructible_v<_Er>)
581 if (__x._M_has_value)
582 _M_assign_val(std::move(__x._M_val));
584 _M_assign_unex(std::move(__x._M_unex));
588 template<typename _Up = remove_cv_t<_Tp>>
589 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
590 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
591 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
592 && (is_nothrow_constructible_v<_Tp, _Up>
593 || is_nothrow_move_constructible_v<_Tp>
594 || is_nothrow_move_constructible_v<_Er>)
598 _M_assign_val(std::forward<_Up>(__v));
602 template<typename _Gr>
603 requires is_constructible_v<_Er, const _Gr&>
604 && is_assignable_v<_Er&, const _Gr&>
605 && (is_nothrow_constructible_v<_Er, const _Gr&>
606 || is_nothrow_move_constructible_v<_Tp>
607 || is_nothrow_move_constructible_v<_Er>)
609 operator=(const unexpected<_Gr>& __e)
611 _M_assign_unex(__e.error());
615 template<typename _Gr>
616 requires is_constructible_v<_Er, _Gr>
617 && is_assignable_v<_Er&, _Gr>
618 && (is_nothrow_constructible_v<_Er, _Gr>
619 || is_nothrow_move_constructible_v<_Tp>
620 || is_nothrow_move_constructible_v<_Er>)
622 operator=(unexpected<_Gr>&& __e)
624 _M_assign_unex(std::move(__e).error());
630 template<typename... _Args>
631 requires is_nothrow_constructible_v<_Tp, _Args...>
633 emplace(_Args&&... __args) noexcept
636 std::destroy_at(__builtin_addressof(_M_val));
639 std::destroy_at(__builtin_addressof(_M_unex));
642 std::construct_at(__builtin_addressof(_M_val),
643 std::forward<_Args>(__args)...);
647 template<typename _Up, typename... _Args>
648 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
651 emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
654 std::destroy_at(__builtin_addressof(_M_val));
657 std::destroy_at(__builtin_addressof(_M_unex));
660 std::construct_at(__builtin_addressof(_M_val),
661 __il, std::forward<_Args>(__args)...);
668 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
669 is_nothrow_move_constructible<_Er>,
670 is_nothrow_swappable<_Tp&>,
671 is_nothrow_swappable<_Er&>>)
672 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
673 && is_move_constructible_v<_Tp>
674 && is_move_constructible_v<_Er>
675 && (is_nothrow_move_constructible_v<_Tp>
676 || is_nothrow_move_constructible_v<_Er>)
680 if (__x._M_has_value)
683 swap(_M_val, __x._M_val);
686 this->_M_swap_val_unex(__x);
690 if (__x._M_has_value)
691 __x._M_swap_val_unex(*this);
695 swap(_M_unex, __x._M_unex);
704 operator->() const noexcept
706 __glibcxx_assert(_M_has_value);
707 return __builtin_addressof(_M_val);
712 operator->() noexcept
714 __glibcxx_assert(_M_has_value);
715 return __builtin_addressof(_M_val);
720 operator*() const & noexcept
722 __glibcxx_assert(_M_has_value);
728 operator*() & noexcept
730 __glibcxx_assert(_M_has_value);
735 constexpr const _Tp&&
736 operator*() const && noexcept
738 __glibcxx_assert(_M_has_value);
739 return std::move(_M_val);
744 operator*() && noexcept
746 __glibcxx_assert(_M_has_value);
747 return std::move(_M_val);
752 operator bool() const noexcept { return _M_has_value; }
755 constexpr bool has_value() const noexcept { return _M_has_value; }
760 static_assert( is_copy_constructible_v<_Er> );
761 if (_M_has_value) [[likely]]
763 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
769 static_assert( is_copy_constructible_v<_Er> );
770 if (_M_has_value) [[likely]]
772 const auto& __unex = _M_unex;
773 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
776 constexpr const _Tp&&
779 static_assert( is_copy_constructible_v<_Er> );
780 static_assert( is_constructible_v<_Er, const _Er&&> );
781 if (_M_has_value) [[likely]]
782 return std::move(_M_val);
783 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
789 static_assert( is_copy_constructible_v<_Er> );
790 static_assert( is_constructible_v<_Er, _Er&&> );
791 if (_M_has_value) [[likely]]
792 return std::move(_M_val);
793 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
797 error() const & noexcept
799 __glibcxx_assert(!_M_has_value);
806 __glibcxx_assert(!_M_has_value);
810 constexpr const _Er&&
811 error() const && noexcept
813 __glibcxx_assert(!_M_has_value);
814 return std::move(_M_unex);
820 __glibcxx_assert(!_M_has_value);
821 return std::move(_M_unex);
824 template<typename _Up = remove_cv_t<_Tp>>
826 value_or(_Up&& __v) const &
827 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
828 is_nothrow_convertible<_Up, _Tp>>)
830 static_assert( is_copy_constructible_v<_Tp> );
831 static_assert( is_convertible_v<_Up, _Tp> );
835 return static_cast<_Tp>(std::forward<_Up>(__v));
838 template<typename _Up = remove_cv_t<_Tp>>
840 value_or(_Up&& __v) &&
841 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
842 is_nothrow_convertible<_Up, _Tp>>)
844 static_assert( is_move_constructible_v<_Tp> );
845 static_assert( is_convertible_v<_Up, _Tp> );
848 return std::move(_M_val);
849 return static_cast<_Tp>(std::forward<_Up>(__v));
852 template<typename _Gr = _Er>
854 error_or(_Gr&& __e) const&
856 static_assert( is_copy_constructible_v<_Er> );
857 static_assert( is_convertible_v<_Gr, _Er> );
860 return std::forward<_Gr>(__e);
864 template<typename _Gr = _Er>
866 error_or(_Gr&& __e) &&
868 static_assert( is_move_constructible_v<_Er> );
869 static_assert( is_convertible_v<_Gr, _Er> );
872 return std::forward<_Gr>(__e);
873 return std::move(_M_unex);
876 // monadic operations
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");
891 return std::__invoke(std::forward<_Fn>(__f), _M_val);
893 return _Up(unexpect, _M_unex);
896 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
898 and_then(_Fn&& __f) const &
900 using _Up = __expected::__result<_Fn, const _Tp&>;
901 static_assert(__expected::__is_expected<_Up>,
902 "the function passed to std::expected<T, E>::and_then "
903 "must return a std::expected");
904 static_assert(is_same_v<typename _Up::error_type, _Er>,
905 "the function passed to std::expected<T, E>::and_then "
906 "must return a std::expected with the same error_type");
909 return std::__invoke(std::forward<_Fn>(__f), _M_val);
911 return _Up(unexpect, _M_unex);
914 template<typename _Fn> requires is_constructible_v<_Er, _Er>
916 and_then(_Fn&& __f) &&
918 using _Up = __expected::__result<_Fn, _Tp&&>;
919 static_assert(__expected::__is_expected<_Up>,
920 "the function passed to std::expected<T, E>::and_then "
921 "must return a std::expected");
922 static_assert(is_same_v<typename _Up::error_type, _Er>,
923 "the function passed to std::expected<T, E>::and_then "
924 "must return a std::expected with the same error_type");
927 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
929 return _Up(unexpect, std::move(_M_unex));
933 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
935 and_then(_Fn&& __f) const &&
937 using _Up = __expected::__result<_Fn, const _Tp&&>;
938 static_assert(__expected::__is_expected<_Up>,
939 "the function passed to std::expected<T, E>::and_then "
940 "must return a std::expected");
941 static_assert(is_same_v<typename _Up::error_type, _Er>,
942 "the function passed to std::expected<T, E>::and_then "
943 "must return a std::expected with the same error_type");
946 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
948 return _Up(unexpect, std::move(_M_unex));
951 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
955 using _Gr = __expected::__result<_Fn, _Er&>;
956 static_assert(__expected::__is_expected<_Gr>,
957 "the function passed to std::expected<T, E>::or_else "
958 "must return a std::expected");
959 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
960 "the function passed to std::expected<T, E>::or_else "
961 "must return a std::expected with the same value_type");
964 return _Gr(in_place, _M_val);
966 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
969 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
971 or_else(_Fn&& __f) const &
973 using _Gr = __expected::__result<_Fn, const _Er&>;
974 static_assert(__expected::__is_expected<_Gr>,
975 "the function passed to std::expected<T, E>::or_else "
976 "must return a std::expected");
977 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
978 "the function passed to std::expected<T, E>::or_else "
979 "must return a std::expected with the same value_type");
982 return _Gr(in_place, _M_val);
984 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
988 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
990 or_else(_Fn&& __f) &&
992 using _Gr = __expected::__result<_Fn, _Er&&>;
993 static_assert(__expected::__is_expected<_Gr>,
994 "the function passed to std::expected<T, E>::or_else "
995 "must return a std::expected");
996 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
997 "the function passed to std::expected<T, E>::or_else "
998 "must return a std::expected with the same value_type");
1001 return _Gr(in_place, std::move(_M_val));
1003 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1006 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1008 or_else(_Fn&& __f) const &&
1010 using _Gr = __expected::__result<_Fn, const _Er&&>;
1011 static_assert(__expected::__is_expected<_Gr>,
1012 "the function passed to std::expected<T, E>::or_else "
1013 "must return a std::expected");
1014 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
1015 "the function passed to std::expected<T, E>::or_else "
1016 "must return a std::expected with the same value_type");
1019 return _Gr(in_place, std::move(_M_val));
1021 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1024 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1026 transform(_Fn&& __f) &
1028 using _Up = __expected::__result_xform<_Fn, _Tp&>;
1029 using _Res = expected<_Up, _Er>;
1032 return _Res(__in_place_inv{}, [&]() {
1033 return std::__invoke(std::forward<_Fn>(__f),
1037 return _Res(unexpect, _M_unex);
1040 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1042 transform(_Fn&& __f) const &
1044 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1045 using _Res = expected<_Up, _Er>;
1048 return _Res(__in_place_inv{}, [&]() {
1049 return std::__invoke(std::forward<_Fn>(__f),
1053 return _Res(unexpect, _M_unex);
1056 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1058 transform(_Fn&& __f) &&
1060 using _Up = __expected::__result_xform<_Fn, _Tp>;
1061 using _Res = expected<_Up, _Er>;
1064 return _Res(__in_place_inv{}, [&]() {
1065 return std::__invoke(std::forward<_Fn>(__f),
1069 return _Res(unexpect, std::move(_M_unex));
1072 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1074 transform(_Fn&& __f) const &&
1076 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1077 using _Res = expected<_Up, _Er>;
1080 return _Res(__in_place_inv{}, [&]() {
1081 return std::__invoke(std::forward<_Fn>(__f),
1085 return _Res(unexpect, std::move(_M_unex));
1088 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
1090 transform_error(_Fn&& __f) &
1092 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1093 using _Res = expected<_Tp, _Gr>;
1096 return _Res(in_place, _M_val);
1098 return _Res(__unexpect_inv{}, [&]() {
1099 return std::__invoke(std::forward<_Fn>(__f),
1104 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
1106 transform_error(_Fn&& __f) const &
1108 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1109 using _Res = expected<_Tp, _Gr>;
1112 return _Res(in_place, _M_val);
1114 return _Res(__unexpect_inv{}, [&]() {
1115 return std::__invoke(std::forward<_Fn>(__f),
1120 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
1122 transform_error(_Fn&& __f) &&
1124 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1125 using _Res = expected<_Tp, _Gr>;
1128 return _Res(in_place, std::move(_M_val));
1130 return _Res(__unexpect_inv{}, [&]() {
1131 return std::__invoke(std::forward<_Fn>(__f),
1132 std::move(_M_unex));
1136 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1138 transform_error(_Fn&& __f) const &&
1140 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1141 using _Res = expected<_Tp, _Gr>;
1144 return _Res(in_place, std::move(_M_val));
1146 return _Res(__unexpect_inv{}, [&]() {
1147 return std::__invoke(std::forward<_Fn>(__f),
1148 std::move(_M_unex));
1152 // equality operators
1154 template<typename _Up, typename _Er2>
1155 requires (!is_void_v<_Up>)
1156 && requires (const _Tp& __t, const _Up& __u,
1157 const _Er& __e, const _Er2& __e2) {
1158 { __t == __u } -> convertible_to<bool>;
1159 { __e == __e2 } -> convertible_to<bool>;
1161 friend constexpr bool
1162 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1163 noexcept(noexcept(bool(*__x == *__y))
1164 && noexcept(bool(__x.error() == __y.error())))
1166 if (__x.has_value())
1167 return __y.has_value() && bool(*__x == *__y);
1169 return !__y.has_value() && bool(__x.error() == __y.error());
1172 template<typename _Up>
1173 requires (!__expected::__is_expected<_Up>)
1174 && requires (const _Tp& __t, const _Up& __u) {
1175 { __t == __u } -> convertible_to<bool>;
1177 friend constexpr bool
1178 operator==(const expected& __x, const _Up& __v)
1179 noexcept(noexcept(bool(*__x == __v)))
1180 { return __x.has_value() && bool(*__x == __v); }
1182 template<typename _Er2>
1183 requires requires (const _Er& __e, const _Er2& __e2) {
1184 { __e == __e2 } -> convertible_to<bool>;
1186 friend constexpr bool
1187 operator==(const expected& __x, const unexpected<_Er2>& __e)
1188 noexcept(noexcept(bool(__x.error() == __e.error())))
1189 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1191 friend constexpr void
1192 swap(expected& __x, expected& __y)
1193 noexcept(noexcept(__x.swap(__y)))
1194 requires requires {__x.swap(__y);}
1198 template<typename, typename> friend class expected;
1200 template<typename _Vp>
1202 _M_assign_val(_Vp&& __v)
1205 _M_val = std::forward<_Vp>(__v);
1208 __expected::__reinit(__builtin_addressof(_M_val),
1209 __builtin_addressof(_M_unex),
1210 std::forward<_Vp>(__v));
1211 _M_has_value = true;
1215 template<typename _Vp>
1217 _M_assign_unex(_Vp&& __v)
1221 __expected::__reinit(__builtin_addressof(_M_unex),
1222 __builtin_addressof(_M_val),
1223 std::forward<_Vp>(__v));
1224 _M_has_value = false;
1227 _M_unex = std::forward<_Vp>(__v);
1230 // Swap two expected objects when only one has a value.
1231 // Precondition: this->_M_has_value && !__rhs._M_has_value
1233 _M_swap_val_unex(expected& __rhs)
1234 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1235 is_nothrow_move_constructible<_Tp>>)
1237 if constexpr (is_nothrow_move_constructible_v<_Er>)
1239 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1240 std::construct_at(__builtin_addressof(__rhs._M_val),
1241 std::move(_M_val)); // might throw
1242 __rhs._M_has_value = true;
1243 std::destroy_at(__builtin_addressof(_M_val));
1244 std::construct_at(__builtin_addressof(_M_unex),
1246 _M_has_value = false;
1250 __expected::_Guard<_Tp> __guard(_M_val);
1251 std::construct_at(__builtin_addressof(_M_unex),
1252 std::move(__rhs._M_unex)); // might throw
1253 _M_has_value = false;
1254 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1255 std::construct_at(__builtin_addressof(__rhs._M_val),
1257 __rhs._M_has_value = true;
1261 using __in_place_inv = __expected::__in_place_inv;
1262 using __unexpect_inv = __expected::__unexpect_inv;
1264 template<typename _Fn>
1266 expected(__in_place_inv, _Fn&& __fn)
1267 : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
1270 template<typename _Fn>
1272 expected(__unexpect_inv, _Fn&& __fn)
1273 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1277 remove_cv_t<_Tp> _M_val;
1284 // Partial specialization for std::expected<cv void, E>
1285 template<typename _Tp, typename _Er> requires is_void_v<_Tp>
1286 class expected<_Tp, _Er>
1288 static_assert( __expected::__can_be_unexpected<_Er> );
1290 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
1291 static constexpr bool __cons_from_expected
1292 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1293 is_constructible<_Unex, expected<_Up, _Err>>,
1294 is_constructible<_Unex, const expected<_Up, _Err>&>,
1295 is_constructible<_Unex, const expected<_Up, _Err>>
1298 template<typename _Up>
1299 static constexpr bool __same_val
1300 = is_same_v<typename _Up::value_type, _Tp>;
1302 template<typename _Up>
1303 static constexpr bool __same_err
1304 = is_same_v<typename _Up::error_type, _Er>;
1307 using value_type = _Tp;
1308 using error_type = _Er;
1309 using unexpected_type = unexpected<_Er>;
1311 template<typename _Up>
1312 using rebind = expected<_Up, error_type>;
1316 : _M_void(), _M_has_value(true)
1319 expected(const expected&) = default;
1322 expected(const expected& __x)
1323 noexcept(is_nothrow_copy_constructible_v<_Er>)
1324 requires is_copy_constructible_v<_Er>
1325 && (!is_trivially_copy_constructible_v<_Er>)
1326 : _M_void(), _M_has_value(__x._M_has_value)
1329 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1332 expected(expected&&) = default;
1335 expected(expected&& __x)
1336 noexcept(is_nothrow_move_constructible_v<_Er>)
1337 requires is_move_constructible_v<_Er>
1338 && (!is_trivially_move_constructible_v<_Er>)
1339 : _M_void(), _M_has_value(__x._M_has_value)
1342 std::construct_at(__builtin_addressof(_M_unex),
1343 std::move(__x)._M_unex);
1346 template<typename _Up, typename _Gr>
1347 requires is_void_v<_Up>
1348 && is_constructible_v<_Er, const _Gr&>
1349 && (!__cons_from_expected<_Up, _Gr>)
1350 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1351 expected(const expected<_Up, _Gr>& __x)
1352 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1353 : _M_void(), _M_has_value(__x._M_has_value)
1356 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1359 template<typename _Up, typename _Gr>
1360 requires is_void_v<_Up>
1361 && is_constructible_v<_Er, _Gr>
1362 && (!__cons_from_expected<_Up, _Gr>)
1363 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1364 expected(expected<_Up, _Gr>&& __x)
1365 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1366 : _M_void(), _M_has_value(__x._M_has_value)
1369 std::construct_at(__builtin_addressof(_M_unex),
1370 std::move(__x)._M_unex);
1373 template<typename _Gr = _Er>
1374 requires is_constructible_v<_Er, const _Gr&>
1375 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1376 expected(const unexpected<_Gr>& __u)
1377 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1378 : _M_unex(__u.error()), _M_has_value(false)
1381 template<typename _Gr = _Er>
1382 requires is_constructible_v<_Er, _Gr>
1383 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1384 expected(unexpected<_Gr>&& __u)
1385 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1386 : _M_unex(std::move(__u).error()), _M_has_value(false)
1390 expected(in_place_t) noexcept
1394 template<typename... _Args>
1395 requires is_constructible_v<_Er, _Args...>
1397 expected(unexpect_t, _Args&&... __args)
1398 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1399 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
1402 template<typename _Up, typename... _Args>
1403 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1405 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1406 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1408 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
1411 constexpr ~expected() = default;
1413 constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
1416 std::destroy_at(__builtin_addressof(_M_unex));
1421 expected& operator=(const expected&) = delete;
1424 operator=(const expected& __x)
1425 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1426 is_nothrow_copy_assignable<_Er>>)
1427 requires is_copy_constructible_v<_Er>
1428 && is_copy_assignable_v<_Er>
1430 if (__x._M_has_value)
1433 _M_assign_unex(__x._M_unex);
1438 operator=(expected&& __x)
1439 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1440 is_nothrow_move_assignable<_Er>>)
1441 requires is_move_constructible_v<_Er>
1442 && is_move_assignable_v<_Er>
1444 if (__x._M_has_value)
1447 _M_assign_unex(std::move(__x._M_unex));
1451 template<typename _Gr>
1452 requires is_constructible_v<_Er, const _Gr&>
1453 && is_assignable_v<_Er&, const _Gr&>
1455 operator=(const unexpected<_Gr>& __e)
1457 _M_assign_unex(__e.error());
1461 template<typename _Gr>
1462 requires is_constructible_v<_Er, _Gr>
1463 && is_assignable_v<_Er&, _Gr>
1465 operator=(unexpected<_Gr>&& __e)
1467 _M_assign_unex(std::move(__e.error()));
1478 std::destroy_at(__builtin_addressof(_M_unex));
1479 _M_has_value = true;
1486 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1487 is_nothrow_move_constructible<_Er>>)
1488 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1492 if (!__x._M_has_value)
1494 std::construct_at(__builtin_addressof(_M_unex),
1495 std::move(__x._M_unex)); // might throw
1496 std::destroy_at(__builtin_addressof(__x._M_unex));
1497 _M_has_value = false;
1498 __x._M_has_value = true;
1503 if (__x._M_has_value)
1505 std::construct_at(__builtin_addressof(__x._M_unex),
1506 std::move(_M_unex)); // might throw
1507 std::destroy_at(__builtin_addressof(_M_unex));
1508 _M_has_value = true;
1509 __x._M_has_value = false;
1514 swap(_M_unex, __x._M_unex);
1523 operator bool() const noexcept { return _M_has_value; }
1526 constexpr bool has_value() const noexcept { return _M_has_value; }
1529 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1534 static_assert( is_copy_constructible_v<_Er> );
1535 if (_M_has_value) [[likely]]
1537 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1543 static_assert( is_copy_constructible_v<_Er> );
1544 static_assert( is_move_constructible_v<_Er> );
1545 if (_M_has_value) [[likely]]
1547 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
1550 constexpr const _Er&
1551 error() const & noexcept
1553 __glibcxx_assert(!_M_has_value);
1560 __glibcxx_assert(!_M_has_value);
1564 constexpr const _Er&&
1565 error() const && noexcept
1567 __glibcxx_assert(!_M_has_value);
1568 return std::move(_M_unex);
1574 __glibcxx_assert(!_M_has_value);
1575 return std::move(_M_unex);
1578 template<typename _Gr = _Er>
1580 error_or(_Gr&& __e) const&
1582 static_assert( is_copy_constructible_v<_Er> );
1583 static_assert( is_convertible_v<_Gr, _Er> );
1586 return std::forward<_Gr>(__e);
1590 template<typename _Gr = _Er>
1592 error_or(_Gr&& __e) &&
1594 static_assert( is_move_constructible_v<_Er> );
1595 static_assert( is_convertible_v<_Gr, _Er> );
1598 return std::forward<_Gr>(__e);
1599 return std::move(_M_unex);
1602 // monadic operations
1604 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1606 and_then(_Fn&& __f) &
1608 using _Up = __expected::__result0<_Fn>;
1609 static_assert(__expected::__is_expected<_Up>);
1610 static_assert(is_same_v<typename _Up::error_type, _Er>);
1613 return std::__invoke(std::forward<_Fn>(__f));
1615 return _Up(unexpect, _M_unex);
1618 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1620 and_then(_Fn&& __f) const &
1622 using _Up = __expected::__result0<_Fn>;
1623 static_assert(__expected::__is_expected<_Up>);
1624 static_assert(is_same_v<typename _Up::error_type, _Er>);
1627 return std::__invoke(std::forward<_Fn>(__f));
1629 return _Up(unexpect, _M_unex);
1632 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1634 and_then(_Fn&& __f) &&
1636 using _Up = __expected::__result0<_Fn>;
1637 static_assert(__expected::__is_expected<_Up>);
1638 static_assert(is_same_v<typename _Up::error_type, _Er>);
1641 return std::__invoke(std::forward<_Fn>(__f));
1643 return _Up(unexpect, std::move(_M_unex));
1646 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1648 and_then(_Fn&& __f) const &&
1650 using _Up = __expected::__result0<_Fn>;
1651 static_assert(__expected::__is_expected<_Up>);
1652 static_assert(is_same_v<typename _Up::error_type, _Er>);
1655 return std::__invoke(std::forward<_Fn>(__f));
1657 return _Up(unexpect, std::move(_M_unex));
1660 template<typename _Fn>
1662 or_else(_Fn&& __f) &
1664 using _Gr = __expected::__result<_Fn, _Er&>;
1665 static_assert(__expected::__is_expected<_Gr>);
1666 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1671 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1674 template<typename _Fn>
1676 or_else(_Fn&& __f) const &
1678 using _Gr = __expected::__result<_Fn, const _Er&>;
1679 static_assert(__expected::__is_expected<_Gr>);
1680 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1685 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1688 template<typename _Fn>
1690 or_else(_Fn&& __f) &&
1692 using _Gr = __expected::__result<_Fn, _Er&&>;
1693 static_assert(__expected::__is_expected<_Gr>);
1694 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1699 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1702 template<typename _Fn>
1704 or_else(_Fn&& __f) const &&
1706 using _Gr = __expected::__result<_Fn, const _Er&&>;
1707 static_assert(__expected::__is_expected<_Gr>);
1708 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1713 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1716 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1718 transform(_Fn&& __f) &
1720 using _Up = __expected::__result0_xform<_Fn>;
1721 using _Res = expected<_Up, _Er>;
1724 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1726 return _Res(unexpect, _M_unex);
1729 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1731 transform(_Fn&& __f) const &
1733 using _Up = __expected::__result0_xform<_Fn>;
1734 using _Res = expected<_Up, _Er>;
1737 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1739 return _Res(unexpect, _M_unex);
1742 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1744 transform(_Fn&& __f) &&
1746 using _Up = __expected::__result0_xform<_Fn>;
1747 using _Res = expected<_Up, _Er>;
1750 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1752 return _Res(unexpect, std::move(_M_unex));
1755 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1757 transform(_Fn&& __f) const &&
1759 using _Up = __expected::__result0_xform<_Fn>;
1760 using _Res = expected<_Up, _Er>;
1763 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1765 return _Res(unexpect, std::move(_M_unex));
1768 template<typename _Fn>
1770 transform_error(_Fn&& __f) &
1772 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1773 using _Res = expected<_Tp, _Gr>;
1778 return _Res(__unexpect_inv{}, [&]() {
1779 return std::__invoke(std::forward<_Fn>(__f),
1784 template<typename _Fn>
1786 transform_error(_Fn&& __f) const &
1788 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1789 using _Res = expected<_Tp, _Gr>;
1794 return _Res(__unexpect_inv{}, [&]() {
1795 return std::__invoke(std::forward<_Fn>(__f),
1800 template<typename _Fn>
1802 transform_error(_Fn&& __f) &&
1804 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1805 using _Res = expected<_Tp, _Gr>;
1810 return _Res(__unexpect_inv{}, [&]() {
1811 return std::__invoke(std::forward<_Fn>(__f),
1812 std::move(_M_unex));
1816 template<typename _Fn>
1818 transform_error(_Fn&& __f) const &&
1820 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1821 using _Res = expected<_Tp, _Gr>;
1826 return _Res(__unexpect_inv{}, [&]() {
1827 return std::__invoke(std::forward<_Fn>(__f),
1828 std::move(_M_unex));
1832 // equality operators
1834 template<typename _Up, typename _Er2>
1835 requires is_void_v<_Up>
1836 && requires (const _Er& __e, const _Er2& __e2) {
1837 { __e == __e2 } -> convertible_to<bool>;
1839 friend constexpr bool
1840 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1841 noexcept(noexcept(bool(__x.error() == __y.error())))
1843 if (__x.has_value())
1844 return __y.has_value();
1846 return !__y.has_value() && bool(__x.error() == __y.error());
1849 template<typename _Er2>
1850 requires requires (const _Er& __e, const _Er2& __e2) {
1851 { __e == __e2 } -> convertible_to<bool>;
1853 friend constexpr bool
1854 operator==(const expected& __x, const unexpected<_Er2>& __e)
1855 noexcept(noexcept(bool(__x.error() == __e.error())))
1856 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1858 friend constexpr void
1859 swap(expected& __x, expected& __y)
1860 noexcept(noexcept(__x.swap(__y)))
1861 requires requires { __x.swap(__y); }
1865 template<typename, typename> friend class expected;
1867 template<typename _Vp>
1869 _M_assign_unex(_Vp&& __v)
1873 std::construct_at(__builtin_addressof(_M_unex),
1874 std::forward<_Vp>(__v));
1875 _M_has_value = false;
1878 _M_unex = std::forward<_Vp>(__v);
1881 using __in_place_inv = __expected::__in_place_inv;
1882 using __unexpect_inv = __expected::__unexpect_inv;
1884 template<typename _Fn>
1886 expected(__in_place_inv, _Fn&& __fn)
1887 : _M_void(), _M_has_value(true)
1888 { std::forward<_Fn>(__fn)(); }
1890 template<typename _Fn>
1892 expected(__unexpect_inv, _Fn&& __fn)
1893 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1905_GLIBCXX_END_NAMESPACE_VERSION
1908#endif // __cpp_lib_expected
1909#endif // _GLIBCXX_EXPECTED