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
32#pragma GCC system_header
34#if __cplusplus > 202002L && __cpp_concepts >= 202002L
36#include <initializer_list>
37#include <bits/exception.h> // exception
38#include <bits/invoke.h> // __invoke
39#include <bits/stl_construct.h> // construct_at
40#include <bits/utility.h> // in_place_t
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @defgroup expected_values Expected values
48 * @addtogroup utilities
53#define __cpp_lib_expected 202211L
55 /// Discriminated union that holds an expected value or an error value.
59 template<typename _Tp, typename _Er>
62 /// Wrapper type used to pass an error value to a `std::expected`.
66 template<typename _Er>
69 /// Exception thrown by std::expected when the value() is not present.
73 template<typename _Er>
74 class bad_expected_access;
77 class bad_expected_access<void> : public exception
80 bad_expected_access() noexcept { }
81 bad_expected_access(const bad_expected_access&) = default;
82 bad_expected_access(bad_expected_access&&) = default;
83 bad_expected_access& operator=(const bad_expected_access&) = default;
84 bad_expected_access& operator=(bad_expected_access&&) = default;
85 ~bad_expected_access() = default;
91 what() const noexcept override
92 { return "bad access to std::expected without expected value"; }
95 template<typename _Er>
96 class bad_expected_access : public bad_expected_access<void> {
99 bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
101 // XXX const char* what() const noexcept override;
110 error() const & noexcept
116 { return std::move(_M_unex); }
120 error() const && noexcept
121 { return std::move(_M_unex); }
127 /// Tag type for constructing unexpected values in a std::expected
133 explicit unexpect_t() = default;
136 /// Tag for constructing unexpected values in a std::expected
140 inline constexpr unexpect_t unexpect{};
142/// @cond undocumented
145 template<typename _Tp>
146 constexpr bool __is_expected = false;
147 template<typename _Tp, typename _Er>
148 constexpr bool __is_expected<expected<_Tp, _Er>> = true;
150 template<typename _Tp>
151 constexpr bool __is_unexpected = false;
152 template<typename _Tp>
153 constexpr bool __is_unexpected<unexpected<_Tp>> = true;
155 template<typename _Fn, typename _Tp>
156 using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
157 template<typename _Fn, typename _Tp>
158 using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
159 template<typename _Fn>
160 using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
161 template<typename _Fn>
162 using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
164 template<typename _Er>
165 concept __can_be_unexpected
166 = is_object_v<_Er> && (!is_array_v<_Er>)
167 && (!__expected::__is_unexpected<_Er>)
168 && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
170 // Tag types for in-place construction from an invocation result.
171 struct __in_place_inv { };
172 struct __unexpect_inv { };
176 template<typename _Er>
179 static_assert( __expected::__can_be_unexpected<_Er> );
182 constexpr unexpected(const unexpected&) = default;
183 constexpr unexpected(unexpected&&) = default;
185 template<typename _Err = _Er>
186 requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
187 && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
188 && is_constructible_v<_Er, _Err>
190 unexpected(_Err&& __e)
191 noexcept(is_nothrow_constructible_v<_Er, _Err>)
192 : _M_unex(std::forward<_Err>(__e))
195 template<typename... _Args>
196 requires is_constructible_v<_Er, _Args...>
198 unexpected(in_place_t, _Args&&... __args)
199 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
200 : _M_unex(std::forward<_Args>(__args)...)
203 template<typename _Up, typename... _Args>
204 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
206 unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
207 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
209 : _M_unex(__il, std::forward<_Args>(__args)...)
212 constexpr unexpected& operator=(const unexpected&) = default;
213 constexpr unexpected& operator=(unexpected&&) = default;
218 error() const & noexcept { return _M_unex; }
222 error() & noexcept { return _M_unex; }
225 constexpr const _Er&&
226 error() const && noexcept { return std::move(_M_unex); }
230 error() && noexcept { return std::move(_M_unex); }
233 swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
234 requires is_swappable_v<_Er>
237 swap(_M_unex, __other._M_unex);
240 template<typename _Err>
242 friend constexpr bool
243 operator==(const unexpected& __x, const unexpected<_Err>& __y)
244 { return __x._M_unex == __y.error(); }
246 friend constexpr void
247 swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
248 requires is_swappable_v<_Er>
255 template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
257/// @cond undocumented
260 template<typename _Tp>
263 static_assert( is_nothrow_move_constructible_v<_Tp> );
267 : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
268 { std::destroy_at(_M_guarded); }
273 if (_M_guarded) [[unlikely]]
274 std::construct_at(_M_guarded, std::move(_M_tmp));
277 _Guard(const _Guard&) = delete;
278 _Guard& operator=(const _Guard&) = delete;
283 _M_guarded = nullptr;
284 return std::move(_M_tmp);
292 // reinit-expected helper from [expected.object.assign]
293 template<typename _Tp, typename _Up, typename _Vp>
295 __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
296 noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
298 if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
300 std::destroy_at(__oldval);
301 std::construct_at(__newval, std::forward<_Vp>(__arg));
303 else if constexpr (is_nothrow_move_constructible_v<_Tp>)
305 _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
306 std::destroy_at(__oldval);
307 std::construct_at(__newval, std::move(__tmp));
311 _Guard<_Up> __guard(*__oldval);
312 std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
319 template<typename _Tp, typename _Er>
322 static_assert( ! is_reference_v<_Tp> );
323 static_assert( ! is_function_v<_Tp> );
324 static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
325 static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
326 static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
327 static_assert( __expected::__can_be_unexpected<_Er> );
329 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
330 static constexpr bool __cons_from_expected
331 = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
332 is_constructible<_Tp, expected<_Up, _Err>>,
333 is_constructible<_Tp, const expected<_Up, _Err>&>,
334 is_constructible<_Tp, const expected<_Up, _Err>>,
335 is_convertible<expected<_Up, _Err>&, _Tp>,
336 is_convertible<expected<_Up, _Err>, _Tp>,
337 is_convertible<const expected<_Up, _Err>&, _Tp>,
338 is_convertible<const expected<_Up, _Err>, _Tp>,
339 is_constructible<_Unex, expected<_Up, _Err>&>,
340 is_constructible<_Unex, expected<_Up, _Err>>,
341 is_constructible<_Unex, const expected<_Up, _Err>&>,
342 is_constructible<_Unex, const expected<_Up, _Err>>
345 template<typename _Up, typename _Err>
346 constexpr static bool __explicit_conv
347 = __or_v<__not_<is_convertible<_Up, _Tp>>,
348 __not_<is_convertible<_Err, _Er>>
351 template<typename _Up>
352 static constexpr bool __same_val
353 = is_same_v<typename _Up::value_type, _Tp>;
355 template<typename _Up>
356 static constexpr bool __same_err
357 = is_same_v<typename _Up::error_type, _Er>;
360 using value_type = _Tp;
361 using error_type = _Er;
362 using unexpected_type = unexpected<_Er>;
364 template<typename _Up>
365 using rebind = expected<_Up, error_type>;
369 noexcept(is_nothrow_default_constructible_v<_Tp>)
370 requires is_default_constructible_v<_Tp>
371 : _M_val(), _M_has_value(true)
374 expected(const expected&) = default;
377 expected(const expected& __x)
378 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
379 is_nothrow_copy_constructible<_Er>>)
380 requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
381 && (!is_trivially_copy_constructible_v<_Tp>
382 || !is_trivially_copy_constructible_v<_Er>)
383 : _M_has_value(__x._M_has_value)
386 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
388 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
391 expected(expected&&) = default;
394 expected(expected&& __x)
395 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
396 is_nothrow_move_constructible<_Er>>)
397 requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
398 && (!is_trivially_move_constructible_v<_Tp>
399 || !is_trivially_move_constructible_v<_Er>)
400 : _M_has_value(__x._M_has_value)
403 std::construct_at(__builtin_addressof(_M_val),
404 std::move(__x)._M_val);
406 std::construct_at(__builtin_addressof(_M_unex),
407 std::move(__x)._M_unex);
410 template<typename _Up, typename _Gr>
411 requires is_constructible_v<_Tp, const _Up&>
412 && is_constructible_v<_Er, const _Gr&>
413 && (!__cons_from_expected<_Up, _Gr>)
414 constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
415 expected(const expected<_Up, _Gr>& __x)
416 noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
417 is_nothrow_constructible<_Er, const _Gr&>>)
418 : _M_has_value(__x._M_has_value)
421 std::construct_at(__builtin_addressof(_M_val), __x._M_val);
423 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
426 template<typename _Up, typename _Gr>
427 requires is_constructible_v<_Tp, _Up>
428 && is_constructible_v<_Er, _Gr>
429 && (!__cons_from_expected<_Up, _Gr>)
430 constexpr explicit(__explicit_conv<_Up, _Gr>)
431 expected(expected<_Up, _Gr>&& __x)
432 noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
433 is_nothrow_constructible<_Er, _Gr>>)
434 : _M_has_value(__x._M_has_value)
437 std::construct_at(__builtin_addressof(_M_val),
438 std::move(__x)._M_val);
440 std::construct_at(__builtin_addressof(_M_unex),
441 std::move(__x)._M_unex);
444 template<typename _Up = _Tp>
445 requires (!is_same_v<remove_cvref_t<_Up>, expected>)
446 && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
447 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
448 && is_constructible_v<_Tp, _Up>
449 constexpr explicit(!is_convertible_v<_Up, _Tp>)
451 noexcept(is_nothrow_constructible_v<_Tp, _Up>)
452 : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
455 template<typename _Gr = _Er>
456 requires is_constructible_v<_Er, const _Gr&>
457 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
458 expected(const unexpected<_Gr>& __u)
459 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
460 : _M_unex(__u.error()), _M_has_value(false)
463 template<typename _Gr = _Er>
464 requires is_constructible_v<_Er, _Gr>
465 constexpr explicit(!is_convertible_v<_Gr, _Er>)
466 expected(unexpected<_Gr>&& __u)
467 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
468 : _M_unex(std::move(__u).error()), _M_has_value(false)
471 template<typename... _Args>
472 requires is_constructible_v<_Tp, _Args...>
474 expected(in_place_t, _Args&&... __args)
475 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
476 : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
479 template<typename _Up, typename... _Args>
480 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
482 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
483 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
485 : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
488 template<typename... _Args>
489 requires is_constructible_v<_Er, _Args...>
491 expected(unexpect_t, _Args&&... __args)
492 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
493 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
496 template<typename _Up, typename... _Args>
497 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
499 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
500 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
502 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
505 constexpr ~expected() = default;
507 constexpr ~expected()
508 requires (!is_trivially_destructible_v<_Tp>)
509 || (!is_trivially_destructible_v<_Er>)
512 std::destroy_at(__builtin_addressof(_M_val));
514 std::destroy_at(__builtin_addressof(_M_unex));
519 expected& operator=(const expected&) = delete;
522 operator=(const expected& __x)
523 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
524 is_nothrow_copy_constructible<_Er>,
525 is_nothrow_copy_assignable<_Tp>,
526 is_nothrow_copy_assignable<_Er>>)
527 requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
528 && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
529 && (is_nothrow_move_constructible_v<_Tp>
530 || is_nothrow_move_constructible_v<_Er>)
532 if (__x._M_has_value)
533 this->_M_assign_val(__x._M_val);
535 this->_M_assign_unex(__x._M_unex);
540 operator=(expected&& __x)
541 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
542 is_nothrow_move_constructible<_Er>,
543 is_nothrow_move_assignable<_Tp>,
544 is_nothrow_move_assignable<_Er>>)
545 requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
546 && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
547 && (is_nothrow_move_constructible_v<_Tp>
548 || is_nothrow_move_constructible_v<_Er>)
550 if (__x._M_has_value)
551 _M_assign_val(std::move(__x._M_val));
553 _M_assign_unex(std::move(__x._M_unex));
557 template<typename _Up = _Tp>
558 requires (!is_same_v<expected, remove_cvref_t<_Up>>)
559 && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
560 && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
561 && (is_nothrow_constructible_v<_Tp, _Up>
562 || is_nothrow_move_constructible_v<_Tp>
563 || is_nothrow_move_constructible_v<_Er>)
567 _M_assign_val(std::forward<_Up>(__v));
571 template<typename _Gr>
572 requires is_constructible_v<_Er, const _Gr&>
573 && is_assignable_v<_Er&, const _Gr&>
574 && (is_nothrow_constructible_v<_Er, const _Gr&>
575 || is_nothrow_move_constructible_v<_Tp>
576 || is_nothrow_move_constructible_v<_Er>)
578 operator=(const unexpected<_Gr>& __e)
580 _M_assign_unex(__e.error());
584 template<typename _Gr>
585 requires is_constructible_v<_Er, _Gr>
586 && is_assignable_v<_Er&, _Gr>
587 && (is_nothrow_constructible_v<_Er, _Gr>
588 || is_nothrow_move_constructible_v<_Tp>
589 || is_nothrow_move_constructible_v<_Er>)
591 operator=(unexpected<_Gr>&& __e)
593 _M_assign_unex(std::move(__e).error());
599 template<typename... _Args>
600 requires is_nothrow_constructible_v<_Tp, _Args...>
602 emplace(_Args&&... __args) noexcept
605 std::destroy_at(__builtin_addressof(_M_val));
608 std::destroy_at(__builtin_addressof(_M_unex));
611 std::construct_at(__builtin_addressof(_M_val),
612 std::forward<_Args>(__args)...);
616 template<typename _Up, typename... _Args>
617 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
620 emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
623 std::destroy_at(__builtin_addressof(_M_val));
626 std::destroy_at(__builtin_addressof(_M_unex));
629 std::construct_at(__builtin_addressof(_M_val),
630 __il, std::forward<_Args>(__args)...);
637 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
638 is_nothrow_move_constructible<_Er>,
639 is_nothrow_swappable<_Tp&>,
640 is_nothrow_swappable<_Er&>>)
641 requires is_swappable_v<_Tp> && is_swappable_v<_Er>
642 && is_move_constructible_v<_Tp>
643 && is_move_constructible_v<_Er>
644 && (is_nothrow_move_constructible_v<_Tp>
645 || is_nothrow_move_constructible_v<_Er>)
649 if (__x._M_has_value)
652 swap(_M_val, __x._M_val);
655 this->_M_swap_val_unex(__x);
659 if (__x._M_has_value)
660 __x._M_swap_val_unex(*this);
664 swap(_M_unex, __x._M_unex);
673 operator->() const noexcept
675 __glibcxx_assert(_M_has_value);
676 return __builtin_addressof(_M_val);
681 operator->() noexcept
683 __glibcxx_assert(_M_has_value);
684 return __builtin_addressof(_M_val);
689 operator*() const & noexcept
691 __glibcxx_assert(_M_has_value);
697 operator*() & noexcept
699 __glibcxx_assert(_M_has_value);
704 constexpr const _Tp&&
705 operator*() const && noexcept
707 __glibcxx_assert(_M_has_value);
708 return std::move(_M_val);
713 operator*() && noexcept
715 __glibcxx_assert(_M_has_value);
716 return std::move(_M_val);
721 operator bool() const noexcept { return _M_has_value; }
724 constexpr bool has_value() const noexcept { return _M_has_value; }
729 if (_M_has_value) [[likely]]
731 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
737 if (_M_has_value) [[likely]]
739 const auto& __unex = _M_unex;
740 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
743 constexpr const _Tp&&
746 if (_M_has_value) [[likely]]
747 return std::move(_M_val);
748 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
754 if (_M_has_value) [[likely]]
755 return std::move(_M_val);
756 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
760 error() const & noexcept
762 __glibcxx_assert(!_M_has_value);
769 __glibcxx_assert(!_M_has_value);
773 constexpr const _Er&&
774 error() const && noexcept
776 __glibcxx_assert(!_M_has_value);
777 return std::move(_M_unex);
783 __glibcxx_assert(!_M_has_value);
784 return std::move(_M_unex);
787 template<typename _Up>
789 value_or(_Up&& __v) const &
790 noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
791 is_nothrow_convertible<_Up, _Tp>>)
793 static_assert( is_copy_constructible_v<_Tp> );
794 static_assert( is_convertible_v<_Up, _Tp> );
798 return static_cast<_Tp>(std::forward<_Up>(__v));
801 template<typename _Up>
803 value_or(_Up&& __v) &&
804 noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
805 is_nothrow_convertible<_Up, _Tp>>)
807 static_assert( is_move_constructible_v<_Tp> );
808 static_assert( is_convertible_v<_Up, _Tp> );
811 return std::move(_M_val);
812 return static_cast<_Tp>(std::forward<_Up>(__v));
815 template<typename _Gr = _Er>
817 error_or(_Gr&& __e) const&
819 static_assert( is_copy_constructible_v<_Er> );
820 static_assert( is_convertible_v<_Gr, _Er> );
823 return std::forward<_Gr>(__e);
827 template<typename _Gr = _Er>
829 error_or(_Gr&& __e) &&
831 static_assert( is_move_constructible_v<_Er> );
832 static_assert( is_convertible_v<_Gr, _Er> );
835 return std::forward<_Gr>(__e);
836 return std::move(_M_unex);
839 // monadic operations
841 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
843 and_then(_Fn&& __f) &
845 using _Up = __expected::__result<_Fn, _Tp&>;
846 static_assert(__expected::__is_expected<_Up>,
847 "the function passed to std::expected<T, E>::and_then "
848 "must return a std::expected");
849 static_assert(is_same_v<typename _Up::error_type, _Er>,
850 "the function passed to std::expected<T, E>::and_then "
851 "must return a std::expected with the same error_type");
854 return std::__invoke(std::forward<_Fn>(__f), _M_val);
856 return _Up(unexpect, _M_unex);
859 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
861 and_then(_Fn&& __f) const &
863 using _Up = __expected::__result<_Fn, const _Tp&>;
864 static_assert(__expected::__is_expected<_Up>,
865 "the function passed to std::expected<T, E>::and_then "
866 "must return a std::expected");
867 static_assert(is_same_v<typename _Up::error_type, _Er>,
868 "the function passed to std::expected<T, E>::and_then "
869 "must return a std::expected with the same error_type");
872 return std::__invoke(std::forward<_Fn>(__f), _M_val);
874 return _Up(unexpect, _M_unex);
877 template<typename _Fn> requires is_constructible_v<_Er, _Er>
879 and_then(_Fn&& __f) &&
881 using _Up = __expected::__result<_Fn, _Tp&&>;
882 static_assert(__expected::__is_expected<_Up>,
883 "the function passed to std::expected<T, E>::and_then "
884 "must return a std::expected");
885 static_assert(is_same_v<typename _Up::error_type, _Er>,
886 "the function passed to std::expected<T, E>::and_then "
887 "must return a std::expected with the same error_type");
890 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
892 return _Up(unexpect, std::move(_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), std::move(_M_val));
911 return _Up(unexpect, std::move(_M_unex));
914 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
918 using _Gr = __expected::__result<_Fn, _Er&>;
919 static_assert(__expected::__is_expected<_Gr>,
920 "the function passed to std::expected<T, E>::or_else "
921 "must return a std::expected");
922 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
923 "the function passed to std::expected<T, E>::or_else "
924 "must return a std::expected with the same value_type");
927 return _Gr(in_place, _M_val);
929 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
932 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
934 or_else(_Fn&& __f) const &
936 using _Gr = __expected::__result<_Fn, const _Er&>;
937 static_assert(__expected::__is_expected<_Gr>,
938 "the function passed to std::expected<T, E>::or_else "
939 "must return a std::expected");
940 static_assert(is_same_v<typename _Gr::value_type, _Tp>,
941 "the function passed to std::expected<T, E>::or_else "
942 "must return a std::expected with the same value_type");
945 return _Gr(in_place, _M_val);
947 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
951 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
953 or_else(_Fn&& __f) &&
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, std::move(_M_val));
966 return std::__invoke(std::forward<_Fn>(__f), std::move(_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, std::move(_M_val));
984 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
987 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
989 transform(_Fn&& __f) &
991 using _Up = __expected::__result_xform<_Fn, _Tp&>;
992 using _Res = expected<_Up, _Er>;
995 return _Res(__in_place_inv{}, [&]() {
996 return std::__invoke(std::forward<_Fn>(__f),
1000 return _Res(unexpect, _M_unex);
1003 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1005 transform(_Fn&& __f) const &
1007 using _Up = __expected::__result_xform<_Fn, const _Tp&>;
1008 using _Res = expected<_Up, _Er>;
1011 return _Res(__in_place_inv{}, [&]() {
1012 return std::__invoke(std::forward<_Fn>(__f),
1016 return _Res(unexpect, _M_unex);
1019 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1021 transform(_Fn&& __f) &&
1023 using _Up = __expected::__result_xform<_Fn, _Tp>;
1024 using _Res = expected<_Up, _Er>;
1027 return _Res(__in_place_inv{}, [&]() {
1028 return std::__invoke(std::forward<_Fn>(__f),
1032 return _Res(unexpect, std::move(_M_unex));
1035 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1037 transform(_Fn&& __f) const &&
1039 using _Up = __expected::__result_xform<_Fn, const _Tp>;
1040 using _Res = expected<_Up, _Er>;
1043 return _Res(__in_place_inv{}, [&]() {
1044 return std::__invoke(std::forward<_Fn>(__f),
1048 return _Res(unexpect, std::move(_M_unex));
1051 template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
1053 transform_error(_Fn&& __f) &
1055 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1056 using _Res = expected<_Tp, _Gr>;
1059 return _Res(in_place, _M_val);
1061 return _Res(__unexpect_inv{}, [&]() {
1062 return std::__invoke(std::forward<_Fn>(__f),
1067 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
1069 transform_error(_Fn&& __f) const &
1071 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1072 using _Res = expected<_Tp, _Gr>;
1075 return _Res(in_place, _M_val);
1077 return _Res(__unexpect_inv{}, [&]() {
1078 return std::__invoke(std::forward<_Fn>(__f),
1083 template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
1085 transform_error(_Fn&& __f) &&
1087 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1088 using _Res = expected<_Tp, _Gr>;
1091 return _Res(in_place, std::move(_M_val));
1093 return _Res(__unexpect_inv{}, [&]() {
1094 return std::__invoke(std::forward<_Fn>(__f),
1095 std::move(_M_unex));
1099 template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
1101 transform_error(_Fn&& __f) const &&
1103 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1104 using _Res = expected<_Tp, _Gr>;
1107 return _Res(in_place, std::move(_M_val));
1109 return _Res(__unexpect_inv{}, [&]() {
1110 return std::__invoke(std::forward<_Fn>(__f),
1111 std::move(_M_unex));
1115 // equality operators
1117 template<typename _Up, typename _Er2>
1118 requires (!is_void_v<_Up>)
1119 friend constexpr bool
1120 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1121 // FIXME: noexcept(noexcept(bool(*__x == *__y))
1122 // && noexcept(bool(__x.error() == __y.error())))
1124 if (__x.has_value())
1125 return __y.has_value() && bool(*__x == *__y);
1127 return !__y.has_value() && bool(__x.error() == __y.error());
1130 template<typename _Up>
1131 friend constexpr bool
1132 operator==(const expected& __x, const _Up& __v)
1133 // FIXME: noexcept(noexcept(bool(*__x == __v)))
1134 { return __x.has_value() && bool(*__x == __v); }
1136 template<typename _Er2>
1137 friend constexpr bool
1138 operator==(const expected& __x, const unexpected<_Er2>& __e)
1139 // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
1140 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1142 friend constexpr void
1143 swap(expected& __x, expected& __y)
1144 noexcept(noexcept(__x.swap(__y)))
1145 requires requires {__x.swap(__y);}
1149 template<typename, typename> friend class expected;
1151 template<typename _Vp>
1153 _M_assign_val(_Vp&& __v)
1156 _M_val = std::forward<_Vp>(__v);
1159 __expected::__reinit(__builtin_addressof(_M_val),
1160 __builtin_addressof(_M_unex),
1161 std::forward<_Vp>(__v));
1162 _M_has_value = true;
1166 template<typename _Vp>
1168 _M_assign_unex(_Vp&& __v)
1172 __expected::__reinit(__builtin_addressof(_M_unex),
1173 __builtin_addressof(_M_val),
1174 std::forward<_Vp>(__v));
1175 _M_has_value = false;
1178 _M_unex = std::forward<_Vp>(__v);
1181 // Swap two expected objects when only one has a value.
1182 // Precondition: this->_M_has_value && !__rhs._M_has_value
1184 _M_swap_val_unex(expected& __rhs)
1185 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1186 is_nothrow_move_constructible<_Tp>>)
1188 if constexpr (is_nothrow_move_constructible_v<_Er>)
1190 __expected::_Guard<_Er> __guard(__rhs._M_unex);
1191 std::construct_at(__builtin_addressof(__rhs._M_val),
1192 std::move(_M_val)); // might throw
1193 __rhs._M_has_value = true;
1194 std::destroy_at(__builtin_addressof(_M_val));
1195 std::construct_at(__builtin_addressof(_M_unex),
1197 _M_has_value = false;
1201 __expected::_Guard<_Tp> __guard(_M_val);
1202 std::construct_at(__builtin_addressof(_M_unex),
1203 std::move(__rhs._M_unex)); // might throw
1204 _M_has_value = false;
1205 std::destroy_at(__builtin_addressof(__rhs._M_unex));
1206 std::construct_at(__builtin_addressof(__rhs._M_val),
1208 __rhs._M_has_value = true;
1212 using __in_place_inv = __expected::__in_place_inv;
1213 using __unexpect_inv = __expected::__unexpect_inv;
1215 template<typename _Fn>
1217 expected(__in_place_inv, _Fn&& __fn)
1218 : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
1221 template<typename _Fn>
1223 expected(__unexpect_inv, _Fn&& __fn)
1224 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1235 // Partial specialization for std::expected<cv void, E>
1236 template<typename _Tp, typename _Er> requires is_void_v<_Tp>
1237 class expected<_Tp, _Er>
1239 static_assert( __expected::__can_be_unexpected<_Er> );
1241 template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
1242 static constexpr bool __cons_from_expected
1243 = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
1244 is_constructible<_Unex, expected<_Up, _Err>>,
1245 is_constructible<_Unex, const expected<_Up, _Err>&>,
1246 is_constructible<_Unex, const expected<_Up, _Err>>
1249 template<typename _Up>
1250 static constexpr bool __same_val
1251 = is_same_v<typename _Up::value_type, _Tp>;
1253 template<typename _Up>
1254 static constexpr bool __same_err
1255 = is_same_v<typename _Up::error_type, _Er>;
1258 using value_type = _Tp;
1259 using error_type = _Er;
1260 using unexpected_type = unexpected<_Er>;
1262 template<typename _Up>
1263 using rebind = expected<_Up, error_type>;
1267 : _M_void(), _M_has_value(true)
1270 expected(const expected&) = default;
1273 expected(const expected& __x)
1274 noexcept(is_nothrow_copy_constructible_v<_Er>)
1275 requires is_copy_constructible_v<_Er>
1276 && (!is_trivially_copy_constructible_v<_Er>)
1277 : _M_void(), _M_has_value(__x._M_has_value)
1280 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1283 expected(expected&&) = default;
1286 expected(expected&& __x)
1287 noexcept(is_nothrow_move_constructible_v<_Er>)
1288 requires is_move_constructible_v<_Er>
1289 && (!is_trivially_move_constructible_v<_Er>)
1290 : _M_void(), _M_has_value(__x._M_has_value)
1293 std::construct_at(__builtin_addressof(_M_unex),
1294 std::move(__x)._M_unex);
1297 template<typename _Up, typename _Gr>
1298 requires is_void_v<_Up>
1299 && is_constructible_v<_Er, const _Gr&>
1300 && (!__cons_from_expected<_Up, _Gr>)
1301 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1302 expected(const expected<_Up, _Gr>& __x)
1303 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1304 : _M_void(), _M_has_value(__x._M_has_value)
1307 std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
1310 template<typename _Up, typename _Gr>
1311 requires is_void_v<_Up>
1312 && is_constructible_v<_Er, _Gr>
1313 && (!__cons_from_expected<_Up, _Gr>)
1314 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1315 expected(expected<_Up, _Gr>&& __x)
1316 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1317 : _M_void(), _M_has_value(__x._M_has_value)
1320 std::construct_at(__builtin_addressof(_M_unex),
1321 std::move(__x)._M_unex);
1324 template<typename _Gr = _Er>
1325 requires is_constructible_v<_Er, const _Gr&>
1326 constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
1327 expected(const unexpected<_Gr>& __u)
1328 noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
1329 : _M_unex(__u.error()), _M_has_value(false)
1332 template<typename _Gr = _Er>
1333 requires is_constructible_v<_Er, _Gr>
1334 constexpr explicit(!is_convertible_v<_Gr, _Er>)
1335 expected(unexpected<_Gr>&& __u)
1336 noexcept(is_nothrow_constructible_v<_Er, _Gr>)
1337 : _M_unex(std::move(__u).error()), _M_has_value(false)
1341 expected(in_place_t) noexcept
1345 template<typename... _Args>
1346 requires is_constructible_v<_Er, _Args...>
1348 expected(unexpect_t, _Args&&... __args)
1349 noexcept(is_nothrow_constructible_v<_Er, _Args...>)
1350 : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
1353 template<typename _Up, typename... _Args>
1354 requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
1356 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
1357 noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
1359 : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
1362 constexpr ~expected() = default;
1364 constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
1367 std::destroy_at(__builtin_addressof(_M_unex));
1372 expected& operator=(const expected&) = delete;
1375 operator=(const expected& __x)
1376 noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
1377 is_nothrow_copy_assignable<_Er>>)
1378 requires is_copy_constructible_v<_Er>
1379 && is_copy_assignable_v<_Er>
1381 if (__x._M_has_value)
1384 _M_assign_unex(__x._M_unex);
1389 operator=(expected&& __x)
1390 noexcept(__and_v<is_nothrow_move_constructible<_Er>,
1391 is_nothrow_move_assignable<_Er>>)
1392 requires is_move_constructible_v<_Er>
1393 && is_move_assignable_v<_Er>
1395 if (__x._M_has_value)
1398 _M_assign_unex(std::move(__x._M_unex));
1402 template<typename _Gr>
1403 requires is_constructible_v<_Er, const _Gr&>
1404 && is_assignable_v<_Er&, const _Gr&>
1406 operator=(const unexpected<_Gr>& __e)
1408 _M_assign_unex(__e.error());
1412 template<typename _Gr>
1413 requires is_constructible_v<_Er, _Gr>
1414 && is_assignable_v<_Er&, _Gr>
1416 operator=(unexpected<_Gr>&& __e)
1418 _M_assign_unex(std::move(__e.error()));
1429 std::destroy_at(__builtin_addressof(_M_unex));
1430 _M_has_value = true;
1437 noexcept(__and_v<is_nothrow_swappable<_Er&>,
1438 is_nothrow_move_constructible<_Er>>)
1439 requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
1443 if (!__x._M_has_value)
1445 std::construct_at(__builtin_addressof(_M_unex),
1446 std::move(__x._M_unex)); // might throw
1447 std::destroy_at(__builtin_addressof(__x._M_unex));
1448 _M_has_value = false;
1449 __x._M_has_value = true;
1454 if (__x._M_has_value)
1456 std::construct_at(__builtin_addressof(__x._M_unex),
1457 std::move(_M_unex)); // might throw
1458 std::destroy_at(__builtin_addressof(_M_unex));
1459 _M_has_value = true;
1460 __x._M_has_value = false;
1465 swap(_M_unex, __x._M_unex);
1474 operator bool() const noexcept { return _M_has_value; }
1477 constexpr bool has_value() const noexcept { return _M_has_value; }
1480 operator*() const noexcept { __glibcxx_assert(_M_has_value); }
1485 if (_M_has_value) [[likely]]
1487 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
1493 if (_M_has_value) [[likely]]
1495 _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
1498 constexpr const _Er&
1499 error() const & noexcept
1501 __glibcxx_assert(!_M_has_value);
1508 __glibcxx_assert(!_M_has_value);
1512 constexpr const _Er&&
1513 error() const && noexcept
1515 __glibcxx_assert(!_M_has_value);
1516 return std::move(_M_unex);
1522 __glibcxx_assert(!_M_has_value);
1523 return std::move(_M_unex);
1526 template<typename _Gr = _Er>
1528 error_or(_Gr&& __e) const&
1530 static_assert( is_copy_constructible_v<_Er> );
1531 static_assert( is_convertible_v<_Gr, _Er> );
1534 return std::forward<_Gr>(__e);
1538 template<typename _Gr = _Er>
1540 error_or(_Gr&& __e) &&
1542 static_assert( is_move_constructible_v<_Er> );
1543 static_assert( is_convertible_v<_Gr, _Er> );
1546 return std::forward<_Gr>(__e);
1547 return std::move(_M_unex);
1550 // monadic operations
1552 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1554 and_then(_Fn&& __f) &
1556 using _Up = __expected::__result0<_Fn>;
1557 static_assert(__expected::__is_expected<_Up>);
1558 static_assert(is_same_v<typename _Up::error_type, _Er>);
1561 return std::__invoke(std::forward<_Fn>(__f));
1563 return _Up(unexpect, _M_unex);
1566 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1568 and_then(_Fn&& __f) const &
1570 using _Up = __expected::__result0<_Fn>;
1571 static_assert(__expected::__is_expected<_Up>);
1572 static_assert(is_same_v<typename _Up::error_type, _Er>);
1575 return std::__invoke(std::forward<_Fn>(__f));
1577 return _Up(unexpect, _M_unex);
1580 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1582 and_then(_Fn&& __f) &&
1584 using _Up = __expected::__result0<_Fn>;
1585 static_assert(__expected::__is_expected<_Up>);
1586 static_assert(is_same_v<typename _Up::error_type, _Er>);
1589 return std::__invoke(std::forward<_Fn>(__f));
1591 return _Up(unexpect, std::move(_M_unex));
1594 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1596 and_then(_Fn&& __f) const &&
1598 using _Up = __expected::__result0<_Fn>;
1599 static_assert(__expected::__is_expected<_Up>);
1600 static_assert(is_same_v<typename _Up::error_type, _Er>);
1603 return std::__invoke(std::forward<_Fn>(__f));
1605 return _Up(unexpect, std::move(_M_unex));
1608 template<typename _Fn>
1610 or_else(_Fn&& __f) &
1612 using _Gr = __expected::__result<_Fn, _Er&>;
1613 static_assert(__expected::__is_expected<_Gr>);
1614 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1619 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1622 template<typename _Fn>
1624 or_else(_Fn&& __f) const &
1626 using _Gr = __expected::__result<_Fn, const _Er&>;
1627 static_assert(__expected::__is_expected<_Gr>);
1628 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1633 return std::__invoke(std::forward<_Fn>(__f), _M_unex);
1636 template<typename _Fn>
1638 or_else(_Fn&& __f) &&
1640 using _Gr = __expected::__result<_Fn, _Er&&>;
1641 static_assert(__expected::__is_expected<_Gr>);
1642 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1647 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1650 template<typename _Fn>
1652 or_else(_Fn&& __f) const &&
1654 using _Gr = __expected::__result<_Fn, const _Er&&>;
1655 static_assert(__expected::__is_expected<_Gr>);
1656 static_assert(is_same_v<typename _Gr::value_type, _Tp>);
1661 return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
1664 template<typename _Fn> requires is_constructible_v<_Er, _Er&>
1666 transform(_Fn&& __f) &
1668 using _Up = __expected::__result0_xform<_Fn>;
1669 using _Res = expected<_Up, _Er>;
1672 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1674 return _Res(unexpect, _M_unex);
1677 template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
1679 transform(_Fn&& __f) const &
1681 using _Up = __expected::__result0_xform<_Fn>;
1682 using _Res = expected<_Up, _Er>;
1685 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1687 return _Res(unexpect, _M_unex);
1690 template<typename _Fn> requires is_constructible_v<_Er, _Er>
1692 transform(_Fn&& __f) &&
1694 using _Up = __expected::__result0_xform<_Fn>;
1695 using _Res = expected<_Up, _Er>;
1698 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1700 return _Res(unexpect, std::move(_M_unex));
1703 template<typename _Fn> requires is_constructible_v<_Er, const _Er>
1705 transform(_Fn&& __f) const &&
1707 using _Up = __expected::__result0_xform<_Fn>;
1708 using _Res = expected<_Up, _Er>;
1711 return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
1713 return _Res(unexpect, std::move(_M_unex));
1716 template<typename _Fn>
1718 transform_error(_Fn&& __f) &
1720 using _Gr = __expected::__result_xform<_Fn, _Er&>;
1721 using _Res = expected<_Tp, _Gr>;
1726 return _Res(__unexpect_inv{}, [&]() {
1727 return std::__invoke(std::forward<_Fn>(__f),
1732 template<typename _Fn>
1734 transform_error(_Fn&& __f) const &
1736 using _Gr = __expected::__result_xform<_Fn, const _Er&>;
1737 using _Res = expected<_Tp, _Gr>;
1742 return _Res(__unexpect_inv{}, [&]() {
1743 return std::__invoke(std::forward<_Fn>(__f),
1748 template<typename _Fn>
1750 transform_error(_Fn&& __f) &&
1752 using _Gr = __expected::__result_xform<_Fn, _Er&&>;
1753 using _Res = expected<_Tp, _Gr>;
1758 return _Res(__unexpect_inv{}, [&]() {
1759 return std::__invoke(std::forward<_Fn>(__f),
1760 std::move(_M_unex));
1764 template<typename _Fn>
1766 transform_error(_Fn&& __f) const &&
1768 using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
1769 using _Res = expected<_Tp, _Gr>;
1774 return _Res(__unexpect_inv{}, [&]() {
1775 return std::__invoke(std::forward<_Fn>(__f),
1776 std::move(_M_unex));
1780 // equality operators
1782 template<typename _Up, typename _Er2>
1783 requires is_void_v<_Up>
1784 friend constexpr bool
1785 operator==(const expected& __x, const expected<_Up, _Er2>& __y)
1786 // FIXME: noexcept(noexcept(bool(__x.error() == __y.error())))
1788 if (__x.has_value())
1789 return __y.has_value();
1791 return !__y.has_value() && bool(__x.error() == __y.error());
1794 template<typename _Er2>
1795 friend constexpr bool
1796 operator==(const expected& __x, const unexpected<_Er2>& __e)
1797 // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
1798 { return !__x.has_value() && bool(__x.error() == __e.error()); }
1800 friend constexpr void
1801 swap(expected& __x, expected& __y)
1802 noexcept(noexcept(__x.swap(__y)))
1803 requires requires { __x.swap(__y); }
1807 template<typename, typename> friend class expected;
1809 template<typename _Vp>
1811 _M_assign_unex(_Vp&& __v)
1815 std::construct_at(__builtin_addressof(_M_unex),
1816 std::forward<_Vp>(__v));
1817 _M_has_value = false;
1820 _M_unex = std::forward<_Vp>(__v);
1823 using __in_place_inv = __expected::__in_place_inv;
1824 using __unexpect_inv = __expected::__unexpect_inv;
1826 template<typename _Fn>
1828 expected(__in_place_inv, _Fn&& __fn)
1829 : _M_void(), _M_has_value(true)
1830 { std::forward<_Fn>(__fn)(); }
1832 template<typename _Fn>
1834 expected(__unexpect_inv, _Fn&& __fn)
1835 : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
1847_GLIBCXX_END_NAMESPACE_VERSION
1851#endif // _GLIBCXX_EXPECTED