Home | History | Annotate | Line # | Download | only in std
      1  1.1  mrg // <expected> -*- C++ -*-
      2  1.1  mrg 
      3  1.1  mrg // Copyright The GNU Toolchain Authors.
      4  1.1  mrg //
      5  1.1  mrg // This file is part of the GNU ISO C++ Library.  This library is free
      6  1.1  mrg // software; you can redistribute it and/or modify it under the
      7  1.1  mrg // terms of the GNU General Public License as published by the
      8  1.1  mrg // Free Software Foundation; either version 3, or (at your option)
      9  1.1  mrg // any later version.
     10  1.1  mrg 
     11  1.1  mrg // This library is distributed in the hope that it will be useful,
     12  1.1  mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  mrg // GNU General Public License for more details.
     15  1.1  mrg 
     16  1.1  mrg // Under Section 7 of GPL version 3, you are granted additional
     17  1.1  mrg // permissions described in the GCC Runtime Library Exception, version
     18  1.1  mrg // 3.1, as published by the Free Software Foundation.
     19  1.1  mrg 
     20  1.1  mrg // You should have received a copy of the GNU General Public License and
     21  1.1  mrg // a copy of the GCC Runtime Library Exception along with this program;
     22  1.1  mrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23  1.1  mrg // <http://www.gnu.org/licenses/>.
     24  1.1  mrg 
     25  1.1  mrg /** @file include/expected
     26  1.1  mrg  *  This is a Standard C++ Library header.
     27  1.1  mrg  */
     28  1.1  mrg 
     29  1.1  mrg #ifndef _GLIBCXX_EXPECTED
     30  1.1  mrg #define _GLIBCXX_EXPECTED
     31  1.1  mrg 
     32  1.1  mrg #pragma GCC system_header
     33  1.1  mrg 
     34  1.1  mrg #if __cplusplus > 202002L && __cpp_concepts >= 202002L
     35  1.1  mrg 
     36  1.1  mrg #include <initializer_list>
     37  1.1  mrg #include <bits/exception.h>	// exception
     38  1.1  mrg #include <bits/stl_construct.h>	// construct_at
     39  1.1  mrg #include <bits/utility.h>	// in_place_t
     40  1.1  mrg 
     41  1.1  mrg namespace std _GLIBCXX_VISIBILITY(default)
     42  1.1  mrg {
     43  1.1  mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
     44  1.1  mrg 
     45  1.1  mrg   /**
     46  1.1  mrg    * @defgroup expected_values Expected values
     47  1.1  mrg    * @addtogroup utilities
     48  1.1  mrg    * @since C++23
     49  1.1  mrg    * @{
     50  1.1  mrg    */
     51  1.1  mrg 
     52  1.1  mrg #define __cpp_lib_expected 202202L
     53  1.1  mrg 
     54  1.1  mrg   /// Discriminated union that holds an expected value or an error value.
     55  1.1  mrg   /**
     56  1.1  mrg    * @since C++23
     57  1.1  mrg    */
     58  1.1  mrg   template<typename _Tp, typename _Er>
     59  1.1  mrg     class expected;
     60  1.1  mrg 
     61  1.1  mrg   /// Wrapper type used to pass an error value to a `std::expected`.
     62  1.1  mrg   /**
     63  1.1  mrg    * @since C++23
     64  1.1  mrg    */
     65  1.1  mrg   template<typename _Er>
     66  1.1  mrg     class unexpected;
     67  1.1  mrg 
     68  1.1  mrg   /// Exception thrown by std::expected when the value() is not present.
     69  1.1  mrg   /**
     70  1.1  mrg    * @since C++23
     71  1.1  mrg    */
     72  1.1  mrg   template<typename _Er>
     73  1.1  mrg     class bad_expected_access;
     74  1.1  mrg 
     75  1.1  mrg   template<>
     76  1.1  mrg     class bad_expected_access<void> : public exception
     77  1.1  mrg     {
     78  1.1  mrg     protected:
     79  1.1  mrg       bad_expected_access() noexcept { }
     80  1.1  mrg       bad_expected_access(const bad_expected_access&) = default;
     81  1.1  mrg       bad_expected_access(bad_expected_access&&) = default;
     82  1.1  mrg       bad_expected_access& operator=(const bad_expected_access&) = default;
     83  1.1  mrg       bad_expected_access& operator=(bad_expected_access&&) = default;
     84  1.1  mrg       ~bad_expected_access() = default;
     85  1.1  mrg 
     86  1.1  mrg     public:
     87  1.1  mrg 
     88  1.1  mrg       [[nodiscard]]
     89  1.1  mrg       const char*
     90  1.1  mrg       what() const noexcept override
     91  1.1  mrg       { return "bad access to std::expected without expected value"; }
     92  1.1  mrg     };
     93  1.1  mrg 
     94  1.1  mrg   template<typename _Er>
     95  1.1  mrg     class bad_expected_access : public bad_expected_access<void> {
     96  1.1  mrg     public:
     97  1.1  mrg       explicit
     98  1.1  mrg       bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
     99  1.1  mrg 
    100  1.1  mrg       // XXX const char* what() const noexcept override;
    101  1.1  mrg 
    102  1.1  mrg       [[nodiscard]]
    103  1.1  mrg       _Er&
    104  1.1  mrg       error() & noexcept
    105  1.1  mrg       { return _M_unex; }
    106  1.1  mrg 
    107  1.1  mrg       [[nodiscard]]
    108  1.1  mrg       const _Er&
    109  1.1  mrg       error() const & noexcept
    110  1.1  mrg       { return _M_unex; }
    111  1.1  mrg 
    112  1.1  mrg       [[nodiscard]]
    113  1.1  mrg       _Er&&
    114  1.1  mrg       error() && noexcept
    115  1.1  mrg       { return std::move(_M_unex); }
    116  1.1  mrg 
    117  1.1  mrg       [[nodiscard]]
    118  1.1  mrg       const _Er&&
    119  1.1  mrg       error() const && noexcept
    120  1.1  mrg       { return std::move(_M_unex); }
    121  1.1  mrg 
    122  1.1  mrg     private:
    123  1.1  mrg       _Er _M_unex;
    124  1.1  mrg     };
    125  1.1  mrg 
    126  1.1  mrg   /// Tag type for constructing unexpected values in a std::expected
    127  1.1  mrg   /**
    128  1.1  mrg    * @since C++23
    129  1.1  mrg    */
    130  1.1  mrg   struct unexpect_t
    131  1.1  mrg   {
    132  1.1  mrg     explicit unexpect_t() = default;
    133  1.1  mrg   };
    134  1.1  mrg 
    135  1.1  mrg   /// Tag for constructing unexpected values in a std::expected
    136  1.1  mrg   /**
    137  1.1  mrg    * @since C++23
    138  1.1  mrg    */
    139  1.1  mrg   inline constexpr unexpect_t unexpect{};
    140  1.1  mrg 
    141  1.1  mrg /// @cond undoc
    142  1.1  mrg namespace __expected
    143  1.1  mrg {
    144  1.1  mrg   template<typename _Tp>
    145  1.1  mrg     constexpr bool __is_expected = false;
    146  1.1  mrg   template<typename _Tp, typename _Er>
    147  1.1  mrg     constexpr bool __is_expected<expected<_Tp, _Er>> = true;
    148  1.1  mrg 
    149  1.1  mrg   template<typename _Tp>
    150  1.1  mrg     constexpr bool __is_unexpected = false;
    151  1.1  mrg   template<typename _Tp>
    152  1.1  mrg     constexpr bool __is_unexpected<unexpected<_Tp>> = true;
    153  1.1  mrg 
    154  1.1  mrg   template<typename _Er>
    155  1.1  mrg     concept __can_be_unexpected
    156  1.1  mrg       = is_object_v<_Er> && (!is_array_v<_Er>)
    157  1.1  mrg 	  && (!__expected::__is_unexpected<_Er>)
    158  1.1  mrg 	  && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
    159  1.1  mrg }
    160  1.1  mrg /// @endcond
    161  1.1  mrg 
    162  1.1  mrg   template<typename _Er>
    163  1.1  mrg     class unexpected
    164  1.1  mrg     {
    165  1.1  mrg       static_assert( __expected::__can_be_unexpected<_Er> );
    166  1.1  mrg 
    167  1.1  mrg     public:
    168  1.1  mrg       constexpr unexpected(const unexpected&) = default;
    169  1.1  mrg       constexpr unexpected(unexpected&&) = default;
    170  1.1  mrg 
    171  1.1  mrg       template<typename _Err = _Er>
    172  1.1  mrg 	requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
    173  1.1  mrg 	  && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
    174  1.1  mrg 	  && is_constructible_v<_Er, _Err>
    175  1.1  mrg 	constexpr explicit
    176  1.1  mrg 	unexpected(_Err&& __e)
    177  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Err>)
    178  1.1  mrg 	: _M_unex(std::forward<_Err>(__e))
    179  1.1  mrg 	{ }
    180  1.1  mrg 
    181  1.1  mrg       template<typename... _Args>
    182  1.1  mrg 	requires is_constructible_v<_Er, _Args...>
    183  1.1  mrg 	constexpr explicit
    184  1.1  mrg 	unexpected(in_place_t, _Args&&... __args)
    185  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Args...>)
    186  1.1  mrg 	: _M_unex(std::forward<_Args>(__args)...)
    187  1.1  mrg 	{ }
    188  1.1  mrg 
    189  1.1  mrg       template<typename _Up, typename... _Args>
    190  1.1  mrg 	requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
    191  1.1  mrg 	constexpr explicit
    192  1.1  mrg 	unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
    193  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
    194  1.1  mrg 					    _Args...>)
    195  1.1  mrg 	: _M_unex(__il, std::forward<_Args>(__args)...)
    196  1.1  mrg 	{ }
    197  1.1  mrg 
    198  1.1  mrg       constexpr unexpected& operator=(const unexpected&) = default;
    199  1.1  mrg       constexpr unexpected& operator=(unexpected&&) = default;
    200  1.1  mrg 
    201  1.1  mrg 
    202  1.1  mrg       [[nodiscard]]
    203  1.1  mrg       constexpr const _Er&
    204  1.1  mrg       error() const & noexcept { return _M_unex; }
    205  1.1  mrg 
    206  1.1  mrg       [[nodiscard]]
    207  1.1  mrg       constexpr _Er&
    208  1.1  mrg       error() & noexcept { return _M_unex; }
    209  1.1  mrg 
    210  1.1  mrg       [[nodiscard]]
    211  1.1  mrg       constexpr const _Er&&
    212  1.1  mrg       error() const && noexcept { return std::move(_M_unex); }
    213  1.1  mrg 
    214  1.1  mrg       [[nodiscard]]
    215  1.1  mrg       constexpr _Er&&
    216  1.1  mrg       error() && noexcept { return std::move(_M_unex); }
    217  1.1  mrg 
    218  1.1  mrg       constexpr void
    219  1.1  mrg       swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
    220  1.1  mrg       requires is_swappable_v<_Er>
    221  1.1  mrg       {
    222  1.1  mrg 	using std::swap;
    223  1.1  mrg 	swap(_M_unex, __other._M_unex);
    224  1.1  mrg       }
    225  1.1  mrg 
    226  1.1  mrg       template<typename _Err>
    227  1.1  mrg 	[[nodiscard]]
    228  1.1  mrg 	friend constexpr bool
    229  1.1  mrg 	operator==(const unexpected& __x, const unexpected<_Err>& __y)
    230  1.1  mrg 	{ return __x._M_unex == __y.error(); }
    231  1.1  mrg 
    232  1.1  mrg       friend constexpr void
    233  1.1  mrg       swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
    234  1.1  mrg       requires is_swappable_v<_Er>
    235  1.1  mrg       { __x.swap(__y); }
    236  1.1  mrg 
    237  1.1  mrg     private:
    238  1.1  mrg       _Er _M_unex;
    239  1.1  mrg     };
    240  1.1  mrg 
    241  1.1  mrg   template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
    242  1.1  mrg 
    243  1.1  mrg /// @cond undoc
    244  1.1  mrg namespace __expected
    245  1.1  mrg {
    246  1.1  mrg   template<typename _Tp>
    247  1.1  mrg     struct _Guard
    248  1.1  mrg     {
    249  1.1  mrg       static_assert( is_nothrow_move_constructible_v<_Tp> );
    250  1.1  mrg 
    251  1.1  mrg       constexpr explicit
    252  1.1  mrg       _Guard(_Tp& __x)
    253  1.1  mrg       : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
    254  1.1  mrg       { std::destroy_at(_M_guarded); }
    255  1.1  mrg 
    256  1.1  mrg       constexpr
    257  1.1  mrg       ~_Guard()
    258  1.1  mrg       {
    259  1.1  mrg 	if (_M_guarded) [[unlikely]]
    260  1.1  mrg 	  std::construct_at(_M_guarded, std::move(_M_tmp));
    261  1.1  mrg       }
    262  1.1  mrg 
    263  1.1  mrg       _Guard(const _Guard&) = delete;
    264  1.1  mrg       _Guard& operator=(const _Guard&) = delete;
    265  1.1  mrg 
    266  1.1  mrg       constexpr _Tp&&
    267  1.1  mrg       release() noexcept
    268  1.1  mrg       {
    269  1.1  mrg 	_M_guarded = nullptr;
    270  1.1  mrg 	return std::move(_M_tmp);
    271  1.1  mrg       }
    272  1.1  mrg 
    273  1.1  mrg     private:
    274  1.1  mrg       _Tp* _M_guarded;
    275  1.1  mrg       _Tp _M_tmp;
    276  1.1  mrg     };
    277  1.1  mrg 
    278  1.1  mrg   // reinit-expected helper from [expected.object.assign]
    279  1.1  mrg   template<typename _Tp, typename _Up, typename _Vp>
    280  1.1  mrg     constexpr void
    281  1.1  mrg     __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
    282  1.1  mrg     noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
    283  1.1  mrg     {
    284  1.1  mrg       if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
    285  1.1  mrg 	{
    286  1.1  mrg 	  std::destroy_at(__oldval);
    287  1.1  mrg 	  std::construct_at(__newval, std::forward<_Vp>(__arg));
    288  1.1  mrg 	}
    289  1.1  mrg       else if constexpr (is_nothrow_move_constructible_v<_Tp>)
    290  1.1  mrg 	{
    291  1.1  mrg 	  _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
    292  1.1  mrg 	  std::destroy_at(__oldval);
    293  1.1  mrg 	  std::construct_at(__newval, std::move(__tmp));
    294  1.1  mrg 	}
    295  1.1  mrg       else
    296  1.1  mrg 	{
    297  1.1  mrg 	  _Guard<_Up> __guard(*__oldval);
    298  1.1  mrg 	  std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
    299  1.1  mrg 	  __guard.release();
    300  1.1  mrg 	}
    301  1.1  mrg     }
    302  1.1  mrg }
    303  1.1  mrg /// @endcond
    304  1.1  mrg 
    305  1.1  mrg   template<typename _Tp, typename _Er>
    306  1.1  mrg     class expected
    307  1.1  mrg     {
    308  1.1  mrg       static_assert( ! is_reference_v<_Tp> );
    309  1.1  mrg       static_assert( ! is_function_v<_Tp> );
    310  1.1  mrg       static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
    311  1.1  mrg       static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
    312  1.1  mrg       static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
    313  1.1  mrg       static_assert( __expected::__can_be_unexpected<_Er> );
    314  1.1  mrg 
    315  1.1  mrg       template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
    316  1.1  mrg 	static constexpr bool __cons_from_expected
    317  1.1  mrg 	  = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
    318  1.1  mrg 		   is_constructible<_Tp, expected<_Up, _Err>>,
    319  1.1  mrg 		   is_constructible<_Tp, const expected<_Up, _Err>&>,
    320  1.1  mrg 		   is_constructible<_Tp, const expected<_Up, _Err>>,
    321  1.1  mrg 		   is_convertible<expected<_Up, _Err>&, _Tp>,
    322  1.1  mrg 		   is_convertible<expected<_Up, _Err>, _Tp>,
    323  1.1  mrg 		   is_convertible<const expected<_Up, _Err>&, _Tp>,
    324  1.1  mrg 		   is_convertible<const expected<_Up, _Err>, _Tp>,
    325  1.1  mrg 		   is_constructible<_Unex, expected<_Up, _Err>&>,
    326  1.1  mrg 		   is_constructible<_Unex, expected<_Up, _Err>>,
    327  1.1  mrg 		   is_constructible<_Unex, const expected<_Up, _Err>&>,
    328  1.1  mrg 		   is_constructible<_Unex, const expected<_Up, _Err>>
    329  1.1  mrg 		  >;
    330  1.1  mrg 
    331  1.1  mrg       template<typename _Up, typename _Err>
    332  1.1  mrg 	constexpr static bool __explicit_conv
    333  1.1  mrg 	  = __or_v<__not_<is_convertible<_Up, _Tp>>,
    334  1.1  mrg 		   __not_<is_convertible<_Err, _Er>>
    335  1.1  mrg 		  >;
    336  1.1  mrg 
    337  1.1  mrg     public:
    338  1.1  mrg       using value_type = _Tp;
    339  1.1  mrg       using error_type = _Er;
    340  1.1  mrg       using unexpected_type = unexpected<_Er>;
    341  1.1  mrg 
    342  1.1  mrg       template<typename _Up>
    343  1.1  mrg 	using rebind = expected<_Up, error_type>;
    344  1.1  mrg 
    345  1.1  mrg       constexpr
    346  1.1  mrg       expected()
    347  1.1  mrg       noexcept(is_nothrow_default_constructible_v<_Tp>)
    348  1.1  mrg       requires is_default_constructible_v<_Tp>
    349  1.1  mrg       : _M_val(), _M_has_value(true)
    350  1.1  mrg       { }
    351  1.1  mrg 
    352  1.1  mrg       expected(const expected&) = default;
    353  1.1  mrg 
    354  1.1  mrg       constexpr
    355  1.1  mrg       expected(const expected& __x)
    356  1.1  mrg       noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
    357  1.1  mrg 		       is_nothrow_copy_constructible<_Er>>)
    358  1.1  mrg       requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
    359  1.1  mrg       && (!is_trivially_copy_constructible_v<_Tp>
    360  1.1  mrg 	  || !is_trivially_copy_constructible_v<_Er>)
    361  1.1  mrg       : _M_has_value(__x._M_has_value)
    362  1.1  mrg       {
    363  1.1  mrg 	if (_M_has_value)
    364  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_val), __x._M_val);
    365  1.1  mrg 	else
    366  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
    367  1.1  mrg       }
    368  1.1  mrg 
    369  1.1  mrg       expected(expected&&) = default;
    370  1.1  mrg 
    371  1.1  mrg       constexpr
    372  1.1  mrg       expected(expected&& __x)
    373  1.1  mrg       noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
    374  1.1  mrg 		       is_nothrow_move_constructible<_Er>>)
    375  1.1  mrg       requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
    376  1.1  mrg       && (!is_trivially_move_constructible_v<_Tp>
    377  1.1  mrg 	  || !is_trivially_move_constructible_v<_Er>)
    378  1.1  mrg       : _M_has_value(__x._M_has_value)
    379  1.1  mrg       {
    380  1.1  mrg 	if (_M_has_value)
    381  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_val),
    382  1.1  mrg 			    std::move(__x)._M_val);
    383  1.1  mrg 	else
    384  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_unex),
    385  1.1  mrg 			    std::move(__x)._M_unex);
    386  1.1  mrg       }
    387  1.1  mrg 
    388  1.1  mrg       template<typename _Up, typename _Gr>
    389  1.1  mrg 	requires is_constructible_v<_Tp, const _Up&>
    390  1.1  mrg 	      && is_constructible_v<_Er, const _Gr&>
    391  1.1  mrg 	      && (!__cons_from_expected<_Up, _Gr>)
    392  1.1  mrg 	constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
    393  1.1  mrg 	expected(const expected<_Up, _Gr>& __x)
    394  1.1  mrg 	noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
    395  1.1  mrg 			 is_nothrow_constructible<_Er, const _Gr&>>)
    396  1.1  mrg 	: _M_has_value(__x._M_has_value)
    397  1.1  mrg 	{
    398  1.1  mrg 	  if (_M_has_value)
    399  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_val), __x._M_val);
    400  1.1  mrg 	  else
    401  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
    402  1.1  mrg 	}
    403  1.1  mrg 
    404  1.1  mrg       template<typename _Up, typename _Gr>
    405  1.1  mrg 	requires is_constructible_v<_Tp, _Up>
    406  1.1  mrg 	      && is_constructible_v<_Er, _Gr>
    407  1.1  mrg 	      && (!__cons_from_expected<_Up, _Gr>)
    408  1.1  mrg 	constexpr explicit(__explicit_conv<_Up, _Gr>)
    409  1.1  mrg 	expected(expected<_Up, _Gr>&& __x)
    410  1.1  mrg 	noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
    411  1.1  mrg 			 is_nothrow_constructible<_Er, _Gr>>)
    412  1.1  mrg 	: _M_has_value(__x._M_has_value)
    413  1.1  mrg 	{
    414  1.1  mrg 	  if (_M_has_value)
    415  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_val),
    416  1.1  mrg 			      std::move(__x)._M_val);
    417  1.1  mrg 	  else
    418  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex),
    419  1.1  mrg 			      std::move(__x)._M_unex);
    420  1.1  mrg 	}
    421  1.1  mrg 
    422  1.1  mrg       template<typename _Up = _Tp>
    423  1.1  mrg 	requires (!is_same_v<remove_cvref_t<_Up>, expected>)
    424  1.1  mrg 	  && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
    425  1.1  mrg 	  && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
    426  1.1  mrg 	  && is_constructible_v<_Tp, _Up>
    427  1.1  mrg 	constexpr explicit(!is_convertible_v<_Up, _Tp>)
    428  1.1  mrg 	expected(_Up&& __v)
    429  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Tp, _Up>)
    430  1.1  mrg 	: _M_val(std::forward<_Up>(__v)), _M_has_value(true)
    431  1.1  mrg 	{ }
    432  1.1  mrg 
    433  1.1  mrg       template<typename _Gr = _Er>
    434  1.1  mrg 	requires is_constructible_v<_Er, const _Gr&>
    435  1.1  mrg 	constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
    436  1.1  mrg 	expected(const unexpected<_Gr>& __u)
    437  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
    438  1.1  mrg 	: _M_unex(__u.error()), _M_has_value(false)
    439  1.1  mrg 	{ }
    440  1.1  mrg 
    441  1.1  mrg       template<typename _Gr = _Er>
    442  1.1  mrg 	requires is_constructible_v<_Er, _Gr>
    443  1.1  mrg 	constexpr explicit(!is_convertible_v<_Gr, _Er>)
    444  1.1  mrg 	expected(unexpected<_Gr>&& __u)
    445  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Gr>)
    446  1.1  mrg 	: _M_unex(std::move(__u).error()), _M_has_value(false)
    447  1.1  mrg 	{ }
    448  1.1  mrg 
    449  1.1  mrg       template<typename... _Args>
    450  1.1  mrg 	requires is_constructible_v<_Tp, _Args...>
    451  1.1  mrg 	constexpr explicit
    452  1.1  mrg 	expected(in_place_t, _Args&&... __args)
    453  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
    454  1.1  mrg 	: _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
    455  1.1  mrg 	{ }
    456  1.1  mrg 
    457  1.1  mrg       template<typename _Up, typename... _Args>
    458  1.1  mrg 	requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
    459  1.1  mrg 	constexpr explicit
    460  1.1  mrg 	expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
    461  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
    462  1.1  mrg 					    _Args...>)
    463  1.1  mrg 	: _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
    464  1.1  mrg 	{ }
    465  1.1  mrg 
    466  1.1  mrg       template<typename... _Args>
    467  1.1  mrg 	requires is_constructible_v<_Er, _Args...>
    468  1.1  mrg 	constexpr explicit
    469  1.1  mrg 	expected(unexpect_t, _Args&&... __args)
    470  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Args...>)
    471  1.1  mrg 	: _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
    472  1.1  mrg 	{ }
    473  1.1  mrg 
    474  1.1  mrg       template<typename _Up, typename... _Args>
    475  1.1  mrg 	requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
    476  1.1  mrg 	constexpr explicit
    477  1.1  mrg 	expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
    478  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
    479  1.1  mrg 					    _Args...>)
    480  1.1  mrg 	: _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
    481  1.1  mrg 	{ }
    482  1.1  mrg 
    483  1.1  mrg       constexpr ~expected() = default;
    484  1.1  mrg 
    485  1.1  mrg       constexpr ~expected()
    486  1.1  mrg       requires (!is_trivially_destructible_v<_Tp>)
    487  1.1  mrg 	    || (!is_trivially_destructible_v<_Er>)
    488  1.1  mrg       {
    489  1.1  mrg 	if (_M_has_value)
    490  1.1  mrg 	  std::destroy_at(__builtin_addressof(_M_val));
    491  1.1  mrg 	else
    492  1.1  mrg 	  std::destroy_at(__builtin_addressof(_M_unex));
    493  1.1  mrg       }
    494  1.1  mrg 
    495  1.1  mrg       // assignment
    496  1.1  mrg 
    497  1.1  mrg       expected& operator=(const expected&) = delete;
    498  1.1  mrg 
    499  1.1  mrg       constexpr expected&
    500  1.1  mrg       operator=(const expected& __x)
    501  1.1  mrg       noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
    502  1.1  mrg 		       is_nothrow_copy_constructible<_Er>,
    503  1.1  mrg 		       is_nothrow_copy_assignable<_Tp>,
    504  1.1  mrg 		       is_nothrow_copy_assignable<_Er>>)
    505  1.1  mrg       requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
    506  1.1  mrg 	    && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
    507  1.1  mrg 	    && (is_nothrow_move_constructible_v<_Tp>
    508  1.1  mrg 		|| is_nothrow_move_constructible_v<_Er>)
    509  1.1  mrg       {
    510  1.1  mrg 	if (__x._M_has_value)
    511  1.1  mrg 	  this->_M_assign_val(__x._M_val);
    512  1.1  mrg 	else
    513  1.1  mrg 	  this->_M_assign_unex(__x._M_unex);
    514  1.1  mrg 	return *this;
    515  1.1  mrg       }
    516  1.1  mrg 
    517  1.1  mrg       constexpr expected&
    518  1.1  mrg       operator=(expected&& __x)
    519  1.1  mrg       noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
    520  1.1  mrg 		       is_nothrow_move_constructible<_Er>,
    521  1.1  mrg 		       is_nothrow_move_assignable<_Tp>,
    522  1.1  mrg 		       is_nothrow_move_assignable<_Er>>)
    523  1.1  mrg       requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
    524  1.1  mrg 	    && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
    525  1.1  mrg 	    && (is_nothrow_move_constructible_v<_Tp>
    526  1.1  mrg 		|| is_nothrow_move_constructible_v<_Er>)
    527  1.1  mrg       {
    528  1.1  mrg 	if (__x._M_has_value)
    529  1.1  mrg 	  _M_assign_val(std::move(__x._M_val));
    530  1.1  mrg 	else
    531  1.1  mrg 	  _M_assign_unex(std::move(__x._M_unex));
    532  1.1  mrg 	return *this;
    533  1.1  mrg       }
    534  1.1  mrg 
    535  1.1  mrg       template<typename _Up = _Tp>
    536  1.1  mrg 	requires (!is_same_v<expected, remove_cvref_t<_Up>>)
    537  1.1  mrg 	      && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
    538  1.1  mrg 	      && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
    539  1.1  mrg 	      && (is_nothrow_constructible_v<_Tp, _Up>
    540  1.1  mrg 		  || is_nothrow_move_constructible_v<_Tp>
    541  1.1  mrg 		  || is_nothrow_move_constructible_v<_Er>)
    542  1.1  mrg 	constexpr expected&
    543  1.1  mrg 	operator=(_Up&& __v)
    544  1.1  mrg 	{
    545  1.1  mrg 	  _M_assign_val(std::forward<_Up>(__v));
    546  1.1  mrg 	  return *this;
    547  1.1  mrg 	}
    548  1.1  mrg 
    549  1.1  mrg       template<typename _Gr>
    550  1.1  mrg 	requires is_constructible_v<_Er, const _Gr&>
    551  1.1  mrg 	      && is_assignable_v<_Er&, const _Gr&>
    552  1.1  mrg 	      && (is_nothrow_constructible_v<_Er, const _Gr&>
    553  1.1  mrg 		  || is_nothrow_move_constructible_v<_Tp>
    554  1.1  mrg 		  || is_nothrow_move_constructible_v<_Er>)
    555  1.1  mrg 	constexpr expected&
    556  1.1  mrg 	operator=(const unexpected<_Gr>& __e)
    557  1.1  mrg 	{
    558  1.1  mrg 	  _M_assign_unex(__e.error());
    559  1.1  mrg 	  return *this;
    560  1.1  mrg 	}
    561  1.1  mrg 
    562  1.1  mrg       template<typename _Gr>
    563  1.1  mrg 	requires is_constructible_v<_Er, _Gr>
    564  1.1  mrg 	      && is_assignable_v<_Er&, _Gr>
    565  1.1  mrg 	      && (is_nothrow_constructible_v<_Er, _Gr>
    566  1.1  mrg 		  || is_nothrow_move_constructible_v<_Tp>
    567  1.1  mrg 		  || is_nothrow_move_constructible_v<_Er>)
    568  1.1  mrg 	constexpr expected&
    569  1.1  mrg 	operator=(unexpected<_Gr>&& __e)
    570  1.1  mrg 	{
    571  1.1  mrg 	  _M_assign_unex(std::move(__e).error());
    572  1.1  mrg 	  return *this;
    573  1.1  mrg 	}
    574  1.1  mrg 
    575  1.1  mrg       // modifiers
    576  1.1  mrg 
    577  1.1  mrg       template<typename... _Args>
    578  1.1  mrg 	requires is_nothrow_constructible_v<_Tp, _Args...>
    579  1.1  mrg 	constexpr _Tp&
    580  1.1  mrg 	emplace(_Args&&... __args) noexcept
    581  1.1  mrg 	{
    582  1.1  mrg 	  if (_M_has_value)
    583  1.1  mrg 	    std::destroy_at(__builtin_addressof(_M_val));
    584  1.1  mrg 	  else
    585  1.1  mrg 	    {
    586  1.1  mrg 	      std::destroy_at(__builtin_addressof(_M_unex));
    587  1.1  mrg 	      _M_has_value = true;
    588  1.1  mrg 	    }
    589  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_val),
    590  1.1  mrg 			    std::forward<_Args>(__args)...);
    591  1.1  mrg 	  return _M_val;
    592  1.1  mrg 	}
    593  1.1  mrg 
    594  1.1  mrg       template<typename _Up, typename... _Args>
    595  1.1  mrg 	requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
    596  1.1  mrg 					    _Args...>
    597  1.1  mrg 	constexpr _Tp&
    598  1.1  mrg 	emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
    599  1.1  mrg 	{
    600  1.1  mrg 	  if (_M_has_value)
    601  1.1  mrg 	    std::destroy_at(__builtin_addressof(_M_val));
    602  1.1  mrg 	  else
    603  1.1  mrg 	    {
    604  1.1  mrg 	      std::destroy_at(__builtin_addressof(_M_unex));
    605  1.1  mrg 	      _M_has_value = true;
    606  1.1  mrg 	    }
    607  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_val),
    608  1.1  mrg 			    __il, std::forward<_Args>(__args)...);
    609  1.1  mrg 	  return _M_val;
    610  1.1  mrg 	}
    611  1.1  mrg 
    612  1.1  mrg       // swap
    613  1.1  mrg       constexpr void
    614  1.1  mrg       swap(expected& __x)
    615  1.1  mrg       noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
    616  1.1  mrg 		       is_nothrow_move_constructible<_Er>,
    617  1.1  mrg 		       is_nothrow_swappable<_Tp&>,
    618  1.1  mrg 		       is_nothrow_swappable<_Er&>>)
    619  1.1  mrg       requires is_swappable_v<_Tp> && is_swappable_v<_Er>
    620  1.1  mrg 	    && is_move_constructible_v<_Tp>
    621  1.1  mrg 	    && is_move_constructible_v<_Er>
    622  1.1  mrg 	    && (is_nothrow_move_constructible_v<_Tp>
    623  1.1  mrg 		|| is_nothrow_move_constructible_v<_Er>)
    624  1.1  mrg       {
    625  1.1  mrg 	if (_M_has_value)
    626  1.1  mrg 	  {
    627  1.1  mrg 	    if (__x._M_has_value)
    628  1.1  mrg 	      {
    629  1.1  mrg 		using std::swap;
    630  1.1  mrg 		swap(_M_val, __x._M_val);
    631  1.1  mrg 	      }
    632  1.1  mrg 	    else
    633  1.1  mrg 	      this->_M_swap_val_unex(__x);
    634  1.1  mrg 	  }
    635  1.1  mrg 	else
    636  1.1  mrg 	  {
    637  1.1  mrg 	    if (__x._M_has_value)
    638  1.1  mrg 	      __x._M_swap_val_unex(*this);
    639  1.1  mrg 	    else
    640  1.1  mrg 	      {
    641  1.1  mrg 		using std::swap;
    642  1.1  mrg 		swap(_M_unex, __x._M_unex);
    643  1.1  mrg 	      }
    644  1.1  mrg 	  }
    645  1.1  mrg       }
    646  1.1  mrg 
    647  1.1  mrg       // observers
    648  1.1  mrg 
    649  1.1  mrg       [[nodiscard]]
    650  1.1  mrg       constexpr const _Tp*
    651  1.1  mrg       operator->() const noexcept
    652  1.1  mrg       {
    653  1.1  mrg 	__glibcxx_assert(_M_has_value);
    654  1.1  mrg 	return __builtin_addressof(_M_val);
    655  1.1  mrg       }
    656  1.1  mrg 
    657  1.1  mrg       [[nodiscard]]
    658  1.1  mrg       constexpr _Tp*
    659  1.1  mrg       operator->() noexcept
    660  1.1  mrg       {
    661  1.1  mrg 	__glibcxx_assert(_M_has_value);
    662  1.1  mrg 	return __builtin_addressof(_M_val);
    663  1.1  mrg       }
    664  1.1  mrg 
    665  1.1  mrg       [[nodiscard]]
    666  1.1  mrg       constexpr const _Tp&
    667  1.1  mrg       operator*() const & noexcept
    668  1.1  mrg       {
    669  1.1  mrg 	__glibcxx_assert(_M_has_value);
    670  1.1  mrg 	return _M_val;
    671  1.1  mrg       }
    672  1.1  mrg 
    673  1.1  mrg       [[nodiscard]]
    674  1.1  mrg       constexpr _Tp&
    675  1.1  mrg       operator*() & noexcept
    676  1.1  mrg       {
    677  1.1  mrg 	__glibcxx_assert(_M_has_value);
    678  1.1  mrg 	return _M_val;
    679  1.1  mrg       }
    680  1.1  mrg 
    681  1.1  mrg       [[nodiscard]]
    682  1.1  mrg       constexpr const _Tp&&
    683  1.1  mrg       operator*() const && noexcept
    684  1.1  mrg       {
    685  1.1  mrg 	__glibcxx_assert(_M_has_value);
    686  1.1  mrg 	return std::move(_M_val);
    687  1.1  mrg       }
    688  1.1  mrg 
    689  1.1  mrg       [[nodiscard]]
    690  1.1  mrg       constexpr _Tp&&
    691  1.1  mrg       operator*() && noexcept
    692  1.1  mrg       {
    693  1.1  mrg 	__glibcxx_assert(_M_has_value);
    694  1.1  mrg 	return std::move(_M_val);
    695  1.1  mrg       }
    696  1.1  mrg 
    697  1.1  mrg       [[nodiscard]]
    698  1.1  mrg       constexpr explicit
    699  1.1  mrg       operator bool() const noexcept { return _M_has_value; }
    700  1.1  mrg 
    701  1.1  mrg       [[nodiscard]]
    702  1.1  mrg       constexpr bool has_value() const noexcept { return _M_has_value; }
    703  1.1  mrg 
    704  1.1  mrg       constexpr const _Tp&
    705  1.1  mrg       value() const &
    706  1.1  mrg       {
    707  1.1  mrg 	if (_M_has_value) [[likely]]
    708  1.1  mrg 	  return _M_val;
    709  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
    710  1.1  mrg       }
    711  1.1  mrg 
    712  1.1  mrg       constexpr _Tp&
    713  1.1  mrg       value() &
    714  1.1  mrg       {
    715  1.1  mrg 	if (_M_has_value) [[likely]]
    716  1.1  mrg 	  return _M_val;
    717  1.1  mrg 	const auto& __unex = _M_unex;
    718  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
    719  1.1  mrg       }
    720  1.1  mrg 
    721  1.1  mrg       constexpr const _Tp&&
    722  1.1  mrg       value() const &&
    723  1.1  mrg       {
    724  1.1  mrg 	if (_M_has_value) [[likely]]
    725  1.1  mrg 	  return std::move(_M_val);
    726  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
    727  1.1  mrg 				  std::move(_M_unex)));
    728  1.1  mrg       }
    729  1.1  mrg 
    730  1.1  mrg       constexpr _Tp&&
    731  1.1  mrg       value() &&
    732  1.1  mrg       {
    733  1.1  mrg 	if (_M_has_value) [[likely]]
    734  1.1  mrg 	  return std::move(_M_val);
    735  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
    736  1.1  mrg 				  std::move(_M_unex)));
    737  1.1  mrg       }
    738  1.1  mrg 
    739  1.1  mrg       constexpr const _Er&
    740  1.1  mrg       error() const & noexcept
    741  1.1  mrg       {
    742  1.1  mrg 	__glibcxx_assert(!_M_has_value);
    743  1.1  mrg 	return _M_unex;
    744  1.1  mrg       }
    745  1.1  mrg 
    746  1.1  mrg       constexpr _Er&
    747  1.1  mrg       error() & noexcept
    748  1.1  mrg       {
    749  1.1  mrg 	__glibcxx_assert(!_M_has_value);
    750  1.1  mrg 	return _M_unex;
    751  1.1  mrg       }
    752  1.1  mrg 
    753  1.1  mrg       constexpr const _Er&&
    754  1.1  mrg       error() const && noexcept
    755  1.1  mrg       {
    756  1.1  mrg 	__glibcxx_assert(!_M_has_value);
    757  1.1  mrg 	return std::move(_M_unex);
    758  1.1  mrg       }
    759  1.1  mrg 
    760  1.1  mrg       constexpr _Er&&
    761  1.1  mrg       error() && noexcept
    762  1.1  mrg       {
    763  1.1  mrg 	__glibcxx_assert(!_M_has_value);
    764  1.1  mrg 	return std::move(_M_unex);
    765  1.1  mrg       }
    766  1.1  mrg 
    767  1.1  mrg       template<typename _Up>
    768  1.1  mrg 	constexpr _Tp
    769  1.1  mrg 	value_or(_Up&& __v) const &
    770  1.1  mrg 	noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
    771  1.1  mrg 			 is_nothrow_convertible<_Up, _Tp>>)
    772  1.1  mrg 	{
    773  1.1  mrg 	  static_assert( is_copy_constructible_v<_Tp> );
    774  1.1  mrg 	  static_assert( is_convertible_v<_Up, _Tp> );
    775  1.1  mrg 
    776  1.1  mrg 	  if (_M_has_value)
    777  1.1  mrg 	    return _M_val;
    778  1.1  mrg 	  return static_cast<_Tp>(std::forward<_Up>(__v));
    779  1.1  mrg 	}
    780  1.1  mrg 
    781  1.1  mrg       template<typename _Up>
    782  1.1  mrg 	constexpr _Tp
    783  1.1  mrg 	value_or(_Up&& __v) &&
    784  1.1  mrg 	noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
    785  1.1  mrg 			 is_nothrow_convertible<_Up, _Tp>>)
    786  1.1  mrg 	{
    787  1.1  mrg 	  static_assert( is_move_constructible_v<_Tp> );
    788  1.1  mrg 	  static_assert( is_convertible_v<_Up, _Tp> );
    789  1.1  mrg 
    790  1.1  mrg 	  if (_M_has_value)
    791  1.1  mrg 	    return std::move(_M_val);
    792  1.1  mrg 	  return static_cast<_Tp>(std::forward<_Up>(__v));
    793  1.1  mrg 	}
    794  1.1  mrg 
    795  1.1  mrg       // equality operators
    796  1.1  mrg 
    797  1.1  mrg       template<typename _Up, typename _Er2>
    798  1.1  mrg 	requires (!is_void_v<_Up>)
    799  1.1  mrg 	friend constexpr bool
    800  1.1  mrg 	operator==(const expected& __x, const expected<_Up, _Er2>& __y)
    801  1.1  mrg 	// FIXME: noexcept(noexcept(bool(*__x == *__y))
    802  1.1  mrg 		  // && noexcept(bool(__x.error() == __y.error())))
    803  1.1  mrg 	{
    804  1.1  mrg 	  if (__x.has_value())
    805  1.1  mrg 	    return __y.has_value() && bool(*__x == *__y);
    806  1.1  mrg 	  else
    807  1.1  mrg 	    return !__y.has_value() && bool(__x.error() == __y.error());
    808  1.1  mrg 	}
    809  1.1  mrg 
    810  1.1  mrg       template<typename _Up>
    811  1.1  mrg 	friend constexpr bool
    812  1.1  mrg 	operator==(const expected& __x, const _Up& __v)
    813  1.1  mrg 	// FIXME: noexcept(noexcept(bool(*__x == __v)))
    814  1.1  mrg 	{ return __x.has_value() && bool(*__x == __v); }
    815  1.1  mrg 
    816  1.1  mrg       template<typename _Er2>
    817  1.1  mrg 	friend constexpr bool
    818  1.1  mrg 	operator==(const expected& __x, const unexpected<_Er2>& __e)
    819  1.1  mrg 	// FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
    820  1.1  mrg 	{ return !__x.has_value() && bool(__x.error() == __e.error()); }
    821  1.1  mrg 
    822  1.1  mrg       friend constexpr void
    823  1.1  mrg       swap(expected& __x, expected& __y)
    824  1.1  mrg       noexcept(noexcept(__x.swap(__y)))
    825  1.1  mrg       requires requires {__x.swap(__y);}
    826  1.1  mrg       { __x.swap(__y); }
    827  1.1  mrg 
    828  1.1  mrg     private:
    829  1.1  mrg       template<typename, typename> friend class expected;
    830  1.1  mrg 
    831  1.1  mrg       template<typename _Vp>
    832  1.1  mrg 	constexpr void
    833  1.1  mrg 	_M_assign_val(_Vp&& __v)
    834  1.1  mrg 	{
    835  1.1  mrg 	  if (_M_has_value)
    836  1.1  mrg 	    _M_val = std::forward<_Vp>(__v);
    837  1.1  mrg 	  else
    838  1.1  mrg 	    {
    839  1.1  mrg 	      __expected::__reinit(__builtin_addressof(_M_val),
    840  1.1  mrg 				   __builtin_addressof(_M_unex),
    841  1.1  mrg 				   std::forward<_Vp>(__v));
    842  1.1  mrg 	      _M_has_value = true;
    843  1.1  mrg 	    }
    844  1.1  mrg 	}
    845  1.1  mrg 
    846  1.1  mrg       template<typename _Vp>
    847  1.1  mrg 	constexpr void
    848  1.1  mrg 	_M_assign_unex(_Vp&& __v)
    849  1.1  mrg 	{
    850  1.1  mrg 	  if (_M_has_value)
    851  1.1  mrg 	    {
    852  1.1  mrg 	      __expected::__reinit(__builtin_addressof(_M_unex),
    853  1.1  mrg 				   __builtin_addressof(_M_val),
    854  1.1  mrg 				   std::forward<_Vp>(__v));
    855  1.1  mrg 	      _M_has_value = false;
    856  1.1  mrg 	    }
    857  1.1  mrg 	  else
    858  1.1  mrg 	    _M_unex = std::forward<_Vp>(__v);
    859  1.1  mrg 	}
    860  1.1  mrg 
    861  1.1  mrg       // Swap two expected objects when only one has a value.
    862  1.1  mrg       // Precondition: this->_M_has_value && !__rhs._M_has_value
    863  1.1  mrg       constexpr void
    864  1.1  mrg       _M_swap_val_unex(expected& __rhs)
    865  1.1  mrg       noexcept(__and_v<is_nothrow_move_constructible<_Er>,
    866  1.1  mrg 		       is_nothrow_move_constructible<_Tp>>)
    867  1.1  mrg       {
    868  1.1  mrg 	if constexpr (is_nothrow_move_constructible_v<_Er>)
    869  1.1  mrg 	  {
    870  1.1  mrg 	    __expected::_Guard<_Er> __guard(__rhs._M_unex);
    871  1.1  mrg 	    std::construct_at(__builtin_addressof(__rhs._M_val),
    872  1.1  mrg 			      std::move(_M_val)); // might throw
    873  1.1  mrg 	    __rhs._M_has_value = true;
    874  1.1  mrg 	    std::destroy_at(__builtin_addressof(_M_val));
    875  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex),
    876  1.1  mrg 			      __guard.release());
    877  1.1  mrg 	    _M_has_value = false;
    878  1.1  mrg 	  }
    879  1.1  mrg 	else
    880  1.1  mrg 	  {
    881  1.1  mrg 	    __expected::_Guard<_Tp> __guard(_M_val);
    882  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex),
    883  1.1  mrg 			      std::move(__rhs._M_unex)); // might throw
    884  1.1  mrg 	    _M_has_value = false;
    885  1.1  mrg 	    std::destroy_at(__builtin_addressof(__rhs._M_unex));
    886  1.1  mrg 	    std::construct_at(__builtin_addressof(__rhs._M_val),
    887  1.1  mrg 			      __guard.release());
    888  1.1  mrg 	    __rhs._M_has_value = true;
    889  1.1  mrg 	  }
    890  1.1  mrg       }
    891  1.1  mrg 
    892  1.1  mrg       union {
    893  1.1  mrg 	_Tp _M_val;
    894  1.1  mrg 	_Er _M_unex;
    895  1.1  mrg       };
    896  1.1  mrg 
    897  1.1  mrg       bool _M_has_value;
    898  1.1  mrg     };
    899  1.1  mrg 
    900  1.1  mrg   // Partial specialization for std::expected<cv void, E>
    901  1.1  mrg   template<typename _Tp, typename _Er> requires is_void_v<_Tp>
    902  1.1  mrg     class expected<_Tp, _Er>
    903  1.1  mrg     {
    904  1.1  mrg       static_assert( __expected::__can_be_unexpected<_Er> );
    905  1.1  mrg 
    906  1.1  mrg       template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
    907  1.1  mrg 	static constexpr bool __cons_from_expected
    908  1.1  mrg 	  = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
    909  1.1  mrg 		   is_constructible<_Unex, expected<_Up, _Err>>,
    910  1.1  mrg 		   is_constructible<_Unex, const expected<_Up, _Err>&>,
    911  1.1  mrg 		   is_constructible<_Unex, const expected<_Up, _Err>>
    912  1.1  mrg 		  >;
    913  1.1  mrg 
    914  1.1  mrg     public:
    915  1.1  mrg       using value_type = _Tp;
    916  1.1  mrg       using error_type = _Er;
    917  1.1  mrg       using unexpected_type = unexpected<_Er>;
    918  1.1  mrg 
    919  1.1  mrg       template<typename _Up>
    920  1.1  mrg 	using rebind = expected<_Up, error_type>;
    921  1.1  mrg 
    922  1.1  mrg       constexpr
    923  1.1  mrg       expected() noexcept
    924  1.1  mrg       : _M_void(), _M_has_value(true)
    925  1.1  mrg       { }
    926  1.1  mrg 
    927  1.1  mrg       expected(const expected&) = default;
    928  1.1  mrg 
    929  1.1  mrg       constexpr
    930  1.1  mrg       expected(const expected& __x)
    931  1.1  mrg       noexcept(is_nothrow_copy_constructible_v<_Er>)
    932  1.1  mrg       requires is_copy_constructible_v<_Er>
    933  1.1  mrg 	    && (!is_trivially_copy_constructible_v<_Er>)
    934  1.1  mrg       : _M_void(), _M_has_value(__x._M_has_value)
    935  1.1  mrg       {
    936  1.1  mrg 	if (!_M_has_value)
    937  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
    938  1.1  mrg       }
    939  1.1  mrg 
    940  1.1  mrg       expected(expected&&) = default;
    941  1.1  mrg 
    942  1.1  mrg       constexpr
    943  1.1  mrg       expected(expected&& __x)
    944  1.1  mrg       noexcept(is_nothrow_move_constructible_v<_Er>)
    945  1.1  mrg       requires is_move_constructible_v<_Er>
    946  1.1  mrg 	    && (!is_trivially_move_constructible_v<_Er>)
    947  1.1  mrg       : _M_void(), _M_has_value(__x._M_has_value)
    948  1.1  mrg       {
    949  1.1  mrg 	if (!_M_has_value)
    950  1.1  mrg 	  std::construct_at(__builtin_addressof(_M_unex),
    951  1.1  mrg 			    std::move(__x)._M_unex);
    952  1.1  mrg       }
    953  1.1  mrg 
    954  1.1  mrg       template<typename _Up, typename _Gr>
    955  1.1  mrg 	requires is_void_v<_Up>
    956  1.1  mrg 	      && is_constructible_v<_Er, const _Gr&>
    957  1.1  mrg 	      && (!__cons_from_expected<_Up, _Gr>)
    958  1.1  mrg 	constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
    959  1.1  mrg 	expected(const expected<_Up, _Gr>& __x)
    960  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
    961  1.1  mrg 	: _M_void(), _M_has_value(__x._M_has_value)
    962  1.1  mrg 	{
    963  1.1  mrg 	  if (!_M_has_value)
    964  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
    965  1.1  mrg 	}
    966  1.1  mrg 
    967  1.1  mrg       template<typename _Up, typename _Gr>
    968  1.1  mrg 	requires is_void_v<_Up>
    969  1.1  mrg 	      && is_constructible_v<_Er, _Gr>
    970  1.1  mrg 	      && (!__cons_from_expected<_Up, _Gr>)
    971  1.1  mrg 	constexpr explicit(!is_convertible_v<_Gr, _Er>)
    972  1.1  mrg 	expected(expected<_Up, _Gr>&& __x)
    973  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Gr>)
    974  1.1  mrg 	: _M_void(), _M_has_value(__x._M_has_value)
    975  1.1  mrg 	{
    976  1.1  mrg 	  if (!_M_has_value)
    977  1.1  mrg 	    std::construct_at(__builtin_addressof(_M_unex),
    978  1.1  mrg 			      std::move(__x)._M_unex);
    979  1.1  mrg 	}
    980  1.1  mrg 
    981  1.1  mrg       template<typename _Gr = _Er>
    982  1.1  mrg 	requires is_constructible_v<_Er, const _Gr&>
    983  1.1  mrg 	constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
    984  1.1  mrg 	expected(const unexpected<_Gr>& __u)
    985  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
    986  1.1  mrg 	: _M_unex(__u.error()), _M_has_value(false)
    987  1.1  mrg 	{ }
    988  1.1  mrg 
    989  1.1  mrg       template<typename _Gr = _Er>
    990  1.1  mrg 	requires is_constructible_v<_Er, _Gr>
    991  1.1  mrg 	constexpr explicit(!is_convertible_v<_Gr, _Er>)
    992  1.1  mrg 	expected(unexpected<_Gr>&& __u)
    993  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Gr>)
    994  1.1  mrg 	: _M_unex(std::move(__u).error()), _M_has_value(false)
    995  1.1  mrg 	{ }
    996  1.1  mrg 
    997  1.1  mrg       constexpr explicit
    998  1.1  mrg       expected(in_place_t) noexcept
    999  1.1  mrg       : expected()
   1000  1.1  mrg       { }
   1001  1.1  mrg 
   1002  1.1  mrg       template<typename... _Args>
   1003  1.1  mrg 	requires is_constructible_v<_Er, _Args...>
   1004  1.1  mrg 	constexpr explicit
   1005  1.1  mrg 	expected(unexpect_t, _Args&&... __args)
   1006  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, _Args...>)
   1007  1.1  mrg 	: _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
   1008  1.1  mrg 	{ }
   1009  1.1  mrg 
   1010  1.1  mrg       template<typename _Up, typename... _Args>
   1011  1.1  mrg 	requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
   1012  1.1  mrg 	constexpr explicit
   1013  1.1  mrg 	expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
   1014  1.1  mrg 	noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
   1015  1.1  mrg 					    _Args...>)
   1016  1.1  mrg 	: _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
   1017  1.1  mrg 	{ }
   1018  1.1  mrg 
   1019  1.1  mrg       constexpr ~expected() = default;
   1020  1.1  mrg 
   1021  1.1  mrg       constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
   1022  1.1  mrg       {
   1023  1.1  mrg 	if (!_M_has_value)
   1024  1.1  mrg 	  std::destroy_at(__builtin_addressof(_M_unex));
   1025  1.1  mrg       }
   1026  1.1  mrg 
   1027  1.1  mrg       // assignment
   1028  1.1  mrg 
   1029  1.1  mrg       expected& operator=(const expected&) = delete;
   1030  1.1  mrg 
   1031  1.1  mrg       constexpr expected&
   1032  1.1  mrg       operator=(const expected& __x)
   1033  1.1  mrg       noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
   1034  1.1  mrg 		       is_nothrow_copy_assignable<_Er>>)
   1035  1.1  mrg       requires is_copy_constructible_v<_Er>
   1036  1.1  mrg 	    && is_copy_assignable_v<_Er>
   1037  1.1  mrg       {
   1038  1.1  mrg 	if (__x._M_has_value)
   1039  1.1  mrg 	  emplace();
   1040  1.1  mrg 	else
   1041  1.1  mrg 	  _M_assign_unex(__x._M_unex);
   1042  1.1  mrg 	return *this;
   1043  1.1  mrg       }
   1044  1.1  mrg 
   1045  1.1  mrg       constexpr expected&
   1046  1.1  mrg       operator=(expected&& __x)
   1047  1.1  mrg       noexcept(__and_v<is_nothrow_move_constructible<_Er>,
   1048  1.1  mrg 		       is_nothrow_move_assignable<_Er>>)
   1049  1.1  mrg       requires is_move_constructible_v<_Er>
   1050  1.1  mrg 	    && is_move_assignable_v<_Er>
   1051  1.1  mrg       {
   1052  1.1  mrg 	if (__x._M_has_value)
   1053  1.1  mrg 	  emplace();
   1054  1.1  mrg 	else
   1055  1.1  mrg 	  _M_assign_unex(std::move(__x._M_unex));
   1056  1.1  mrg 	return *this;
   1057  1.1  mrg       }
   1058  1.1  mrg 
   1059  1.1  mrg       template<typename _Gr>
   1060  1.1  mrg 	requires is_constructible_v<_Er, const _Gr&>
   1061  1.1  mrg 	      && is_assignable_v<_Er&, const _Gr&>
   1062  1.1  mrg 	constexpr expected&
   1063  1.1  mrg 	operator=(const unexpected<_Gr>& __e)
   1064  1.1  mrg 	{
   1065  1.1  mrg 	  _M_assign_unex(__e.error());
   1066  1.1  mrg 	  return *this;
   1067  1.1  mrg 	}
   1068  1.1  mrg 
   1069  1.1  mrg       template<typename _Gr>
   1070  1.1  mrg 	requires is_constructible_v<_Er, _Gr>
   1071  1.1  mrg 	      && is_assignable_v<_Er&, _Gr>
   1072  1.1  mrg 	constexpr expected&
   1073  1.1  mrg 	operator=(unexpected<_Gr>&& __e)
   1074  1.1  mrg 	{
   1075  1.1  mrg 	  _M_assign_unex(std::move(__e.error()));
   1076  1.1  mrg 	  return *this;
   1077  1.1  mrg 	}
   1078  1.1  mrg 
   1079  1.1  mrg       // modifiers
   1080  1.1  mrg 
   1081  1.1  mrg       constexpr void
   1082  1.1  mrg       emplace() noexcept
   1083  1.1  mrg       {
   1084  1.1  mrg 	if (!_M_has_value)
   1085  1.1  mrg 	  {
   1086  1.1  mrg 	    std::destroy_at(__builtin_addressof(_M_unex));
   1087  1.1  mrg 	    _M_has_value = true;
   1088  1.1  mrg 	  }
   1089  1.1  mrg       }
   1090  1.1  mrg 
   1091  1.1  mrg       // swap
   1092  1.1  mrg       constexpr void
   1093  1.1  mrg       swap(expected& __x)
   1094  1.1  mrg       noexcept(__and_v<is_nothrow_swappable<_Er&>,
   1095  1.1  mrg 		       is_nothrow_move_constructible<_Er>>)
   1096  1.1  mrg       requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
   1097  1.1  mrg       {
   1098  1.1  mrg 	if (_M_has_value)
   1099  1.1  mrg 	  {
   1100  1.1  mrg 	    if (!__x._M_has_value)
   1101  1.1  mrg 	      {
   1102  1.1  mrg 		std::construct_at(__builtin_addressof(_M_unex),
   1103  1.1  mrg 				  std::move(__x._M_unex)); // might throw
   1104  1.1  mrg 		std::destroy_at(__builtin_addressof(__x._M_unex));
   1105  1.1  mrg 		_M_has_value = false;
   1106  1.1  mrg 		__x._M_has_value = true;
   1107  1.1  mrg 	      }
   1108  1.1  mrg 	  }
   1109  1.1  mrg 	else
   1110  1.1  mrg 	  {
   1111  1.1  mrg 	    if (__x._M_has_value)
   1112  1.1  mrg 	      {
   1113  1.1  mrg 		std::construct_at(__builtin_addressof(__x._M_unex),
   1114  1.1  mrg 				  std::move(_M_unex)); // might throw
   1115  1.1  mrg 		std::destroy_at(__builtin_addressof(_M_unex));
   1116  1.1  mrg 		_M_has_value = true;
   1117  1.1  mrg 		__x._M_has_value = false;
   1118  1.1  mrg 	      }
   1119  1.1  mrg 	    else
   1120  1.1  mrg 	      {
   1121  1.1  mrg 		using std::swap;
   1122  1.1  mrg 		swap(_M_unex, __x._M_unex);
   1123  1.1  mrg 	      }
   1124  1.1  mrg 	  }
   1125  1.1  mrg       }
   1126  1.1  mrg 
   1127  1.1  mrg       // observers
   1128  1.1  mrg 
   1129  1.1  mrg       [[nodiscard]]
   1130  1.1  mrg       constexpr explicit
   1131  1.1  mrg       operator bool() const noexcept { return _M_has_value; }
   1132  1.1  mrg 
   1133  1.1  mrg       [[nodiscard]]
   1134  1.1  mrg       constexpr bool has_value() const noexcept { return _M_has_value; }
   1135  1.1  mrg 
   1136  1.1  mrg       constexpr void
   1137  1.1  mrg       operator*() const noexcept { __glibcxx_assert(_M_has_value); }
   1138  1.1  mrg 
   1139  1.1  mrg       constexpr void
   1140  1.1  mrg       value() const&
   1141  1.1  mrg       {
   1142  1.1  mrg 	if (_M_has_value) [[likely]]
   1143  1.1  mrg 	  return;
   1144  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
   1145  1.1  mrg       }
   1146  1.1  mrg 
   1147  1.1  mrg       constexpr void
   1148  1.1  mrg       value() &&
   1149  1.1  mrg       {
   1150  1.1  mrg 	if (_M_has_value) [[likely]]
   1151  1.1  mrg 	  return;
   1152  1.1  mrg 	_GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
   1153  1.1  mrg       }
   1154  1.1  mrg 
   1155  1.1  mrg       constexpr const _Er&
   1156  1.1  mrg       error() const & noexcept
   1157  1.1  mrg       {
   1158  1.1  mrg 	__glibcxx_assert(!_M_has_value);
   1159  1.1  mrg 	return _M_unex;
   1160  1.1  mrg       }
   1161  1.1  mrg 
   1162  1.1  mrg       constexpr _Er&
   1163  1.1  mrg       error() & noexcept
   1164  1.1  mrg       {
   1165  1.1  mrg 	__glibcxx_assert(!_M_has_value);
   1166  1.1  mrg 	return _M_unex;
   1167  1.1  mrg       }
   1168  1.1  mrg 
   1169  1.1  mrg       constexpr const _Er&&
   1170  1.1  mrg       error() const && noexcept
   1171  1.1  mrg       {
   1172  1.1  mrg 	__glibcxx_assert(!_M_has_value);
   1173  1.1  mrg 	return std::move(_M_unex);
   1174  1.1  mrg       }
   1175  1.1  mrg 
   1176  1.1  mrg       constexpr _Er&&
   1177  1.1  mrg       error() && noexcept
   1178  1.1  mrg       {
   1179  1.1  mrg 	__glibcxx_assert(!_M_has_value);
   1180  1.1  mrg 	return std::move(_M_unex);
   1181  1.1  mrg       }
   1182  1.1  mrg 
   1183  1.1  mrg       // equality operators
   1184  1.1  mrg 
   1185  1.1  mrg       template<typename _Up, typename _Er2>
   1186  1.1  mrg 	requires is_void_v<_Up>
   1187  1.1  mrg 	friend constexpr bool
   1188  1.1  mrg 	operator==(const expected& __x, const expected<_Up, _Er2>& __y)
   1189  1.1  mrg 	// FIXME: noexcept(noexcept(bool(__x.error() == __y.error())))
   1190  1.1  mrg 	{
   1191  1.1  mrg 	  if (__x.has_value())
   1192  1.1  mrg 	    return __y.has_value();
   1193  1.1  mrg 	  else
   1194  1.1  mrg 	    return !__y.has_value() && bool(__x.error() == __y.error());
   1195  1.1  mrg 	}
   1196  1.1  mrg 
   1197  1.1  mrg       template<typename _Er2>
   1198  1.1  mrg 	friend constexpr bool
   1199  1.1  mrg 	operator==(const expected& __x, const unexpected<_Er2>& __e)
   1200  1.1  mrg 	// FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
   1201  1.1  mrg 	{ return !__x.has_value() && bool(__x.error() == __e.error()); }
   1202  1.1  mrg 
   1203  1.1  mrg       friend constexpr void
   1204  1.1  mrg       swap(expected& __x, expected& __y)
   1205  1.1  mrg       noexcept(noexcept(__x.swap(__y)))
   1206  1.1  mrg       requires requires { __x.swap(__y); }
   1207  1.1  mrg       { __x.swap(__y); }
   1208  1.1  mrg 
   1209  1.1  mrg     private:
   1210  1.1  mrg       template<typename, typename> friend class expected;
   1211  1.1  mrg 
   1212  1.1  mrg       template<typename _Vp>
   1213  1.1  mrg 	constexpr void
   1214  1.1  mrg 	_M_assign_unex(_Vp&& __v)
   1215  1.1  mrg 	{
   1216  1.1  mrg 	  if (_M_has_value)
   1217  1.1  mrg 	    {
   1218  1.1  mrg 	      std::construct_at(__builtin_addressof(_M_unex),
   1219  1.1  mrg 				std::forward<_Vp>(__v));
   1220  1.1  mrg 	      _M_has_value = false;
   1221  1.1  mrg 	    }
   1222  1.1  mrg 	  else
   1223  1.1  mrg 	    _M_unex = std::forward<_Vp>(__v);
   1224  1.1  mrg 	}
   1225  1.1  mrg 
   1226  1.1  mrg 
   1227  1.1  mrg       union {
   1228  1.1  mrg 	struct { } _M_void;
   1229  1.1  mrg 	_Er _M_unex;
   1230  1.1  mrg       };
   1231  1.1  mrg 
   1232  1.1  mrg       bool _M_has_value;
   1233  1.1  mrg     };
   1234  1.1  mrg   /// @}
   1235  1.1  mrg 
   1236  1.1  mrg _GLIBCXX_END_NAMESPACE_VERSION
   1237  1.1  mrg } // namespace std
   1238  1.1  mrg 
   1239  1.1  mrg #endif // C++23
   1240  1.1  mrg #endif // _GLIBCXX_EXPECTED
   1241