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