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