xref: /llvm-project/libcxx/include/__cxx03/__expected/expected.h (revision ce7771902dc50d900de639d499a60486b83f70e0)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CXX03___EXPECTED_EXPECTED_H
10 #define _LIBCPP___CXX03___EXPECTED_EXPECTED_H
11 
12 #include <__cxx03/__assert>
13 #include <__cxx03/__config>
14 #include <__cxx03/__expected/bad_expected_access.h>
15 #include <__cxx03/__expected/unexpect.h>
16 #include <__cxx03/__expected/unexpected.h>
17 #include <__cxx03/__functional/invoke.h>
18 #include <__cxx03/__memory/addressof.h>
19 #include <__cxx03/__memory/construct_at.h>
20 #include <__cxx03/__type_traits/conjunction.h>
21 #include <__cxx03/__type_traits/disjunction.h>
22 #include <__cxx03/__type_traits/integral_constant.h>
23 #include <__cxx03/__type_traits/is_assignable.h>
24 #include <__cxx03/__type_traits/is_constructible.h>
25 #include <__cxx03/__type_traits/is_convertible.h>
26 #include <__cxx03/__type_traits/is_function.h>
27 #include <__cxx03/__type_traits/is_nothrow_assignable.h>
28 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
29 #include <__cxx03/__type_traits/is_reference.h>
30 #include <__cxx03/__type_traits/is_same.h>
31 #include <__cxx03/__type_traits/is_swappable.h>
32 #include <__cxx03/__type_traits/is_trivially_constructible.h>
33 #include <__cxx03/__type_traits/is_trivially_destructible.h>
34 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
35 #include <__cxx03/__type_traits/is_void.h>
36 #include <__cxx03/__type_traits/lazy.h>
37 #include <__cxx03/__type_traits/negation.h>
38 #include <__cxx03/__type_traits/remove_cv.h>
39 #include <__cxx03/__type_traits/remove_cvref.h>
40 #include <__cxx03/__utility/as_const.h>
41 #include <__cxx03/__utility/exception_guard.h>
42 #include <__cxx03/__utility/forward.h>
43 #include <__cxx03/__utility/in_place.h>
44 #include <__cxx03/__utility/move.h>
45 #include <__cxx03/__utility/swap.h>
46 #include <__cxx03/__verbose_abort>
47 #include <__cxx03/initializer_list>
48 
49 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50 #  pragma GCC system_header
51 #endif
52 
53 _LIBCPP_PUSH_MACROS
54 #include <__cxx03/__undef_macros>
55 
56 #if _LIBCPP_STD_VER >= 23
57 
58 _LIBCPP_BEGIN_NAMESPACE_STD
59 
60 template <class _Tp, class _Err>
61 class expected;
62 
63 template <class _Tp>
64 struct __is_std_expected : false_type {};
65 
66 template <class _Tp, class _Err>
67 struct __is_std_expected<expected<_Tp, _Err>> : true_type {};
68 
69 struct __expected_construct_in_place_from_invoke_tag {};
70 struct __expected_construct_unexpected_from_invoke_tag {};
71 
72 template <class _Err, class _Arg>
73 _LIBCPP_HIDE_FROM_ABI void __throw_bad_expected_access(_Arg&& __arg) {
74 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
75   throw bad_expected_access<_Err>(std::forward<_Arg>(__arg));
76 #  else
77   (void)__arg;
78   _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");
79 #  endif
80 }
81 
82 // If parameter type `_Tp` of `__conditional_no_unique_address` is neither
83 // copyable nor movable, a constructor with this tag is provided. For that
84 // constructor, the user has to provide a function and arguments. The function
85 // must return an object of type `_Tp`. When the function is invoked by the
86 // constructor, guaranteed copy elision kicks in and the `_Tp` is constructed
87 // in place.
88 struct __conditional_no_unique_address_invoke_tag {};
89 
90 // This class implements an object with `[[no_unique_address]]` conditionally applied to it,
91 // based on the value of `_NoUnique`.
92 //
93 // A member of this class must always have `[[no_unique_address]]` applied to
94 // it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case
95 // would not have any effect. In the `false` case, the `__v` is not
96 // `[[no_unique_address]]`, so nullifies the effects of the "outer"
97 // `[[no_unique_address]]` regarding data layout.
98 //
99 // If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`.
100 template <bool _NoUnique, class _Tp>
101 struct __conditional_no_unique_address;
102 
103 template <class _Tp>
104 struct __conditional_no_unique_address<true, _Tp> {
105   template <class... _Args>
106   _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
107       : __v(std::forward<_Args>(__args)...) {}
108 
109   template <class _Func, class... _Args>
110   _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
111       __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
112       : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
113 
114   _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v;
115 };
116 
117 template <class _Tp>
118 struct __conditional_no_unique_address<false, _Tp> {
119   template <class... _Args>
120   _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
121       : __v(std::forward<_Args>(__args)...) {}
122 
123   template <class _Func, class... _Args>
124   _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
125       __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
126       : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
127 
128   _Tp __v;
129 };
130 
131 // This function returns whether the type `_Second` can be stuffed into the tail padding
132 // of the `_First` type if both of them are given `[[no_unique_address]]`.
133 template <class _First, class _Second>
134 inline constexpr bool __fits_in_tail_padding = []() {
135   struct __x {
136     _LIBCPP_NO_UNIQUE_ADDRESS _First __first;
137     _LIBCPP_NO_UNIQUE_ADDRESS _Second __second;
138   };
139   return sizeof(__x) == sizeof(_First);
140 }();
141 
142 // This class implements the storage used by `std::expected`. We have a few
143 // goals for this storage:
144 // 1. Whenever the underlying {_Tp | _Unex} combination has free bytes in its
145 //    tail padding, we should reuse it to store the bool discriminator of the
146 //    expected, so as to save space.
147 // 2. Whenever the `expected<_Tp, _Unex>` as a whole has free bytes in its tail
148 //    padding, we should allow an object following the expected to be stored in
149 //    its tail padding.
150 // 3. However, we never want a user object (say `X`) that would follow an
151 //    `expected<_Tp, _Unex>` to be stored in the padding bytes of the
152 //    underlying {_Tp | _Unex} union, if any. That is because we use
153 //    `construct_at` on that union, which would end up overwriting the `X`
154 //    member if it is stored in the tail padding of the union.
155 //
156 // To achieve this, `__expected_base`'s logic is implemented in an inner
157 // `__repr` class. `__expected_base` holds one `__repr` member which is
158 // conditionally `[[no_unique_address]]`. The `__repr` class holds the
159 // underlying {_Tp | _Unex} union and a boolean "has value" flag.
160 //
161 // Which one of the `__repr_`/`__union_` members is `[[no_unique_address]]`
162 // depends on whether the "has value" boolean fits into the tail padding of
163 // the underlying {_Tp | _Unex} union:
164 //
165 // - In case the "has value" bool fits into the tail padding of the union, the
166 //   whole `__repr_` member is _not_ `[[no_unique_address]]` as it needs to be
167 //   transparently replaced on `emplace()`/`swap()` etc.
168 // - In case the "has value" bool does not fit into the tail padding of the
169 //   union, only the union member must be transparently replaced (therefore is
170 //   _not_ `[[no_unique_address]]`) and the "has value" flag must be adjusted
171 //   manually.
172 //
173 // This way, the member that is transparently replaced on mutating operations
174 // is never `[[no_unique_address]]`, satisfying the requirements from
175 // "[basic.life]" in the standard.
176 //
177 // Stripped away of all superfluous elements, the layout of `__expected_base`
178 // then looks like this:
179 //
180 //     template <class Tp, class Err>
181 //     class expected_base {
182 //       union union_t {
183 //         [[no_unique_address]] Tp val;
184 //         [[no_unique_address]] Err unex;
185 //       };
186 //
187 //       static constexpr bool put_flag_in_tail                    = fits_in_tail_padding<union_t, bool>;
188 //       static constexpr bool allow_reusing_expected_tail_padding = !put_flag_in_tail;
189 //
190 //       struct repr {
191 //       private:
192 //         // If "has value" fits into the tail, this should be
193 //         // `[[no_unique_address]]`, otherwise not.
194 //         [[no_unique_address]] conditional_no_unique_address<
195 //             put_flag_in_tail,
196 //             union_t>::type union_;
197 //         [[no_unique_address]] bool has_val_;
198 //       };
199 //
200 //     protected:
201 //       // If "has value" fits into the tail, this must _not_ be
202 //       // `[[no_unique_address]]` so that we fill out the
203 //       // complete `expected` object.
204 //       [[no_unique_address]] conditional_no_unique_address<
205 //           allow_reusing_expected_tail_padding,
206 //           repr>::type repr_;
207 //     };
208 //
209 template <class _Tp, class _Err>
210 class __expected_base {
211   // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
212   // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
213   // it's not clear that it's implementable, given that the function is allowed to clobber
214   // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
215   union __union_t {
216     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
217     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
218       requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
219                is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
220     = default;
221     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
222     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
223       requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
224                is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
225     = default;
226     _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
227     _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&)      = delete;
228 
229     template <class... _Args>
230     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t, _Args&&... __args)
231         : __val_(std::forward<_Args>(__args)...) {}
232 
233     template <class... _Args>
234     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
235         : __unex_(std::forward<_Args>(__args)...) {}
236 
237     template <class _Func, class... _Args>
238     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
239         std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args)
240         : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
241 
242     template <class _Func, class... _Args>
243     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
244         std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
245         : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
246 
247     _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
248       requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
249     = default;
250 
251     // __repr's destructor handles this
252     _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {}
253 
254     _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
255     _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
256   };
257 
258   static constexpr bool __put_flag_in_tail                    = __fits_in_tail_padding<__union_t, bool>;
259   static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
260 
261   struct __repr {
262     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
263 
264     template <class... _Args>
265     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag, _Args&&... __args)
266         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
267 
268     template <class... _Args>
269     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
270         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
271 
272     template <class... _Args>
273     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag,
274                                                     _Args&&... __args)
275         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
276 
277     template <class... _Args>
278     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
279                                                     _Args&&... __args)
280         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
281 
282     // The return value of `__make_union` must be constructed in place in the
283     // `__v` member of `__union_`, relying on guaranteed copy elision. To do
284     // this, the `__conditional_no_unique_address_invoke_tag` constructor is
285     // called with a lambda that is immediately called inside
286     // `__conditional_no_unique_address`'s constructor.
287     template <class _OtherUnion>
288     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
289       requires(__allow_reusing_expected_tail_padding)
290         : __union_(__conditional_no_unique_address_invoke_tag{},
291                    [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
292           __has_val_(__has_val) {}
293 
294     _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
295     _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
296       requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
297                is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
298     = default;
299     _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
300     _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
301       requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
302                is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
303     = default;
304 
305     _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
306     _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&)      = delete;
307 
308     _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
309       requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
310     = default;
311 
312     _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
313       requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
314     {
315       __destroy_union_member();
316     }
317 
318     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
319       requires(__allow_reusing_expected_tail_padding &&
320                (is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>))
321     {
322       // Note: Since the destructor of the union is trivial, this does nothing
323       // except to end the lifetime of the union.
324       std::destroy_at(&__union_.__v);
325     }
326 
327     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
328       requires(__allow_reusing_expected_tail_padding &&
329                (!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>))
330     {
331       __destroy_union_member();
332       std::destroy_at(&__union_.__v);
333     }
334 
335     template <class... _Args>
336     _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t, _Args&&... __args)
337       requires(__allow_reusing_expected_tail_padding)
338     {
339       std::construct_at(&__union_.__v, in_place, std::forward<_Args>(__args)...);
340       __has_val_ = true;
341     }
342 
343     template <class... _Args>
344     _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
345       requires(__allow_reusing_expected_tail_padding)
346     {
347       std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
348       __has_val_ = false;
349     }
350 
351   private:
352     template <class, class>
353     friend class __expected_base;
354 
355     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
356       requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
357     {
358       if (__has_val_) {
359         std::destroy_at(std::addressof(__union_.__v.__val_));
360       } else {
361         std::destroy_at(std::addressof(__union_.__v.__unex_));
362       }
363     }
364 
365     template <class _OtherUnion>
366     _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
367       requires(__allow_reusing_expected_tail_padding)
368     {
369       if (__has_val)
370         return __union_t(in_place, std::forward<_OtherUnion>(__other).__val_);
371       else
372         return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
373     }
374 
375     _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
376     _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
377   };
378 
379   template <class _OtherUnion>
380   _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
381     requires(__put_flag_in_tail)
382   {
383     if (__has_val)
384       return __repr(in_place, std::forward<_OtherUnion>(__other).__val_);
385     else
386       return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
387   }
388 
389 protected:
390   template <class... _Args>
391   _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(_Args&&... __args)
392       : __repr_(in_place, std::forward<_Args>(__args)...) {}
393 
394   // In case we copy/move construct from another `expected` we need to create
395   // our `expected` so that it either has a value or not, depending on the "has
396   // value" flag of the other `expected`. To do this without falling back on
397   // `std::construct_at` we rely on guaranteed copy elision using two helper
398   // functions `__make_repr` and `__make_union`. There have to be two since
399   // there are two data layouts with different members being
400   // `[[no_unique_address]]`. GCC (as of version 13) does not do guaranteed
401   // copy elision when initializing `[[no_unique_address]]` members. The two
402   // cases are:
403   //
404   // - `__make_repr`: This is used when the "has value" flag lives in the tail
405   //   of the union. In this case, the `__repr` member is _not_
406   //   `[[no_unique_address]]`.
407   // - `__make_union`: When the "has value" flag does _not_ fit in the tail of
408   //   the union, the `__repr` member is `[[no_unique_address]]` and the union
409   //   is not.
410   //
411   // This constructor "catches" the first case and leaves the second case to
412   // `__union_t`, its constructors and `__make_union`.
413   template <class _OtherUnion>
414   _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(bool __has_val, _OtherUnion&& __other)
415     requires(__put_flag_in_tail)
416       : __repr_(__conditional_no_unique_address_invoke_tag{},
417                 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
418 
419   _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
420     if constexpr (__put_flag_in_tail)
421       std::destroy_at(&__repr_.__v);
422     else
423       __repr_.__v.__destroy_union();
424   }
425 
426   template <class _Tag, class... _Args>
427   _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
428     if constexpr (__put_flag_in_tail)
429       std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
430     else
431       __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
432   }
433 
434   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
435   _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
436   _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
437   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __val() { return __repr_.__v.__union_.__v.__val_; }
438   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& __val() const { return __repr_.__v.__union_.__v.__val_; }
439   _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
440   _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
441 
442 private:
443   _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
444 };
445 
446 template <class _Tp, class _Err>
447 class expected : private __expected_base<_Tp, _Err> {
448   static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> &&
449                     !is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value &&
450                     __valid_std_unexpected<_Err>::value,
451                 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
452                 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
453                 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
454                 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
455                 "template argument for unexpected is ill-formed.");
456 
457   template <class _Up, class _OtherErr>
458   friend class expected;
459 
460   using __base = __expected_base<_Tp, _Err>;
461 
462 public:
463   using value_type      = _Tp;
464   using error_type      = _Err;
465   using unexpected_type = unexpected<_Err>;
466 
467   using __trivially_relocatable =
468       __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,
469                       expected,
470                       void>;
471 
472   template <class _Up>
473   using rebind = expected<_Up, error_type>;
474 
475   // [expected.object.ctor], constructors
476   _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened
477     requires is_default_constructible_v<_Tp>
478       : __base(in_place) {}
479 
480   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
481 
482   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
483     requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&
484              is_trivially_copy_constructible_v<_Err>)
485   = default;
486 
487   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
488       is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened
489     requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
490              !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
491       : __base(__other.__has_val(), __other.__union()) {}
492 
493   _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
494     requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&
495              is_trivially_move_constructible_v<_Err>)
496   = default;
497 
498   _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(
499       is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)
500     requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
501              !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))
502       : __base(__other.__has_val(), std::move(__other.__union())) {}
503 
504 private:
505   template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>
506   using __can_convert = _And<
507       is_constructible<_Tp, _UfQual>,
508       is_constructible<_Err, _OtherErrQual>,
509       _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
510           _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>, // use the copy constructor instead, see #92676
511                 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
512                 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
513                 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
514                 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
515                 _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>,
516                 _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>,
517                 _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>,
518                 _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>,
519           true_type>,
520       _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
521       _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
522       _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
523       _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >;
524 
525   template <class _Func, class... _Args>
526   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
527       std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
528       : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
529 
530   template <class _Func, class... _Args>
531   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
532       std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
533       : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
534 
535 public:
536   template <class _Up, class _OtherErr>
537     requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
538   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> ||
539                                            !is_convertible_v<const _OtherErr&, _Err>)
540       expected(const expected<_Up, _OtherErr>& __other) noexcept(
541           is_nothrow_constructible_v<_Tp, const _Up&> &&
542           is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
543       : __base(__other.__has_val(), __other.__union()) {}
544 
545   template <class _Up, class _OtherErr>
546     requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value
547   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>)
548       expected(expected<_Up, _OtherErr>&& __other) noexcept(
549           is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
550       : __base(__other.__has_val(), std::move(__other.__union())) {}
551 
552   template <class _Up = _Tp>
553     requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&
554              is_constructible_v<_Tp, _Up> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
555              (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))
556   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)
557       expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened
558       : __base(in_place, std::forward<_Up>(__u)) {}
559 
560   template <class _OtherErr>
561     requires is_constructible_v<_Err, const _OtherErr&>
562   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
563       const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
564       : __base(unexpect, __unex.error()) {}
565 
566   template <class _OtherErr>
567     requires is_constructible_v<_Err, _OtherErr>
568   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
569       expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
570       : __base(unexpect, std::move(__unex.error())) {}
571 
572   template <class... _Args>
573     requires is_constructible_v<_Tp, _Args...>
574   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(
575       is_nothrow_constructible_v<_Tp, _Args...>) // strengthened
576       : __base(in_place, std::forward<_Args>(__args)...) {}
577 
578   template <class _Up, class... _Args>
579     requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >
580   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
581       is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened
582       : __base(in_place, __il, std::forward<_Args>(__args)...) {}
583 
584   template <class... _Args>
585     requires is_constructible_v<_Err, _Args...>
586   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
587       is_nothrow_constructible_v<_Err, _Args...>) // strengthened
588       : __base(unexpect, std::forward<_Args>(__args)...) {}
589 
590   template <class _Up, class... _Args>
591     requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
592   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
593       is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
594       : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
595 
596   // [expected.object.dtor], destructor
597 
598   _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
599 
600 private:
601   template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>
602   _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {
603     if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {
604       this->__destroy();
605       this->__construct(_Tag{}, std::forward<_Args>(__args)...);
606     } else if constexpr (is_nothrow_move_constructible_v<_T1>) {
607       _T1 __tmp(std::forward<_Args>(__args)...);
608       this->__destroy();
609       this->__construct(_Tag{}, std::move(__tmp));
610     } else {
611       static_assert(
612           is_nothrow_move_constructible_v<_T2>,
613           "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
614           "be reverted to the previous state in case an exception is thrown during the assignment.");
615       _T2 __tmp(std::move(__oldval));
616       this->__destroy();
617       auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });
618       this->__construct(_Tag{}, std::forward<_Args>(__args)...);
619       __trans.__complete();
620     }
621   }
622 
623 public:
624   // [expected.object.assign], assignment
625   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
626 
627   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
628       is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&
629       is_nothrow_copy_constructible_v<_Err>) // strengthened
630     requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&
631              is_copy_constructible_v<_Err> &&
632              (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
633   {
634     if (this->__has_val() && __rhs.__has_val()) {
635       this->__val() = __rhs.__val();
636     } else if (this->__has_val()) {
637       __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());
638     } else if (__rhs.__has_val()) {
639       __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());
640     } else {
641       this->__unex() = __rhs.__unex();
642     }
643     return *this;
644   }
645 
646   _LIBCPP_HIDE_FROM_ABI constexpr expected&
647   operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&
648                                        is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
649     requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&
650              is_move_assignable_v<_Err> &&
651              (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
652   {
653     if (this->__has_val() && __rhs.__has_val()) {
654       this->__val() = std::move(__rhs.__val());
655     } else if (this->__has_val()) {
656       __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));
657     } else if (__rhs.__has_val()) {
658       __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));
659     } else {
660       this->__unex() = std::move(__rhs.__unex());
661     }
662     return *this;
663   }
664 
665   template <class _Up = _Tp>
666   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)
667     requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
668              is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&
669              (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||
670               is_nothrow_move_constructible_v<_Err>))
671   {
672     if (this->__has_val()) {
673       this->__val() = std::forward<_Up>(__v);
674     } else {
675       __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));
676     }
677     return *this;
678   }
679 
680 private:
681   template <class _OtherErrQual>
682   static constexpr bool __can_assign_from_unexpected =
683       _And< is_constructible<_Err, _OtherErrQual>,
684             is_assignable<_Err&, _OtherErrQual>,
685             _Lazy<_Or,
686                   is_nothrow_constructible<_Err, _OtherErrQual>,
687                   is_nothrow_move_constructible<_Tp>,
688                   is_nothrow_move_constructible<_Err>> >::value;
689 
690 public:
691   template <class _OtherErr>
692     requires(__can_assign_from_unexpected<const _OtherErr&>)
693   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
694     if (this->__has_val()) {
695       __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error());
696     } else {
697       this->__unex() = __un.error();
698     }
699     return *this;
700   }
701 
702   template <class _OtherErr>
703     requires(__can_assign_from_unexpected<_OtherErr>)
704   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
705     if (this->__has_val()) {
706       __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error()));
707     } else {
708       this->__unex() = std::move(__un.error());
709     }
710     return *this;
711   }
712 
713   template <class... _Args>
714     requires is_nothrow_constructible_v<_Tp, _Args...>
715   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept {
716     this->__destroy();
717     this->__construct(in_place, std::forward<_Args>(__args)...);
718     return this->__val();
719   }
720 
721   template <class _Up, class... _Args>
722     requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
723   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept {
724     this->__destroy();
725     this->__construct(in_place, __il, std::forward<_Args>(__args)...);
726     return this->__val();
727   }
728 
729 public:
730   // [expected.object.swap], swap
731   _LIBCPP_HIDE_FROM_ABI constexpr void
732   swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> &&
733                                  is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
734     requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> &&
735              is_move_constructible_v<_Err> &&
736              (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
737   {
738     auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
739       if constexpr (is_nothrow_move_constructible_v<_Err>) {
740         _Err __tmp(std::move(__with_err.__unex()));
741         __with_err.__destroy();
742         auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); });
743         __with_err.__construct(in_place, std::move(__with_val.__val()));
744         __trans.__complete();
745         __with_val.__destroy();
746         __with_val.__construct(unexpect, std::move(__tmp));
747       } else {
748         static_assert(is_nothrow_move_constructible_v<_Tp>,
749                       "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
750                       "that it can be reverted to the previous state in case an exception is thrown during swap.");
751         _Tp __tmp(std::move(__with_val.__val()));
752         __with_val.__destroy();
753         auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); });
754         __with_val.__construct(unexpect, std::move(__with_err.__unex()));
755         __trans.__complete();
756         __with_err.__destroy();
757         __with_err.__construct(in_place, std::move(__tmp));
758       }
759     };
760 
761     if (this->__has_val()) {
762       if (__rhs.__has_val()) {
763         using std::swap;
764         swap(this->__val(), __rhs.__val());
765       } else {
766         __swap_val_unex_impl(*this, __rhs);
767       }
768     } else {
769       if (__rhs.__has_val()) {
770         __swap_val_unex_impl(__rhs, *this);
771       } else {
772         using std::swap;
773         swap(this->__unex(), __rhs.__unex());
774       }
775     }
776   }
777 
778   _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))
779     requires requires { __x.swap(__y); }
780   {
781     __x.swap(__y);
782   }
783 
784   // [expected.object.obs], observers
785   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept {
786     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
787         this->__has_val(), "expected::operator-> requires the expected to contain a value");
788     return std::addressof(this->__val());
789   }
790 
791   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept {
792     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
793         this->__has_val(), "expected::operator-> requires the expected to contain a value");
794     return std::addressof(this->__val());
795   }
796 
797   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
798     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
799         this->__has_val(), "expected::operator* requires the expected to contain a value");
800     return this->__val();
801   }
802 
803   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
804     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
805         this->__has_val(), "expected::operator* requires the expected to contain a value");
806     return this->__val();
807   }
808 
809   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
810     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
811         this->__has_val(), "expected::operator* requires the expected to contain a value");
812     return std::move(this->__val());
813   }
814 
815   _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
816     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
817         this->__has_val(), "expected::operator* requires the expected to contain a value");
818     return std::move(this->__val());
819   }
820 
821   _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
822 
823   _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
824 
825   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
826     static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
827     if (!this->__has_val()) {
828       std::__throw_bad_expected_access<_Err>(std::as_const(error()));
829     }
830     return this->__val();
831   }
832 
833   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
834     static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
835     if (!this->__has_val()) {
836       std::__throw_bad_expected_access<_Err>(std::as_const(error()));
837     }
838     return this->__val();
839   }
840 
841   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& {
842     static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
843                   "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
844     if (!this->__has_val()) {
845       std::__throw_bad_expected_access<_Err>(std::move(error()));
846     }
847     return std::move(this->__val());
848   }
849 
850   _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
851     static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
852                   "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
853     if (!this->__has_val()) {
854       std::__throw_bad_expected_access<_Err>(std::move(error()));
855     }
856     return std::move(this->__val());
857   }
858 
859   _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {
860     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
861         !this->__has_val(), "expected::error requires the expected to contain an error");
862     return this->__unex();
863   }
864 
865   _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {
866     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
867         !this->__has_val(), "expected::error requires the expected to contain an error");
868     return this->__unex();
869   }
870 
871   _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {
872     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
873         !this->__has_val(), "expected::error requires the expected to contain an error");
874     return std::move(this->__unex());
875   }
876 
877   _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
878     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
879         !this->__has_val(), "expected::error requires the expected to contain an error");
880     return std::move(this->__unex());
881   }
882 
883   template <class _Up>
884   _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
885     static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible");
886     static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
887     return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v));
888   }
889 
890   template <class _Up>
891   _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
892     static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible");
893     static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
894     return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v));
895   }
896 
897   template <class _Up = _Err>
898   _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
899     static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
900     static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
901     if (has_value())
902       return std::forward<_Up>(__error);
903     return error();
904   }
905 
906   template <class _Up = _Err>
907   _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
908     static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
909     static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
910     if (has_value())
911       return std::forward<_Up>(__error);
912     return std::move(error());
913   }
914 
915   // [expected.void.monadic], monadic
916   template <class _Func>
917     requires is_constructible_v<_Err, _Err&>
918   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
919     using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;
920     static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");
921     static_assert(is_same_v<typename _Up::error_type, _Err>,
922                   "The result of f(**this) must have the same error_type as this expected");
923     if (has_value()) {
924       return std::invoke(std::forward<_Func>(__f), this->__val());
925     }
926     return _Up(unexpect, error());
927   }
928 
929   template <class _Func>
930     requires is_constructible_v<_Err, const _Err&>
931   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
932     using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>;
933     static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");
934     static_assert(is_same_v<typename _Up::error_type, _Err>,
935                   "The result of f(**this) must have the same error_type as this expected");
936     if (has_value()) {
937       return std::invoke(std::forward<_Func>(__f), this->__val());
938     }
939     return _Up(unexpect, error());
940   }
941 
942   template <class _Func>
943     requires is_constructible_v<_Err, _Err&&>
944   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
945     using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>;
946     static_assert(
947         __is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");
948     static_assert(is_same_v<typename _Up::error_type, _Err>,
949                   "The result of f(std::move(**this)) must have the same error_type as this expected");
950     if (has_value()) {
951       return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
952     }
953     return _Up(unexpect, std::move(error()));
954   }
955 
956   template <class _Func>
957     requires is_constructible_v<_Err, const _Err&&>
958   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
959     using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
960     static_assert(
961         __is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");
962     static_assert(is_same_v<typename _Up::error_type, _Err>,
963                   "The result of f(std::move(**this)) must have the same error_type as this expected");
964     if (has_value()) {
965       return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
966     }
967     return _Up(unexpect, std::move(error()));
968   }
969 
970   template <class _Func>
971     requires is_constructible_v<_Tp, _Tp&>
972   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
973     using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
974     static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
975     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
976                   "The result of f(error()) must have the same value_type as this expected");
977     if (has_value()) {
978       return _Gp(in_place, this->__val());
979     }
980     return std::invoke(std::forward<_Func>(__f), error());
981   }
982 
983   template <class _Func>
984     requires is_constructible_v<_Tp, const _Tp&>
985   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
986     using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
987     static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
988     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
989                   "The result of f(error()) must have the same value_type as this expected");
990     if (has_value()) {
991       return _Gp(in_place, this->__val());
992     }
993     return std::invoke(std::forward<_Func>(__f), error());
994   }
995 
996   template <class _Func>
997     requires is_constructible_v<_Tp, _Tp&&>
998   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
999     using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1000     static_assert(
1001         __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1002     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1003                   "The result of f(std::move(error())) must have the same value_type as this expected");
1004     if (has_value()) {
1005       return _Gp(in_place, std::move(this->__val()));
1006     }
1007     return std::invoke(std::forward<_Func>(__f), std::move(error()));
1008   }
1009 
1010   template <class _Func>
1011     requires is_constructible_v<_Tp, const _Tp&&>
1012   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {
1013     using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;
1014     static_assert(
1015         __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1016     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1017                   "The result of f(std::move(error())) must have the same value_type as this expected");
1018     if (has_value()) {
1019       return _Gp(in_place, std::move(this->__val()));
1020     }
1021     return std::invoke(std::forward<_Func>(__f), std::move(error()));
1022   }
1023 
1024   template <class _Func>
1025     requires is_constructible_v<_Err, _Err&>
1026   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1027     using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
1028     if (!has_value()) {
1029       return expected<_Up, _Err>(unexpect, error());
1030     }
1031     if constexpr (!is_void_v<_Up>) {
1032       return expected<_Up, _Err>(
1033           __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());
1034     } else {
1035       std::invoke(std::forward<_Func>(__f), this->__val());
1036       return expected<_Up, _Err>();
1037     }
1038   }
1039 
1040   template <class _Func>
1041     requires is_constructible_v<_Err, const _Err&>
1042   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1043     using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
1044     if (!has_value()) {
1045       return expected<_Up, _Err>(unexpect, error());
1046     }
1047     if constexpr (!is_void_v<_Up>) {
1048       return expected<_Up, _Err>(
1049           __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());
1050     } else {
1051       std::invoke(std::forward<_Func>(__f), this->__val());
1052       return expected<_Up, _Err>();
1053     }
1054   }
1055 
1056   template <class _Func>
1057     requires is_constructible_v<_Err, _Err&&>
1058   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1059     using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
1060     if (!has_value()) {
1061       return expected<_Up, _Err>(unexpect, std::move(error()));
1062     }
1063     if constexpr (!is_void_v<_Up>) {
1064       return expected<_Up, _Err>(
1065           __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));
1066     } else {
1067       std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1068       return expected<_Up, _Err>();
1069     }
1070   }
1071 
1072   template <class _Func>
1073     requires is_constructible_v<_Err, const _Err&&>
1074   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1075     using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>;
1076     if (!has_value()) {
1077       return expected<_Up, _Err>(unexpect, std::move(error()));
1078     }
1079     if constexpr (!is_void_v<_Up>) {
1080       return expected<_Up, _Err>(
1081           __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));
1082     } else {
1083       std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1084       return expected<_Up, _Err>();
1085     }
1086   }
1087 
1088   template <class _Func>
1089     requires is_constructible_v<_Tp, _Tp&>
1090   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {
1091     using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;
1092     static_assert(__valid_std_unexpected<_Gp>::value,
1093                   "The result of f(error()) must be a valid template argument for unexpected");
1094     if (has_value()) {
1095       return expected<_Tp, _Gp>(in_place, this->__val());
1096     }
1097     return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1098   }
1099 
1100   template <class _Func>
1101     requires is_constructible_v<_Tp, const _Tp&>
1102   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {
1103     using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;
1104     static_assert(__valid_std_unexpected<_Gp>::value,
1105                   "The result of f(error()) must be a valid template argument for unexpected");
1106     if (has_value()) {
1107       return expected<_Tp, _Gp>(in_place, this->__val());
1108     }
1109     return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1110   }
1111 
1112   template <class _Func>
1113     requires is_constructible_v<_Tp, _Tp&&>
1114   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {
1115     using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;
1116     static_assert(__valid_std_unexpected<_Gp>::value,
1117                   "The result of f(std::move(error())) must be a valid template argument for unexpected");
1118     if (has_value()) {
1119       return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1120     }
1121     return expected<_Tp, _Gp>(
1122         __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1123   }
1124 
1125   template <class _Func>
1126     requires is_constructible_v<_Tp, const _Tp&&>
1127   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {
1128     using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;
1129     static_assert(__valid_std_unexpected<_Gp>::value,
1130                   "The result of f(std::move(error())) must be a valid template argument for unexpected");
1131     if (has_value()) {
1132       return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1133     }
1134     return expected<_Tp, _Gp>(
1135         __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1136   }
1137 
1138   // [expected.object.eq], equality operators
1139   template <class _T2, class _E2>
1140     requires(!is_void_v<_T2>)
1141   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) {
1142     if (__x.__has_val() != __y.__has_val()) {
1143       return false;
1144     } else {
1145       if (__x.__has_val()) {
1146         return __x.__val() == __y.__val();
1147       } else {
1148         return __x.__unex() == __y.__unex();
1149       }
1150     }
1151   }
1152 
1153   template <class _T2>
1154   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const _T2& __v) {
1155     return __x.__has_val() && static_cast<bool>(__x.__val() == __v);
1156   }
1157 
1158   template <class _E2>
1159   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) {
1160     return !__x.__has_val() && static_cast<bool>(__x.__unex() == __e.error());
1161   }
1162 };
1163 
1164 template <class _Err>
1165 class __expected_void_base {
1166   struct __empty_t {};
1167   // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
1168   // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
1169   // it's not clear that it's implementable, given that the function is allowed to clobber
1170   // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
1171   union __union_t {
1172     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
1173     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
1174       requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1175     = default;
1176     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
1177     _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
1178       requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1179     = default;
1180     _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
1181     _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&)      = delete;
1182 
1183     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}
1184 
1185     template <class... _Args>
1186     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
1187         : __unex_(std::forward<_Args>(__args)...) {}
1188 
1189     template <class _Func, class... _Args>
1190     _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
1191         __expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
1192         : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
1193 
1194     _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1195       requires(is_trivially_destructible_v<_Err>)
1196     = default;
1197 
1198     // __repr's destructor handles this
1199     _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1200       requires(!is_trivially_destructible_v<_Err>)
1201     {}
1202 
1203     _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;
1204     _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
1205   };
1206 
1207   static constexpr bool __put_flag_in_tail                    = __fits_in_tail_padding<__union_t, bool>;
1208   static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
1209 
1210   struct __repr {
1211     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
1212 
1213     template <class... _Args>
1214     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}
1215 
1216     template <class... _Args>
1217     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
1218         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1219 
1220     template <class... _Args>
1221     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
1222                                                     _Args&&... __args)
1223         : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1224 
1225     template <class _OtherUnion>
1226     _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
1227       requires(__allow_reusing_expected_tail_padding)
1228         : __union_(__conditional_no_unique_address_invoke_tag{},
1229                    [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
1230           __has_val_(__has_val) {}
1231 
1232     _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
1233     _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
1234       requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1235     = default;
1236     _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
1237     _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
1238       requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1239     = default;
1240 
1241     _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
1242     _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&)      = delete;
1243 
1244     _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1245       requires(is_trivially_destructible_v<_Err>)
1246     = default;
1247 
1248     _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1249       requires(!is_trivially_destructible_v<_Err>)
1250     {
1251       __destroy_union_member();
1252     }
1253 
1254     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1255       requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>)
1256     {
1257       std::destroy_at(&__union_.__v);
1258     }
1259 
1260     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1261       requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)
1262     {
1263       __destroy_union_member();
1264       std::destroy_at(&__union_.__v);
1265     }
1266 
1267     _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)
1268       requires(__allow_reusing_expected_tail_padding)
1269     {
1270       std::construct_at(&__union_.__v, in_place);
1271       __has_val_ = true;
1272     }
1273 
1274     template <class... _Args>
1275     _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
1276       requires(__allow_reusing_expected_tail_padding)
1277     {
1278       std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
1279       __has_val_ = false;
1280     }
1281 
1282   private:
1283     template <class>
1284     friend class __expected_void_base;
1285 
1286     _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
1287       requires(!is_trivially_destructible_v<_Err>)
1288     {
1289       if (!__has_val_)
1290         std::destroy_at(std::addressof(__union_.__v.__unex_));
1291     }
1292 
1293     template <class _OtherUnion>
1294     _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
1295       requires(__allow_reusing_expected_tail_padding)
1296     {
1297       if (__has_val)
1298         return __union_t(in_place);
1299       else
1300         return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1301     }
1302 
1303     _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
1304     _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
1305   };
1306 
1307   template <class _OtherUnion>
1308   _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
1309     requires(__put_flag_in_tail)
1310   {
1311     if (__has_val)
1312       return __repr(in_place);
1313     else
1314       return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1315   }
1316 
1317 protected:
1318   template <class... _Args>
1319   _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)
1320       : __repr_(in_place, std::forward<_Args>(__args)...) {}
1321 
1322   template <class _OtherUnion>
1323   _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(bool __has_val, _OtherUnion&& __other)
1324     requires(__put_flag_in_tail)
1325       : __repr_(__conditional_no_unique_address_invoke_tag{},
1326                 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
1327 
1328   _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
1329     if constexpr (__put_flag_in_tail)
1330       std::destroy_at(&__repr_.__v);
1331     else
1332       __repr_.__v.__destroy_union();
1333   }
1334 
1335   template <class _Tag, class... _Args>
1336   _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
1337     if constexpr (__put_flag_in_tail)
1338       std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
1339     else
1340       __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
1341   }
1342 
1343   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
1344   _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
1345   _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
1346   _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
1347   _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
1348 
1349 private:
1350   _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
1351 };
1352 
1353 template <class _Tp, class _Err>
1354   requires is_void_v<_Tp>
1355 class expected<_Tp, _Err> : private __expected_void_base<_Err> {
1356   static_assert(__valid_std_unexpected<_Err>::value,
1357                 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "
1358                 "valid argument for unexpected<E> is ill-formed");
1359 
1360   template <class, class>
1361   friend class expected;
1362 
1363   template <class _Up, class _OtherErr, class _OtherErrQual>
1364   using __can_convert =
1365       _And< is_void<_Up>,
1366             is_constructible<_Err, _OtherErrQual>,
1367             _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
1368             _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
1369             _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
1370             _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>;
1371 
1372   using __base = __expected_void_base<_Err>;
1373 
1374 public:
1375   using value_type      = _Tp;
1376   using error_type      = _Err;
1377   using unexpected_type = unexpected<_Err>;
1378 
1379   template <class _Up>
1380   using rebind = expected<_Up, error_type>;
1381 
1382   // [expected.void.ctor], constructors
1383   _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}
1384 
1385   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
1386 
1387   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
1388     requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1389   = default;
1390 
1391   _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(
1392       is_nothrow_copy_constructible_v<_Err>) // strengthened
1393     requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)
1394       : __base(__rhs.__has_val(), __rhs.__union()) {}
1395 
1396   _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
1397     requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1398   = default;
1399 
1400   _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __rhs) noexcept(is_nothrow_move_constructible_v<_Err>)
1401     requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>)
1402       : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1403 
1404   template <class _Up, class _OtherErr>
1405     requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value
1406   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>)
1407       expected(const expected<_Up, _OtherErr>& __rhs) noexcept(
1408           is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1409       : __base(__rhs.__has_val(), __rhs.__union()) {}
1410 
1411   template <class _Up, class _OtherErr>
1412     requires __can_convert<_Up, _OtherErr, _OtherErr>::value
1413   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1414       expected(expected<_Up, _OtherErr>&& __rhs) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1415       : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1416 
1417   template <class _OtherErr>
1418     requires is_constructible_v<_Err, const _OtherErr&>
1419   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
1420       const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1421       : __base(unexpect, __unex.error()) {}
1422 
1423   template <class _OtherErr>
1424     requires is_constructible_v<_Err, _OtherErr>
1425   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1426       expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1427       : __base(unexpect, std::move(__unex.error())) {}
1428 
1429   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}
1430 
1431   template <class... _Args>
1432     requires is_constructible_v<_Err, _Args...>
1433   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
1434       is_nothrow_constructible_v<_Err, _Args...>) // strengthened
1435       : __base(unexpect, std::forward<_Args>(__args)...) {}
1436 
1437   template <class _Up, class... _Args>
1438     requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
1439   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
1440       is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
1441       : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
1442 
1443 private:
1444   template <class _Func, class... _Args>
1445   _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
1446       __expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
1447       : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
1448 
1449 public:
1450   // [expected.void.dtor], destructor
1451 
1452   _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
1453 
1454 private:
1455   template <class... _Args>
1456   _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(unexpect_t, _Args&&... __args) {
1457     _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");
1458 
1459     this->__destroy();
1460     auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });
1461     this->__construct(unexpect, std::forward<_Args>(__args)...);
1462     __trans.__complete();
1463   }
1464 
1465   _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(in_place_t) {
1466     _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");
1467 
1468     this->__destroy();
1469     this->__construct(in_place);
1470   }
1471 
1472 public:
1473   // [expected.void.assign], assignment
1474   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
1475 
1476   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
1477       is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened
1478     requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)
1479   {
1480     if (this->__has_val()) {
1481       if (!__rhs.__has_val()) {
1482         __reinit_expected(unexpect, __rhs.__unex());
1483       }
1484     } else {
1485       if (__rhs.__has_val()) {
1486         __reinit_expected(in_place);
1487       } else {
1488         this->__unex() = __rhs.__unex();
1489       }
1490     }
1491     return *this;
1492   }
1493 
1494   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&&) = delete;
1495 
1496   _LIBCPP_HIDE_FROM_ABI constexpr expected&
1497   operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
1498     requires(is_move_assignable_v<_Err> && is_move_constructible_v<_Err>)
1499   {
1500     if (this->__has_val()) {
1501       if (!__rhs.__has_val()) {
1502         __reinit_expected(unexpect, std::move(__rhs.__unex()));
1503       }
1504     } else {
1505       if (__rhs.__has_val()) {
1506         __reinit_expected(in_place);
1507       } else {
1508         this->__unex() = std::move(__rhs.__unex());
1509       }
1510     }
1511     return *this;
1512   }
1513 
1514   template <class _OtherErr>
1515     requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>)
1516   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
1517     if (this->__has_val()) {
1518       __reinit_expected(unexpect, __un.error());
1519     } else {
1520       this->__unex() = __un.error();
1521     }
1522     return *this;
1523   }
1524 
1525   template <class _OtherErr>
1526     requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>)
1527   _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
1528     if (this->__has_val()) {
1529       __reinit_expected(unexpect, std::move(__un.error()));
1530     } else {
1531       this->__unex() = std::move(__un.error());
1532     }
1533     return *this;
1534   }
1535 
1536   _LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept {
1537     if (!this->__has_val()) {
1538       __reinit_expected(in_place);
1539     }
1540   }
1541 
1542   // [expected.void.swap], swap
1543   _LIBCPP_HIDE_FROM_ABI constexpr void
1544   swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
1545     requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>)
1546   {
1547     auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
1548       // May throw, but will re-engage `__with_val` in that case.
1549       __with_val.__reinit_expected(unexpect, std::move(__with_err.__unex()));
1550       // Will not throw.
1551       __with_err.__reinit_expected(in_place);
1552     };
1553 
1554     if (this->__has_val()) {
1555       if (!__rhs.__has_val()) {
1556         __swap_val_unex_impl(*this, __rhs);
1557       }
1558     } else {
1559       if (__rhs.__has_val()) {
1560         __swap_val_unex_impl(__rhs, *this);
1561       } else {
1562         using std::swap;
1563         swap(this->__unex(), __rhs.__unex());
1564       }
1565     }
1566   }
1567 
1568   _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))
1569     requires requires { __x.swap(__y); }
1570   {
1571     __x.swap(__y);
1572   }
1573 
1574   // [expected.void.obs], observers
1575   _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
1576 
1577   _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
1578 
1579   _LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {
1580     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1581         this->__has_val(), "expected::operator* requires the expected to contain a value");
1582   }
1583 
1584   _LIBCPP_HIDE_FROM_ABI constexpr void value() const& {
1585     static_assert(is_copy_constructible_v<_Err>);
1586     if (!this->__has_val()) {
1587       std::__throw_bad_expected_access<_Err>(this->__unex());
1588     }
1589   }
1590 
1591   _LIBCPP_HIDE_FROM_ABI constexpr void value() && {
1592     static_assert(is_copy_constructible_v<_Err> && is_move_constructible_v<_Err>);
1593     if (!this->__has_val()) {
1594       std::__throw_bad_expected_access<_Err>(std::move(this->__unex()));
1595     }
1596   }
1597 
1598   _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {
1599     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1600         !this->__has_val(), "expected::error requires the expected to contain an error");
1601     return this->__unex();
1602   }
1603 
1604   _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {
1605     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1606         !this->__has_val(), "expected::error requires the expected to contain an error");
1607     return this->__unex();
1608   }
1609 
1610   _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {
1611     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1612         !this->__has_val(), "expected::error requires the expected to contain an error");
1613     return std::move(this->__unex());
1614   }
1615 
1616   _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
1617     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1618         !this->__has_val(), "expected::error requires the expected to contain an error");
1619     return std::move(this->__unex());
1620   }
1621 
1622   template <class _Up = _Err>
1623   _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
1624     static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
1625     static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
1626     if (has_value()) {
1627       return std::forward<_Up>(__error);
1628     }
1629     return error();
1630   }
1631 
1632   template <class _Up = _Err>
1633   _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
1634     static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
1635     static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
1636     if (has_value()) {
1637       return std::forward<_Up>(__error);
1638     }
1639     return std::move(error());
1640   }
1641 
1642   // [expected.void.monadic], monadic
1643   template <class _Func>
1644     requires is_constructible_v<_Err, _Err&>
1645   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1646     using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1647     static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1648     static_assert(
1649         is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1650     if (has_value()) {
1651       return std::invoke(std::forward<_Func>(__f));
1652     }
1653     return _Up(unexpect, error());
1654   }
1655 
1656   template <class _Func>
1657     requires is_constructible_v<_Err, const _Err&>
1658   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1659     using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1660     static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1661     static_assert(
1662         is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1663     if (has_value()) {
1664       return std::invoke(std::forward<_Func>(__f));
1665     }
1666     return _Up(unexpect, error());
1667   }
1668 
1669   template <class _Func>
1670     requires is_constructible_v<_Err, _Err&&>
1671   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1672     using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1673     static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1674     static_assert(
1675         is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1676     if (has_value()) {
1677       return std::invoke(std::forward<_Func>(__f));
1678     }
1679     return _Up(unexpect, std::move(error()));
1680   }
1681 
1682   template <class _Func>
1683     requires is_constructible_v<_Err, const _Err&&>
1684   _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1685     using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1686     static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1687     static_assert(
1688         is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1689     if (has_value()) {
1690       return std::invoke(std::forward<_Func>(__f));
1691     }
1692     return _Up(unexpect, std::move(error()));
1693   }
1694 
1695   template <class _Func>
1696   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
1697     using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
1698     static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1699     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1700                   "The result of f(error()) must have the same value_type as this expected");
1701     if (has_value()) {
1702       return _Gp();
1703     }
1704     return std::invoke(std::forward<_Func>(__f), error());
1705   }
1706 
1707   template <class _Func>
1708   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
1709     using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
1710     static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1711     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1712                   "The result of f(error()) must have the same value_type as this expected");
1713     if (has_value()) {
1714       return _Gp();
1715     }
1716     return std::invoke(std::forward<_Func>(__f), error());
1717   }
1718 
1719   template <class _Func>
1720   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
1721     using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1722     static_assert(
1723         __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1724     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1725                   "The result of f(std::move(error())) must have the same value_type as this expected");
1726     if (has_value()) {
1727       return _Gp();
1728     }
1729     return std::invoke(std::forward<_Func>(__f), std::move(error()));
1730   }
1731 
1732   template <class _Func>
1733   _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {
1734     using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;
1735     static_assert(
1736         __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1737     static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1738                   "The result of f(std::move(error())) must have the same value_type as this expected");
1739     if (has_value()) {
1740       return _Gp();
1741     }
1742     return std::invoke(std::forward<_Func>(__f), std::move(error()));
1743   }
1744 
1745   template <class _Func>
1746     requires is_constructible_v<_Err, _Err&>
1747   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1748     using _Up = remove_cv_t<invoke_result_t<_Func>>;
1749     if (!has_value()) {
1750       return expected<_Up, _Err>(unexpect, error());
1751     }
1752     if constexpr (!is_void_v<_Up>) {
1753       return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1754     } else {
1755       std::invoke(std::forward<_Func>(__f));
1756       return expected<_Up, _Err>();
1757     }
1758   }
1759 
1760   template <class _Func>
1761     requires is_constructible_v<_Err, const _Err&>
1762   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1763     using _Up = remove_cv_t<invoke_result_t<_Func>>;
1764     if (!has_value()) {
1765       return expected<_Up, _Err>(unexpect, error());
1766     }
1767     if constexpr (!is_void_v<_Up>) {
1768       return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1769     } else {
1770       std::invoke(std::forward<_Func>(__f));
1771       return expected<_Up, _Err>();
1772     }
1773   }
1774 
1775   template <class _Func>
1776     requires is_constructible_v<_Err, _Err&&>
1777   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1778     using _Up = remove_cv_t<invoke_result_t<_Func>>;
1779     if (!has_value()) {
1780       return expected<_Up, _Err>(unexpect, std::move(error()));
1781     }
1782     if constexpr (!is_void_v<_Up>) {
1783       return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1784     } else {
1785       std::invoke(std::forward<_Func>(__f));
1786       return expected<_Up, _Err>();
1787     }
1788   }
1789 
1790   template <class _Func>
1791     requires is_constructible_v<_Err, const _Err&&>
1792   _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1793     using _Up = remove_cv_t<invoke_result_t<_Func>>;
1794     if (!has_value()) {
1795       return expected<_Up, _Err>(unexpect, std::move(error()));
1796     }
1797     if constexpr (!is_void_v<_Up>) {
1798       return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1799     } else {
1800       std::invoke(std::forward<_Func>(__f));
1801       return expected<_Up, _Err>();
1802     }
1803   }
1804 
1805   template <class _Func>
1806   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {
1807     using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;
1808     static_assert(__valid_std_unexpected<_Gp>::value,
1809                   "The result of f(error()) must be a valid template argument for unexpected");
1810     if (has_value()) {
1811       return expected<_Tp, _Gp>();
1812     }
1813     return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1814   }
1815 
1816   template <class _Func>
1817   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {
1818     using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;
1819     static_assert(__valid_std_unexpected<_Gp>::value,
1820                   "The result of f(error()) must be a valid template argument for unexpected");
1821     if (has_value()) {
1822       return expected<_Tp, _Gp>();
1823     }
1824     return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1825   }
1826 
1827   template <class _Func>
1828   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {
1829     using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;
1830     static_assert(__valid_std_unexpected<_Gp>::value,
1831                   "The result of f(std::move(error())) must be a valid template argument for unexpected");
1832     if (has_value()) {
1833       return expected<_Tp, _Gp>();
1834     }
1835     return expected<_Tp, _Gp>(
1836         __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1837   }
1838 
1839   template <class _Func>
1840   _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {
1841     using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;
1842     static_assert(__valid_std_unexpected<_Gp>::value,
1843                   "The result of f(std::move(error())) must be a valid template argument for unexpected");
1844     if (has_value()) {
1845       return expected<_Tp, _Gp>();
1846     }
1847     return expected<_Tp, _Gp>(
1848         __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1849   }
1850 
1851   // [expected.void.eq], equality operators
1852   template <class _T2, class _E2>
1853     requires is_void_v<_T2>
1854   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) {
1855     if (__x.__has_val() != __y.__has_val()) {
1856       return false;
1857     } else {
1858       return __x.__has_val() || static_cast<bool>(__x.__unex() == __y.__unex());
1859     }
1860   }
1861 
1862   template <class _E2>
1863   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) {
1864     return !__x.__has_val() && static_cast<bool>(__x.__unex() == __y.error());
1865   }
1866 };
1867 
1868 _LIBCPP_END_NAMESPACE_STD
1869 
1870 #endif // _LIBCPP_STD_VER >= 23
1871 
1872 _LIBCPP_POP_MACROS
1873 
1874 #endif // _LIBCPP___CXX03___EXPECTED_EXPECTED_H
1875