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
449template <class _Tp, class _Err>
450class expected : private __expected_base<_Tp, _Err> {
451 static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> &&
452 !is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value &&
453 __valid_std_unexpected<_Err>::value,
454 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
455 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
456 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
457 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
458 "template argument for unexpected is ill-formed.");
459
460 template <class _Up, class _OtherErr>
461 friend class expected;
462
463 using __base _LIBCPP_NODEBUG = __expected_base<_Tp, _Err>;
464
465public:
466 using value_type = _Tp;
467 using error_type = _Err;
468 using unexpected_type = unexpected<_Err>;
469
470 using __trivially_relocatable _LIBCPP_NODEBUG =
471 __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,
472 expected,
473 void>;
474
475 template <class _Up>
476 using rebind = expected<_Up, error_type>;
477
478 // [expected.object.ctor], constructors
479 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened
480 requires is_default_constructible_v<_Tp>
481 : __base(in_place) {}
482
483 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
484
485 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
486 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&
487 is_trivially_copy_constructible_v<_Err>)
488 = default;
489
490 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
491 is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened
492 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
493 !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
494 : __base(__other.__has_val(), __other.__union()) {}
495
496 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
497 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&
498 is_trivially_move_constructible_v<_Err>)
499 = default;
500
501 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(
502 is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)
503 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
504 !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))
505 : __base(__other.__has_val(), std::move(__other.__union())) {}
506
507private:
508 template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>
509 using __can_convert _LIBCPP_NODEBUG = _And<
510 is_constructible<_Tp, _UfQual>,
511 is_constructible<_Err, _OtherErrQual>,
512 _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
513 _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>, // use the copy constructor instead, see #92676
514 _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 = remove_cv_t<_Tp>>
556 requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&
557 !is_same_v<remove_cvref_t<_Up>, unexpect_t> && is_constructible_v<_Tp, _Up> &&
558 !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
559 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))
560 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)
561 expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened
562 : __base(in_place, std::forward<_Up>(__u)) {}
563
564 template <class _OtherErr>
565 requires is_constructible_v<_Err, const _OtherErr&>
566 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
567 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
568 : __base(unexpect, __unex.error()) {}
569
570 template <class _OtherErr>
571 requires is_constructible_v<_Err, _OtherErr>
572 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
573 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
574 : __base(unexpect, std::move(__unex.error())) {}
575
576 template <class... _Args>
577 requires is_constructible_v<_Tp, _Args...>
578 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(
579 is_nothrow_constructible_v<_Tp, _Args...>) // strengthened
580 : __base(in_place, std::forward<_Args>(__args)...) {}
581
582 template <class _Up, class... _Args>
583 requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >
584 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
585 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened
586 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
587
588 template <class... _Args>
589 requires is_constructible_v<_Err, _Args...>
590 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
591 is_nothrow_constructible_v<_Err, _Args...>) // strengthened
592 : __base(unexpect, std::forward<_Args>(__args)...) {}
593
594 template <class _Up, class... _Args>
595 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
596 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
597 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
598 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
599
600 // [expected.object.dtor], destructor
601
602 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
603
604private:
605 template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>
606 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {
607 if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {
608 this->__destroy();
609 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
610 } else if constexpr (is_nothrow_move_constructible_v<_T1>) {
611 _T1 __tmp(std::forward<_Args>(__args)...);
612 this->__destroy();
613 this->__construct(_Tag{}, std::move(__tmp));
614 } else {
615 static_assert(
616 is_nothrow_move_constructible_v<_T2>,
617 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
618 "be reverted to the previous state in case an exception is thrown during the assignment.");
619 _T2 __tmp(std::move(__oldval));
620 this->__destroy();
621 auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });
622 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
623 __trans.__complete();
624 }
625 }
626
627public:
628 // [expected.object.assign], assignment
629 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
630
631 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
632 is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&
633 is_nothrow_copy_constructible_v<_Err>) // strengthened
634 requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&
635 is_copy_constructible_v<_Err> &&
636 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
637 {
638 if (this->__has_val() && __rhs.__has_val()) {
639 this->__val() = __rhs.__val();
640 } else if (this->__has_val()) {
641 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());
642 } else if (__rhs.__has_val()) {
643 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());
644 } else {
645 this->__unex() = __rhs.__unex();
646 }
647 return *this;
648 }
649
650 _LIBCPP_HIDE_FROM_ABI constexpr expected&
651 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&
652 is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
653 requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&
654 is_move_assignable_v<_Err> &&
655 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
656 {
657 if (this->__has_val() && __rhs.__has_val()) {
658 this->__val() = std::move(__rhs.__val());
659 } else if (this->__has_val()) {
660 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));
661 } else if (__rhs.__has_val()) {
662 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));
663 } else {
664 this->__unex() = std::move(__rhs.__unex());
665 }
666 return *this;
667 }
668
669 template <class _Up = remove_cv_t<_Tp>>
670 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)
671 requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
672 is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&
673 (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||
674 is_nothrow_move_constructible_v<_Err>))
675 {
676 if (this->__has_val()) {
677 this->__val() = std::forward<_Up>(__v);
678 } else {
679 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));
680 }
681 return *this;
682 }
683
684private:
685 template <class _OtherErrQual>
686 static constexpr bool __can_assign_from_unexpected =
687 _And<is_constructible<_Err, _OtherErrQual>,
688 is_assignable<_Err&, _OtherErrQual>,
689 _Or<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(rhs&: __y)))
782 requires requires { __x.swap(rhs&: __y); }
783 {
784 __x.swap(rhs&: __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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
827
828 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 = remove_cv_t<_Tp>>
887 [[nodiscard]] _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 = remove_cv_t<_Tp>>
894 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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(value()) must be a specialization of std::expected");
924 static_assert(is_same_v<typename _Up::error_type, _Err>,
925 "The result of f(value()) 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 [[nodiscard]] _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(value()) must be a specialization of std::expected");
937 static_assert(is_same_v<typename _Up::error_type, _Err>,
938 "The result of f(value()) 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 [[nodiscard]] _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(value())) 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(value())) 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 [[nodiscard]] _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(value())) 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(value())) 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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 [[nodiscard]] _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 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y)
1144 requires(!is_void_v<_T2>)
1145# if _LIBCPP_STD_VER >= 26
1146 && requires {
1147 { *__x == *__y } -> __core_convertible_to<bool>;
1148 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1149 }
1150# endif
1151 {
1152 if (__x.__has_val() != __y.__has_val()) {
1153 return false;
1154 } else {
1155 if (__x.__has_val()) {
1156 return __x.__val() == __y.__val();
1157 } else {
1158 return __x.__unex() == __y.__unex();
1159 }
1160 }
1161 }
1162
1163 // The unusual signature avoids constraint recursion via ADL through
1164 // std::expected, see https://llvm.org/PR160431. Note that this only
1165 // triggers with compilers that implement https://wg21.link/CWG2369.
1166 template <class _T2, same_as<_Tp> _Tp2>
1167 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected<_Tp2, _Err>& __x, const _T2& __v)
1168# if _LIBCPP_STD_VER >= 26
1169 requires(!__is_std_expected<_T2>::value) && requires {
1170 { *__x == __v } -> __core_convertible_to<bool>;
1171 }
1172# endif
1173 {
1174 return __x.__has_val() && static_cast<bool>(__x.__val() == __v);
1175 }
1176
1177 template <class _E2>
1178 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e)
1179# if _LIBCPP_STD_VER >= 26
1180 requires requires {
1181 { __x.error() == __e.error() } -> __core_convertible_to<bool>;
1182 }
1183# endif
1184 {
1185 return !__x.__has_val() && static_cast<bool>(__x.__unex() == __e.error());
1186 }
1187};
1188
1189template <class _Err>
1190class __expected_void_base {
1191 struct __empty_t {};
1192 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
1193 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
1194 // it's not clear that it's implementable, given that the function is allowed to clobber
1195 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
1196 union __union_t {
1197 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
1198 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
1199 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1200 = default;
1201 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
1202 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
1203 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1204 = default;
1205 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
1206 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;
1207
1208 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}
1209
1210 template <class... _Args>
1211 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
1212 : __unex_(std::forward<_Args>(__args)...) {}
1213
1214 template <class _Func, class... _Args>
1215 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
1216 __expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
1217 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
1218
1219 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1220 requires(is_trivially_destructible_v<_Err>)
1221 = default;
1222
1223 // __repr's destructor handles this
1224 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1225 requires(!is_trivially_destructible_v<_Err>)
1226 {}
1227
1228 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;
1229 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
1230 };
1231
1232 static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;
1233 static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
1234
1235 struct __repr {
1236 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
1237
1238 template <class... _Args>
1239 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}
1240
1241 template <class... _Args>
1242 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
1243 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1244
1245 template <class... _Args>
1246 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
1247 _Args&&... __args)
1248 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1249
1250 template <class _OtherUnion>
1251 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
1252 requires(__allow_reusing_expected_tail_padding)
1253 : __union_(__conditional_no_unique_address_invoke_tag{},
1254 [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
1255 __has_val_(__has_val) {}
1256
1257 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
1258 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
1259 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1260 = default;
1261 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
1262 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
1263 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1264 = default;
1265
1266 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
1267 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
1268
1269 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1270 requires(is_trivially_destructible_v<_Err>)
1271 = default;
1272
1273 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1274 requires(!is_trivially_destructible_v<_Err>)
1275 {
1276 __destroy_union_member();
1277 }
1278
1279 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1280 requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>)
1281 {
1282 std::destroy_at(&__union_.__v);
1283 }
1284
1285 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1286 requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)
1287 {
1288 __destroy_union_member();
1289 std::destroy_at(&__union_.__v);
1290 }
1291
1292 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)
1293 requires(__allow_reusing_expected_tail_padding)
1294 {
1295 std::construct_at(&__union_.__v, in_place);
1296 __has_val_ = true;
1297 }
1298
1299 template <class... _Args>
1300 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
1301 requires(__allow_reusing_expected_tail_padding)
1302 {
1303 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
1304 __has_val_ = false;
1305 }
1306
1307 private:
1308 template <class>
1309 friend class __expected_void_base;
1310
1311 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
1312 requires(!is_trivially_destructible_v<_Err>)
1313 {
1314 if (!__has_val_)
1315 std::destroy_at(std::addressof(__union_.__v.__unex_));
1316 }
1317
1318 template <class _OtherUnion>
1319 _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
1320 requires(__allow_reusing_expected_tail_padding)
1321 {
1322 if (__has_val)
1323 return __union_t(in_place);
1324 else
1325 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1326 }
1327
1328 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
1329 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
1330 };
1331
1332 template <class _OtherUnion>
1333 _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
1334 requires(__put_flag_in_tail)
1335 {
1336 if (__has_val)
1337 return __repr(in_place);
1338 else
1339 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1340 }
1341
1342protected:
1343 template <class... _Args>
1344 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)
1345 : __repr_(in_place, std::forward<_Args>(__args)...) {}
1346
1347 template <class _OtherUnion>
1348 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(bool __has_val, _OtherUnion&& __other)
1349 requires(__put_flag_in_tail)
1350 : __repr_(__conditional_no_unique_address_invoke_tag{},
1351 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
1352
1353 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
1354 if constexpr (__put_flag_in_tail)
1355 std::destroy_at(&__repr_.__v);
1356 else
1357 __repr_.__v.__destroy_union();
1358 }
1359
1360 template <class _Tag, class... _Args>
1361 _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
1362 if constexpr (__put_flag_in_tail)
1363 std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
1364 else
1365 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
1366 }
1367
1368 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
1369 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
1370 _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
1371 _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
1372 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
1373
1374private:
1375 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
1376};
1377
1378template <class _Tp, class _Err>
1379 requires is_void_v<_Tp>
1380class expected<_Tp, _Err> : private __expected_void_base<_Err> {
1381 static_assert(__valid_std_unexpected<_Err>::value,
1382 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "
1383 "valid argument for unexpected<E> is ill-formed");
1384
1385 template <class, class>
1386 friend class expected;
1387
1388 template <class _Up, class _OtherErr, class _OtherErrQual>
1389 using __can_convert _LIBCPP_NODEBUG =
1390 _And< is_void<_Up>,
1391 is_constructible<_Err, _OtherErrQual>,
1392 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
1393 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
1394 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
1395 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>;
1396
1397 using __base _LIBCPP_NODEBUG = __expected_void_base<_Err>;
1398
1399public:
1400 using value_type = _Tp;
1401 using error_type = _Err;
1402 using unexpected_type = unexpected<_Err>;
1403
1404 template <class _Up>
1405 using rebind = expected<_Up, error_type>;
1406
1407 // [expected.void.ctor], constructors
1408 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}
1409
1410 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
1411
1412 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
1413 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1414 = default;
1415
1416 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(
1417 is_nothrow_copy_constructible_v<_Err>) // strengthened
1418 requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)
1419 : __base(__rhs.__has_val(), __rhs.__union()) {}
1420
1421 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
1422 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1423 = default;
1424
1425 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __rhs) noexcept(is_nothrow_move_constructible_v<_Err>)
1426 requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>)
1427 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1428
1429 template <class _Up, class _OtherErr>
1430 requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value
1431 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>)
1432 expected(const expected<_Up, _OtherErr>& __rhs) noexcept(
1433 is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1434 : __base(__rhs.__has_val(), __rhs.__union()) {}
1435
1436 template <class _Up, class _OtherErr>
1437 requires __can_convert<_Up, _OtherErr, _OtherErr>::value
1438 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1439 expected(expected<_Up, _OtherErr>&& __rhs) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1440 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1441
1442 template <class _OtherErr>
1443 requires is_constructible_v<_Err, const _OtherErr&>
1444 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
1445 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1446 : __base(unexpect, __unex.error()) {}
1447
1448 template <class _OtherErr>
1449 requires is_constructible_v<_Err, _OtherErr>
1450 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1451 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1452 : __base(unexpect, std::move(__unex.error())) {}
1453
1454 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}
1455
1456 template <class... _Args>
1457 requires is_constructible_v<_Err, _Args...>
1458 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
1459 is_nothrow_constructible_v<_Err, _Args...>) // strengthened
1460 : __base(unexpect, std::forward<_Args>(__args)...) {}
1461
1462 template <class _Up, class... _Args>
1463 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
1464 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
1465 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
1466 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
1467
1468private:
1469 template <class _Func, class... _Args>
1470 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
1471 __expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
1472 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
1473
1474public:
1475 // [expected.void.dtor], destructor
1476
1477 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
1478
1479private:
1480 template <class... _Args>
1481 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(unexpect_t, _Args&&... __args) {
1482 _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");
1483
1484 this->__destroy();
1485 auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });
1486 this->__construct(unexpect, std::forward<_Args>(__args)...);
1487 __trans.__complete();
1488 }
1489
1490 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(in_place_t) {
1491 _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");
1492
1493 this->__destroy();
1494 this->__construct(in_place);
1495 }
1496
1497public:
1498 // [expected.void.assign], assignment
1499 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
1500
1501 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
1502 is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened
1503 requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)
1504 {
1505 if (this->__has_val()) {
1506 if (!__rhs.__has_val()) {
1507 __reinit_expected(unexpect, __rhs.__unex());
1508 }
1509 } else {
1510 if (__rhs.__has_val()) {
1511 __reinit_expected(in_place);
1512 } else {
1513 this->__unex() = __rhs.__unex();
1514 }
1515 }
1516 return *this;
1517 }
1518
1519 _LIBCPP_HIDE_FROM_ABI constexpr expected&
1520 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
1521 requires(is_move_assignable_v<_Err> && is_move_constructible_v<_Err>)
1522 {
1523 if (this->__has_val()) {
1524 if (!__rhs.__has_val()) {
1525 __reinit_expected(unexpect, std::move(__rhs.__unex()));
1526 }
1527 } else {
1528 if (__rhs.__has_val()) {
1529 __reinit_expected(in_place);
1530 } else {
1531 this->__unex() = std::move(__rhs.__unex());
1532 }
1533 }
1534 return *this;
1535 }
1536
1537 template <class _OtherErr>
1538 requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>)
1539 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
1540 if (this->__has_val()) {
1541 __reinit_expected(unexpect, __un.error());
1542 } else {
1543 this->__unex() = __un.error();
1544 }
1545 return *this;
1546 }
1547
1548 template <class _OtherErr>
1549 requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>)
1550 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
1551 if (this->__has_val()) {
1552 __reinit_expected(unexpect, std::move(__un.error()));
1553 } else {
1554 this->__unex() = std::move(__un.error());
1555 }
1556 return *this;
1557 }
1558
1559 _LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept {
1560 if (!this->__has_val()) {
1561 __reinit_expected(in_place);
1562 }
1563 }
1564
1565 // [expected.void.swap], swap
1566 _LIBCPP_HIDE_FROM_ABI constexpr void
1567 swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
1568 requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>)
1569 {
1570 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
1571 // May throw, but will re-engage `__with_val` in that case.
1572 __with_val.__reinit_expected(unexpect, std::move(__with_err.__unex()));
1573 // Will not throw.
1574 __with_err.__reinit_expected(in_place);
1575 };
1576
1577 if (this->__has_val()) {
1578 if (!__rhs.__has_val()) {
1579 __swap_val_unex_impl(*this, __rhs);
1580 }
1581 } else {
1582 if (__rhs.__has_val()) {
1583 __swap_val_unex_impl(__rhs, *this);
1584 } else {
1585 using std::swap;
1586 swap(this->__unex(), __rhs.__unex());
1587 }
1588 }
1589 }
1590
1591 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(rhs&: __y)))
1592 requires requires { __x.swap(rhs&: __y); }
1593 {
1594 __x.swap(rhs&: __y);
1595 }
1596
1597 // [expected.void.obs], observers
1598 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
1599
1600 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
1601
1602 _LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {
1603 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1604 this->__has_val(), "expected::operator* requires the expected to contain a value");
1605 }
1606
1607 _LIBCPP_HIDE_FROM_ABI constexpr void value() const& {
1608 static_assert(is_copy_constructible_v<_Err>);
1609 if (!this->__has_val()) {
1610 std::__throw_bad_expected_access<_Err>(this->__unex());
1611 }
1612 }
1613
1614 _LIBCPP_HIDE_FROM_ABI constexpr void value() && {
1615 static_assert(is_copy_constructible_v<_Err> && is_move_constructible_v<_Err>);
1616 if (!this->__has_val()) {
1617 std::__throw_bad_expected_access<_Err>(std::move(this->__unex()));
1618 }
1619 }
1620
1621 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {
1622 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1623 !this->__has_val(), "expected::error requires the expected to contain an error");
1624 return this->__unex();
1625 }
1626
1627 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {
1628 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1629 !this->__has_val(), "expected::error requires the expected to contain an error");
1630 return this->__unex();
1631 }
1632
1633 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {
1634 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1635 !this->__has_val(), "expected::error requires the expected to contain an error");
1636 return std::move(this->__unex());
1637 }
1638
1639 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
1640 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1641 !this->__has_val(), "expected::error requires the expected to contain an error");
1642 return std::move(this->__unex());
1643 }
1644
1645 template <class _Up = _Err>
1646 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
1647 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
1648 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
1649 if (has_value()) {
1650 return std::forward<_Up>(__error);
1651 }
1652 return error();
1653 }
1654
1655 template <class _Up = _Err>
1656 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
1657 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
1658 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
1659 if (has_value()) {
1660 return std::forward<_Up>(__error);
1661 }
1662 return std::move(error());
1663 }
1664
1665 // [expected.void.monadic], monadic
1666 template <class _Func>
1667 requires is_constructible_v<_Err, _Err&>
1668 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1669 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1670 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1671 static_assert(
1672 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1673 if (has_value()) {
1674 return std::invoke(std::forward<_Func>(__f));
1675 }
1676 return _Up(unexpect, error());
1677 }
1678
1679 template <class _Func>
1680 requires is_constructible_v<_Err, const _Err&>
1681 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1682 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1683 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1684 static_assert(
1685 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1686 if (has_value()) {
1687 return std::invoke(std::forward<_Func>(__f));
1688 }
1689 return _Up(unexpect, error());
1690 }
1691
1692 template <class _Func>
1693 requires is_constructible_v<_Err, _Err&&>
1694 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1695 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1696 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1697 static_assert(
1698 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1699 if (has_value()) {
1700 return std::invoke(std::forward<_Func>(__f));
1701 }
1702 return _Up(unexpect, std::move(error()));
1703 }
1704
1705 template <class _Func>
1706 requires is_constructible_v<_Err, const _Err&&>
1707 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1708 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1709 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1710 static_assert(
1711 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1712 if (has_value()) {
1713 return std::invoke(std::forward<_Func>(__f));
1714 }
1715 return _Up(unexpect, std::move(error()));
1716 }
1717
1718 template <class _Func>
1719 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
1720 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
1721 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1722 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1723 "The result of f(error()) must have the same value_type as this expected");
1724 if (has_value()) {
1725 return _Gp();
1726 }
1727 return std::invoke(std::forward<_Func>(__f), error());
1728 }
1729
1730 template <class _Func>
1731 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
1732 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
1733 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1734 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1735 "The result of f(error()) must have the same value_type as this expected");
1736 if (has_value()) {
1737 return _Gp();
1738 }
1739 return std::invoke(std::forward<_Func>(__f), error());
1740 }
1741
1742 template <class _Func>
1743 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
1744 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1745 static_assert(
1746 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1747 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1748 "The result of f(std::move(error())) must have the same value_type as this expected");
1749 if (has_value()) {
1750 return _Gp();
1751 }
1752 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1753 }
1754
1755 template <class _Func>
1756 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {
1757 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;
1758 static_assert(
1759 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1760 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1761 "The result of f(std::move(error())) must have the same value_type as this expected");
1762 if (has_value()) {
1763 return _Gp();
1764 }
1765 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1766 }
1767
1768 template <class _Func>
1769 requires is_constructible_v<_Err, _Err&>
1770 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1771 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1772 if (!has_value()) {
1773 return expected<_Up, _Err>(unexpect, error());
1774 }
1775 if constexpr (!is_void_v<_Up>) {
1776 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1777 } else {
1778 std::invoke(std::forward<_Func>(__f));
1779 return expected<_Up, _Err>();
1780 }
1781 }
1782
1783 template <class _Func>
1784 requires is_constructible_v<_Err, const _Err&>
1785 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1786 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1787 if (!has_value()) {
1788 return expected<_Up, _Err>(unexpect, error());
1789 }
1790 if constexpr (!is_void_v<_Up>) {
1791 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1792 } else {
1793 std::invoke(std::forward<_Func>(__f));
1794 return expected<_Up, _Err>();
1795 }
1796 }
1797
1798 template <class _Func>
1799 requires is_constructible_v<_Err, _Err&&>
1800 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1801 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1802 if (!has_value()) {
1803 return expected<_Up, _Err>(unexpect, std::move(error()));
1804 }
1805 if constexpr (!is_void_v<_Up>) {
1806 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1807 } else {
1808 std::invoke(std::forward<_Func>(__f));
1809 return expected<_Up, _Err>();
1810 }
1811 }
1812
1813 template <class _Func>
1814 requires is_constructible_v<_Err, const _Err&&>
1815 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1816 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1817 if (!has_value()) {
1818 return expected<_Up, _Err>(unexpect, std::move(error()));
1819 }
1820 if constexpr (!is_void_v<_Up>) {
1821 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1822 } else {
1823 std::invoke(std::forward<_Func>(__f));
1824 return expected<_Up, _Err>();
1825 }
1826 }
1827
1828 template <class _Func>
1829 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {
1830 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;
1831 static_assert(__valid_std_unexpected<_Gp>::value,
1832 "The result of f(error()) must be a valid template argument for unexpected");
1833 if (has_value()) {
1834 return expected<_Tp, _Gp>();
1835 }
1836 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1837 }
1838
1839 template <class _Func>
1840 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {
1841 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;
1842 static_assert(__valid_std_unexpected<_Gp>::value,
1843 "The result of f(error()) must be a valid template argument for unexpected");
1844 if (has_value()) {
1845 return expected<_Tp, _Gp>();
1846 }
1847 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1848 }
1849
1850 template <class _Func>
1851 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {
1852 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;
1853 static_assert(__valid_std_unexpected<_Gp>::value,
1854 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1855 if (has_value()) {
1856 return expected<_Tp, _Gp>();
1857 }
1858 return expected<_Tp, _Gp>(
1859 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1860 }
1861
1862 template <class _Func>
1863 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {
1864 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;
1865 static_assert(__valid_std_unexpected<_Gp>::value,
1866 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1867 if (has_value()) {
1868 return expected<_Tp, _Gp>();
1869 }
1870 return expected<_Tp, _Gp>(
1871 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1872 }
1873
1874 // [expected.void.eq], equality operators
1875 template <class _T2, class _E2>
1876 requires is_void_v<_T2>
1877 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y)
1878# if _LIBCPP_STD_VER >= 26
1879 requires requires {
1880 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1881 }
1882# endif
1883 {
1884 if (__x.__has_val() != __y.__has_val()) {
1885 return false;
1886 } else {
1887 return __x.__has_val() || static_cast<bool>(__x.__unex() == __y.__unex());
1888 }
1889 }
1890
1891 template <class _E2>
1892 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y)
1893# if _LIBCPP_STD_VER >= 26
1894 requires requires {
1895 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1896 }
1897# endif
1898 {
1899 return !__x.__has_val() && static_cast<bool>(__x.__unex() == __y.error());
1900 }
1901};
1902
1903_LIBCPP_END_NAMESPACE_STD
1904
1905#endif // _LIBCPP_STD_VER >= 23
1906
1907_LIBCPP_POP_MACROS
1908
1909#endif // _LIBCPP___EXPECTED_EXPECTED_H
1910