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_relocatable.h>
35#include <__type_traits/is_same.h>
36#include <__type_traits/is_swappable.h>
37#include <__type_traits/is_trivially_constructible.h>
38#include <__type_traits/is_trivially_destructible.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<__is_trivially_relocatable_v<_Tp> && __is_trivially_relocatable_v<_Err>, expected, void>;
476
477 template <class _Up>
478 using rebind = expected<_Up, error_type>;
479
480 // [expected.object.ctor], constructors
481 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened
482 requires is_default_constructible_v<_Tp>
483 : __base(in_place) {}
484
485 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
486
487 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
488 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&
489 is_trivially_copy_constructible_v<_Err>)
490 = default;
491
492 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
493 is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened
494 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
495 !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
496 : __base(__other.__has_val(), __other.__union()) {}
497
498 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
499 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&
500 is_trivially_move_constructible_v<_Err>)
501 = default;
502
503 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(
504 is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)
505 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
506 !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))
507 : __base(__other.__has_val(), std::move(__other.__union())) {}
508
509private:
510 template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>
511 using __can_convert _LIBCPP_NODEBUG = _And<
512 is_constructible<_Tp, _UfQual>,
513 is_constructible<_Err, _OtherErrQual>,
514 _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
515 _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>, // use the copy constructor instead, see #92676
516 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
517 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
518 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
519 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
520 _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>,
521 _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>,
522 _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>,
523 _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>,
524 true_type>,
525 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
526 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
527 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
528 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >;
529
530 template <class _Func, class... _Args>
531 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
532 std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
533 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
534
535 template <class _Func, class... _Args>
536 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
537 std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
538 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
539
540public:
541 template <class _Up, class _OtherErr>
542 requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
543 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> ||
544 !is_convertible_v<const _OtherErr&, _Err>)
545 expected(const expected<_Up, _OtherErr>& __other) noexcept(
546 is_nothrow_constructible_v<_Tp, const _Up&> &&
547 is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
548 : __base(__other.__has_val(), __other.__union()) {}
549
550 template <class _Up, class _OtherErr>
551 requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value
552 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>)
553 expected(expected<_Up, _OtherErr>&& __other) noexcept(
554 is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
555 : __base(__other.__has_val(), std::move(__other.__union())) {}
556
557 template <class _Up = remove_cv_t<_Tp>>
558 requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&
559 !is_same_v<remove_cvref_t<_Up>, unexpect_t> && is_constructible_v<_Tp, _Up> &&
560 !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
561 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))
562 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)
563 expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened
564 : __base(in_place, std::forward<_Up>(__u)) {}
565
566 template <class _OtherErr>
567 requires is_constructible_v<_Err, const _OtherErr&>
568 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
569 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
570 : __base(unexpect, __unex.error()) {}
571
572 template <class _OtherErr>
573 requires is_constructible_v<_Err, _OtherErr>
574 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
575 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
576 : __base(unexpect, std::move(__unex.error())) {}
577
578 template <class... _Args>
579 requires is_constructible_v<_Tp, _Args...>
580 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(
581 is_nothrow_constructible_v<_Tp, _Args...>) // strengthened
582 : __base(in_place, std::forward<_Args>(__args)...) {}
583
584 template <class _Up, class... _Args>
585 requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >
586 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
587 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened
588 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
589
590 template <class... _Args>
591 requires is_constructible_v<_Err, _Args...>
592 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
593 is_nothrow_constructible_v<_Err, _Args...>) // strengthened
594 : __base(unexpect, std::forward<_Args>(__args)...) {}
595
596 template <class _Up, class... _Args>
597 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
598 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
599 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
600 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
601
602 // [expected.object.dtor], destructor
603
604 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
605
606private:
607 template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>
608 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {
609 if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {
610 this->__destroy();
611 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
612 } else if constexpr (is_nothrow_move_constructible_v<_T1>) {
613 _T1 __tmp(std::forward<_Args>(__args)...);
614 this->__destroy();
615 this->__construct(_Tag{}, std::move(__tmp));
616 } else {
617 static_assert(
618 is_nothrow_move_constructible_v<_T2>,
619 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
620 "be reverted to the previous state in case an exception is thrown during the assignment.");
621 _T2 __tmp(std::move(__oldval));
622 this->__destroy();
623 auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });
624 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
625 __trans.__complete();
626 }
627 }
628
629public:
630 // [expected.object.assign], assignment
631 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
632
633 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
634 is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&
635 is_nothrow_copy_constructible_v<_Err>) // strengthened
636 requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&
637 is_copy_constructible_v<_Err> &&
638 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
639 {
640 if (this->__has_val() && __rhs.__has_val()) {
641 this->__val() = __rhs.__val();
642 } else if (this->__has_val()) {
643 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());
644 } else if (__rhs.__has_val()) {
645 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());
646 } else {
647 this->__unex() = __rhs.__unex();
648 }
649 return *this;
650 }
651
652 _LIBCPP_HIDE_FROM_ABI constexpr expected&
653 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&
654 is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
655 requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&
656 is_move_assignable_v<_Err> &&
657 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
658 {
659 if (this->__has_val() && __rhs.__has_val()) {
660 this->__val() = std::move(__rhs.__val());
661 } else if (this->__has_val()) {
662 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));
663 } else if (__rhs.__has_val()) {
664 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));
665 } else {
666 this->__unex() = std::move(__rhs.__unex());
667 }
668 return *this;
669 }
670
671 template <class _Up = remove_cv_t<_Tp>>
672 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)
673 requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
674 is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&
675 (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||
676 is_nothrow_move_constructible_v<_Err>))
677 {
678 if (this->__has_val()) {
679 this->__val() = std::forward<_Up>(__v);
680 } else {
681 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));
682 }
683 return *this;
684 }
685
686private:
687 template <class _OtherErrQual>
688 static constexpr bool __can_assign_from_unexpected =
689 _And<is_constructible<_Err, _OtherErrQual>,
690 is_assignable<_Err&, _OtherErrQual>,
691 _Or<is_nothrow_constructible<_Err, _OtherErrQual>,
692 is_nothrow_move_constructible<_Tp>,
693 is_nothrow_move_constructible<_Err>>>::value;
694
695public:
696 template <class _OtherErr>
697 requires(__can_assign_from_unexpected<const _OtherErr&>)
698 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
699 if (this->__has_val()) {
700 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error());
701 } else {
702 this->__unex() = __un.error();
703 }
704 return *this;
705 }
706
707 template <class _OtherErr>
708 requires(__can_assign_from_unexpected<_OtherErr>)
709 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
710 if (this->__has_val()) {
711 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error()));
712 } else {
713 this->__unex() = std::move(__un.error());
714 }
715 return *this;
716 }
717
718 template <class... _Args>
719 requires is_nothrow_constructible_v<_Tp, _Args...>
720 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept {
721 this->__destroy();
722 this->__construct(in_place, std::forward<_Args>(__args)...);
723 return this->__val();
724 }
725
726 template <class _Up, class... _Args>
727 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
728 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept {
729 this->__destroy();
730 this->__construct(in_place, __il, std::forward<_Args>(__args)...);
731 return this->__val();
732 }
733
734public:
735 // [expected.object.swap], swap
736 _LIBCPP_HIDE_FROM_ABI constexpr void
737 swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> &&
738 is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
739 requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> &&
740 is_move_constructible_v<_Err> &&
741 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
742 {
743 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
744 if constexpr (is_nothrow_move_constructible_v<_Err>) {
745 _Err __tmp(std::move(__with_err.__unex()));
746 __with_err.__destroy();
747 auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); });
748 __with_err.__construct(in_place, std::move(__with_val.__val()));
749 __trans.__complete();
750 __with_val.__destroy();
751 __with_val.__construct(unexpect, std::move(__tmp));
752 } else {
753 static_assert(is_nothrow_move_constructible_v<_Tp>,
754 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
755 "that it can be reverted to the previous state in case an exception is thrown during swap.");
756 _Tp __tmp(std::move(__with_val.__val()));
757 __with_val.__destroy();
758 auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); });
759 __with_val.__construct(unexpect, std::move(__with_err.__unex()));
760 __trans.__complete();
761 __with_err.__destroy();
762 __with_err.__construct(in_place, std::move(__tmp));
763 }
764 };
765
766 if (this->__has_val()) {
767 if (__rhs.__has_val()) {
768 using std::swap;
769 swap(this->__val(), __rhs.__val());
770 } else {
771 __swap_val_unex_impl(*this, __rhs);
772 }
773 } else {
774 if (__rhs.__has_val()) {
775 __swap_val_unex_impl(__rhs, *this);
776 } else {
777 using std::swap;
778 swap(this->__unex(), __rhs.__unex());
779 }
780 }
781 }
782
783 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(rhs&: __y)))
784 requires requires { __x.swap(rhs&: __y); }
785 {
786 __x.swap(rhs&: __y);
787 }
788
789 // [expected.object.obs], observers
790 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept {
791 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
792 this->__has_val(), "expected::operator-> requires the expected to contain a value");
793 return std::addressof(this->__val());
794 }
795
796 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept {
797 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
798 this->__has_val(), "expected::operator-> requires the expected to contain a value");
799 return std::addressof(this->__val());
800 }
801
802 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
803 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
804 this->__has_val(), "expected::operator* requires the expected to contain a value");
805 return this->__val();
806 }
807
808 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
809 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
810 this->__has_val(), "expected::operator* requires the expected to contain a value");
811 return this->__val();
812 }
813
814 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
815 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
816 this->__has_val(), "expected::operator* requires the expected to contain a value");
817 return std::move(this->__val());
818 }
819
820 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
821 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
822 this->__has_val(), "expected::operator* requires the expected to contain a value");
823 return std::move(this->__val());
824 }
825
826 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
827
828 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
829
830# if _LIBCPP_STD_VER >= 29
831 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
832# endif
833
834 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
835 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
836 if (!this->__has_val()) {
837 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
838 }
839 return this->__val();
840 }
841
842 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
843 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
844 if (!this->__has_val()) {
845 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
846 }
847 return this->__val();
848 }
849
850 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& {
851 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
852 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
853 if (!this->__has_val()) {
854 std::__throw_bad_expected_access<_Err>(std::move(error()));
855 }
856 return std::move(this->__val());
857 }
858
859 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
860 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
861 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
862 if (!this->__has_val()) {
863 std::__throw_bad_expected_access<_Err>(std::move(error()));
864 }
865 return std::move(this->__val());
866 }
867
868 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& 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 _Err& error() & noexcept {
875 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
876 !this->__has_val(), "expected::error requires the expected to contain an error");
877 return this->__unex();
878 }
879
880 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& 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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
887 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
888 !this->__has_val(), "expected::error requires the expected to contain an error");
889 return std::move(this->__unex());
890 }
891
892 template <class _Up = remove_cv_t<_Tp>>
893 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
894 static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible");
895 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
896 return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v));
897 }
898
899 template <class _Up = remove_cv_t<_Tp>>
900 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
901 static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible");
902 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
903 return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v));
904 }
905
906 template <class _Up = _Err>
907 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
908 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
909 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
910 if (has_value())
911 return std::forward<_Up>(__error);
912 return error();
913 }
914
915 template <class _Up = _Err>
916 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
917 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
918 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
919 if (has_value())
920 return std::forward<_Up>(__error);
921 return std::move(error());
922 }
923
924 // [expected.void.monadic], monadic
925 template <class _Func>
926 requires is_constructible_v<_Err, _Err&>
927 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
928 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;
929 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected");
930 static_assert(is_same_v<typename _Up::error_type, _Err>,
931 "The result of f(value()) must have the same error_type as this expected");
932 if (has_value()) {
933 return std::invoke(std::forward<_Func>(__f), this->__val());
934 }
935 return _Up(unexpect, error());
936 }
937
938 template <class _Func>
939 requires is_constructible_v<_Err, const _Err&>
940 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
941 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>;
942 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected");
943 static_assert(is_same_v<typename _Up::error_type, _Err>,
944 "The result of f(value()) must have the same error_type as this expected");
945 if (has_value()) {
946 return std::invoke(std::forward<_Func>(__f), this->__val());
947 }
948 return _Up(unexpect, error());
949 }
950
951 template <class _Func>
952 requires is_constructible_v<_Err, _Err&&>
953 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
954 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>;
955 static_assert(
956 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected");
957 static_assert(is_same_v<typename _Up::error_type, _Err>,
958 "The result of f(std::move(value())) must have the same error_type as this expected");
959 if (has_value()) {
960 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
961 }
962 return _Up(unexpect, std::move(error()));
963 }
964
965 template <class _Func>
966 requires is_constructible_v<_Err, const _Err&&>
967 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
968 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
969 static_assert(
970 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected");
971 static_assert(is_same_v<typename _Up::error_type, _Err>,
972 "The result of f(std::move(value())) must have the same error_type as this expected");
973 if (has_value()) {
974 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
975 }
976 return _Up(unexpect, std::move(error()));
977 }
978
979 template <class _Func>
980 requires is_constructible_v<_Tp, _Tp&>
981 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
982 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
983 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
984 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
985 "The result of f(error()) must have the same value_type as this expected");
986 if (has_value()) {
987 return _Gp(in_place, this->__val());
988 }
989 return std::invoke(std::forward<_Func>(__f), error());
990 }
991
992 template <class _Func>
993 requires is_constructible_v<_Tp, const _Tp&>
994 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
995 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
996 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
997 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
998 "The result of f(error()) must have the same value_type as this expected");
999 if (has_value()) {
1000 return _Gp(in_place, this->__val());
1001 }
1002 return std::invoke(std::forward<_Func>(__f), error());
1003 }
1004
1005 template <class _Func>
1006 requires is_constructible_v<_Tp, _Tp&&>
1007 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
1008 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1009 static_assert(
1010 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1011 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1012 "The result of f(std::move(error())) must have the same value_type as this expected");
1013 if (has_value()) {
1014 return _Gp(in_place, std::move(this->__val()));
1015 }
1016 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1017 }
1018
1019 template <class _Func>
1020 requires is_constructible_v<_Tp, const _Tp&&>
1021 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {
1022 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;
1023 static_assert(
1024 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1025 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1026 "The result of f(std::move(error())) must have the same value_type as this expected");
1027 if (has_value()) {
1028 return _Gp(in_place, std::move(this->__val()));
1029 }
1030 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1031 }
1032
1033 template <class _Func>
1034 requires is_constructible_v<_Err, _Err&>
1035 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1036 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
1037 if (!has_value()) {
1038 return expected<_Up, _Err>(unexpect, error());
1039 }
1040 if constexpr (!is_void_v<_Up>) {
1041 return expected<_Up, _Err>(
1042 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());
1043 } else {
1044 std::invoke(std::forward<_Func>(__f), this->__val());
1045 return expected<_Up, _Err>();
1046 }
1047 }
1048
1049 template <class _Func>
1050 requires is_constructible_v<_Err, const _Err&>
1051 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1052 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
1053 if (!has_value()) {
1054 return expected<_Up, _Err>(unexpect, error());
1055 }
1056 if constexpr (!is_void_v<_Up>) {
1057 return expected<_Up, _Err>(
1058 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());
1059 } else {
1060 std::invoke(std::forward<_Func>(__f), this->__val());
1061 return expected<_Up, _Err>();
1062 }
1063 }
1064
1065 template <class _Func>
1066 requires is_constructible_v<_Err, _Err&&>
1067 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1068 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
1069 if (!has_value()) {
1070 return expected<_Up, _Err>(unexpect, std::move(error()));
1071 }
1072 if constexpr (!is_void_v<_Up>) {
1073 return expected<_Up, _Err>(
1074 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));
1075 } else {
1076 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1077 return expected<_Up, _Err>();
1078 }
1079 }
1080
1081 template <class _Func>
1082 requires is_constructible_v<_Err, const _Err&&>
1083 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1084 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>;
1085 if (!has_value()) {
1086 return expected<_Up, _Err>(unexpect, std::move(error()));
1087 }
1088 if constexpr (!is_void_v<_Up>) {
1089 return expected<_Up, _Err>(
1090 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));
1091 } else {
1092 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1093 return expected<_Up, _Err>();
1094 }
1095 }
1096
1097 template <class _Func>
1098 requires is_constructible_v<_Tp, _Tp&>
1099 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {
1100 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;
1101 static_assert(__valid_std_unexpected<_Gp>::value,
1102 "The result of f(error()) must be a valid template argument for unexpected");
1103 if (has_value()) {
1104 return expected<_Tp, _Gp>(in_place, this->__val());
1105 }
1106 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1107 }
1108
1109 template <class _Func>
1110 requires is_constructible_v<_Tp, const _Tp&>
1111 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {
1112 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;
1113 static_assert(__valid_std_unexpected<_Gp>::value,
1114 "The result of f(error()) must be a valid template argument for unexpected");
1115 if (has_value()) {
1116 return expected<_Tp, _Gp>(in_place, this->__val());
1117 }
1118 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1119 }
1120
1121 template <class _Func>
1122 requires is_constructible_v<_Tp, _Tp&&>
1123 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {
1124 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;
1125 static_assert(__valid_std_unexpected<_Gp>::value,
1126 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1127 if (has_value()) {
1128 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1129 }
1130 return expected<_Tp, _Gp>(
1131 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1132 }
1133
1134 template <class _Func>
1135 requires is_constructible_v<_Tp, const _Tp&&>
1136 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {
1137 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;
1138 static_assert(__valid_std_unexpected<_Gp>::value,
1139 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1140 if (has_value()) {
1141 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1142 }
1143 return expected<_Tp, _Gp>(
1144 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1145 }
1146
1147 // [expected.object.eq], equality operators
1148 template <class _T2, class _E2>
1149 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y)
1150 requires(!is_void_v<_T2>)
1151# if _LIBCPP_STD_VER >= 26
1152 && requires {
1153 { *__x == *__y } -> __core_convertible_to<bool>;
1154 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1155 }
1156# endif
1157 {
1158 if (__x.__has_val() != __y.__has_val()) {
1159 return false;
1160 } else {
1161 if (__x.__has_val()) {
1162 return __x.__val() == __y.__val();
1163 } else {
1164 return __x.__unex() == __y.__unex();
1165 }
1166 }
1167 }
1168
1169 // The unusual signature avoids constraint recursion via ADL through
1170 // std::expected, see https://llvm.org/PR160431. Note that this only
1171 // triggers with compilers that implement https://wg21.link/CWG2369.
1172 template <class _T2, same_as<_Tp> _Tp2>
1173 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected<_Tp2, _Err>& __x, const _T2& __v)
1174# if _LIBCPP_STD_VER >= 26
1175 requires(!__is_std_expected<_T2>::value) && requires {
1176 { *__x == __v } -> __core_convertible_to<bool>;
1177 }
1178# endif
1179 {
1180 return __x.__has_val() && std::__into_bool(b: __x.__val() == __v);
1181 }
1182
1183 template <class _E2>
1184 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e)
1185# if _LIBCPP_STD_VER >= 26
1186 requires requires {
1187 { __x.error() == __e.error() } -> __core_convertible_to<bool>;
1188 }
1189# endif
1190 {
1191 return !__x.__has_val() && std::__into_bool(b: __x.__unex() == __e.error());
1192 }
1193};
1194
1195template <class _Err>
1196class __expected_void_base {
1197 struct __empty_t {};
1198 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
1199 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
1200 // it's not clear that it's implementable, given that the function is allowed to clobber
1201 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
1202 union __union_t {
1203 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
1204 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
1205 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1206 = default;
1207 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
1208 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
1209 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1210 = default;
1211 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
1212 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;
1213
1214 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}
1215
1216 template <class... _Args>
1217 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
1218 : __unex_(std::forward<_Args>(__args)...) {}
1219
1220 template <class _Func, class... _Args>
1221 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
1222 __expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
1223 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
1224
1225 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1226 requires(is_trivially_destructible_v<_Err>)
1227 = default;
1228
1229 // __repr's destructor handles this
1230 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1231 requires(!is_trivially_destructible_v<_Err>)
1232 {}
1233
1234 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;
1235 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
1236 };
1237
1238 static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;
1239 static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
1240
1241 struct __repr {
1242 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
1243
1244 template <class... _Args>
1245 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}
1246
1247 template <class... _Args>
1248 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
1249 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1250
1251 template <class... _Args>
1252 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
1253 _Args&&... __args)
1254 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1255
1256 template <class _OtherUnion>
1257 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
1258 requires(__allow_reusing_expected_tail_padding)
1259 : __union_(__conditional_no_unique_address_invoke_tag{},
1260 [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
1261 __has_val_(__has_val) {}
1262
1263 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
1264 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
1265 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1266 = default;
1267 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
1268 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
1269 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1270 = default;
1271
1272 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
1273 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
1274
1275 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1276 requires(is_trivially_destructible_v<_Err>)
1277 = default;
1278
1279 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1280 requires(!is_trivially_destructible_v<_Err>)
1281 {
1282 __destroy_union_member();
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 std::destroy_at(&__union_.__v);
1289 }
1290
1291 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1292 requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)
1293 {
1294 __destroy_union_member();
1295 std::destroy_at(&__union_.__v);
1296 }
1297
1298 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)
1299 requires(__allow_reusing_expected_tail_padding)
1300 {
1301 std::construct_at(&__union_.__v, in_place);
1302 __has_val_ = true;
1303 }
1304
1305 template <class... _Args>
1306 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
1307 requires(__allow_reusing_expected_tail_padding)
1308 {
1309 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
1310 __has_val_ = false;
1311 }
1312
1313 private:
1314 template <class>
1315 friend class __expected_void_base;
1316
1317 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
1318 requires(!is_trivially_destructible_v<_Err>)
1319 {
1320 if (!__has_val_)
1321 std::destroy_at(std::addressof(__union_.__v.__unex_));
1322 }
1323
1324 template <class _OtherUnion>
1325 _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
1326 requires(__allow_reusing_expected_tail_padding)
1327 {
1328 if (__has_val)
1329 return __union_t(in_place);
1330 else
1331 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1332 }
1333
1334 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
1335 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
1336 };
1337
1338 template <class _OtherUnion>
1339 _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
1340 requires(__put_flag_in_tail)
1341 {
1342 if (__has_val)
1343 return __repr(in_place);
1344 else
1345 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1346 }
1347
1348protected:
1349 template <class... _Args>
1350 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)
1351 : __repr_(in_place, std::forward<_Args>(__args)...) {}
1352
1353 template <class _OtherUnion>
1354 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(bool __has_val, _OtherUnion&& __other)
1355 requires(__put_flag_in_tail)
1356 : __repr_(__conditional_no_unique_address_invoke_tag{},
1357 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
1358
1359 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
1360 if constexpr (__put_flag_in_tail)
1361 std::destroy_at(&__repr_.__v);
1362 else
1363 __repr_.__v.__destroy_union();
1364 }
1365
1366 template <class _Tag, class... _Args>
1367 _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
1368 if constexpr (__put_flag_in_tail)
1369 std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
1370 else
1371 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
1372 }
1373
1374 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
1375 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
1376 _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
1377 _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
1378 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
1379
1380private:
1381 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
1382};
1383
1384template <class _Tp, class _Err>
1385 requires is_void_v<_Tp>
1386class expected<_Tp, _Err> : private __expected_void_base<_Err> {
1387 static_assert(__valid_std_unexpected<_Err>::value,
1388 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "
1389 "valid argument for unexpected<E> is ill-formed");
1390
1391 template <class, class>
1392 friend class expected;
1393
1394 template <class _Up, class _OtherErr, class _OtherErrQual>
1395 using __can_convert _LIBCPP_NODEBUG =
1396 _And< is_void<_Up>,
1397 is_constructible<_Err, _OtherErrQual>,
1398 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
1399 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
1400 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
1401 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>;
1402
1403 using __base _LIBCPP_NODEBUG = __expected_void_base<_Err>;
1404
1405public:
1406 using value_type = _Tp;
1407 using error_type = _Err;
1408 using unexpected_type = unexpected<_Err>;
1409
1410 template <class _Up>
1411 using rebind = expected<_Up, error_type>;
1412
1413 // [expected.void.ctor], constructors
1414 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}
1415
1416 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
1417
1418 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
1419 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1420 = default;
1421
1422 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(
1423 is_nothrow_copy_constructible_v<_Err>) // strengthened
1424 requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)
1425 : __base(__rhs.__has_val(), __rhs.__union()) {}
1426
1427 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
1428 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1429 = default;
1430
1431 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __rhs) noexcept(is_nothrow_move_constructible_v<_Err>)
1432 requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>)
1433 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1434
1435 template <class _Up, class _OtherErr>
1436 requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value
1437 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>)
1438 expected(const expected<_Up, _OtherErr>& __rhs) noexcept(
1439 is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1440 : __base(__rhs.__has_val(), __rhs.__union()) {}
1441
1442 template <class _Up, class _OtherErr>
1443 requires __can_convert<_Up, _OtherErr, _OtherErr>::value
1444 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1445 expected(expected<_Up, _OtherErr>&& __rhs) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1446 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1447
1448 template <class _OtherErr>
1449 requires is_constructible_v<_Err, const _OtherErr&>
1450 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
1451 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened
1452 : __base(unexpect, __unex.error()) {}
1453
1454 template <class _OtherErr>
1455 requires is_constructible_v<_Err, _OtherErr>
1456 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
1457 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened
1458 : __base(unexpect, std::move(__unex.error())) {}
1459
1460 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}
1461
1462 template <class... _Args>
1463 requires is_constructible_v<_Err, _Args...>
1464 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
1465 is_nothrow_constructible_v<_Err, _Args...>) // strengthened
1466 : __base(unexpect, std::forward<_Args>(__args)...) {}
1467
1468 template <class _Up, class... _Args>
1469 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
1470 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
1471 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
1472 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
1473
1474private:
1475 template <class _Func, class... _Args>
1476 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
1477 __expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
1478 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
1479
1480public:
1481 // [expected.void.dtor], destructor
1482
1483 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
1484
1485private:
1486 template <class... _Args>
1487 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(unexpect_t, _Args&&... __args) {
1488 _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");
1489
1490 this->__destroy();
1491 auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });
1492 this->__construct(unexpect, std::forward<_Args>(__args)...);
1493 __trans.__complete();
1494 }
1495
1496 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(in_place_t) {
1497 _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");
1498
1499 this->__destroy();
1500 this->__construct(in_place);
1501 }
1502
1503public:
1504 // [expected.void.assign], assignment
1505 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
1506
1507 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
1508 is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened
1509 requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)
1510 {
1511 if (this->__has_val()) {
1512 if (!__rhs.__has_val()) {
1513 __reinit_expected(unexpect, __rhs.__unex());
1514 }
1515 } else {
1516 if (__rhs.__has_val()) {
1517 __reinit_expected(in_place);
1518 } else {
1519 this->__unex() = __rhs.__unex();
1520 }
1521 }
1522 return *this;
1523 }
1524
1525 _LIBCPP_HIDE_FROM_ABI constexpr expected&
1526 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
1527 requires(is_move_assignable_v<_Err> && is_move_constructible_v<_Err>)
1528 {
1529 if (this->__has_val()) {
1530 if (!__rhs.__has_val()) {
1531 __reinit_expected(unexpect, std::move(__rhs.__unex()));
1532 }
1533 } else {
1534 if (__rhs.__has_val()) {
1535 __reinit_expected(in_place);
1536 } else {
1537 this->__unex() = std::move(__rhs.__unex());
1538 }
1539 }
1540 return *this;
1541 }
1542
1543 template <class _OtherErr>
1544 requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>)
1545 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
1546 if (this->__has_val()) {
1547 __reinit_expected(unexpect, __un.error());
1548 } else {
1549 this->__unex() = __un.error();
1550 }
1551 return *this;
1552 }
1553
1554 template <class _OtherErr>
1555 requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>)
1556 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
1557 if (this->__has_val()) {
1558 __reinit_expected(unexpect, std::move(__un.error()));
1559 } else {
1560 this->__unex() = std::move(__un.error());
1561 }
1562 return *this;
1563 }
1564
1565 _LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept {
1566 if (!this->__has_val()) {
1567 __reinit_expected(in_place);
1568 }
1569 }
1570
1571 // [expected.void.swap], swap
1572 _LIBCPP_HIDE_FROM_ABI constexpr void
1573 swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
1574 requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>)
1575 {
1576 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
1577 // May throw, but will re-engage `__with_val` in that case.
1578 __with_val.__reinit_expected(unexpect, std::move(__with_err.__unex()));
1579 // Will not throw.
1580 __with_err.__reinit_expected(in_place);
1581 };
1582
1583 if (this->__has_val()) {
1584 if (!__rhs.__has_val()) {
1585 __swap_val_unex_impl(*this, __rhs);
1586 }
1587 } else {
1588 if (__rhs.__has_val()) {
1589 __swap_val_unex_impl(__rhs, *this);
1590 } else {
1591 using std::swap;
1592 swap(this->__unex(), __rhs.__unex());
1593 }
1594 }
1595 }
1596
1597 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(rhs&: __y)))
1598 requires requires { __x.swap(rhs&: __y); }
1599 {
1600 __x.swap(rhs&: __y);
1601 }
1602
1603 // [expected.void.obs], observers
1604 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
1605
1606 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
1607
1608# if _LIBCPP_STD_VER >= 29
1609 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
1610# endif
1611
1612 _LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {
1613 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1614 this->__has_val(), "expected::operator* requires the expected to contain a value");
1615 }
1616
1617 _LIBCPP_HIDE_FROM_ABI constexpr void value() const& {
1618 static_assert(is_copy_constructible_v<_Err>);
1619 if (!this->__has_val()) {
1620 std::__throw_bad_expected_access<_Err>(this->__unex());
1621 }
1622 }
1623
1624 _LIBCPP_HIDE_FROM_ABI constexpr void value() && {
1625 static_assert(is_copy_constructible_v<_Err> && is_move_constructible_v<_Err>);
1626 if (!this->__has_val()) {
1627 std::__throw_bad_expected_access<_Err>(std::move(this->__unex()));
1628 }
1629 }
1630
1631 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& 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 _Err& error() & noexcept {
1638 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1639 !this->__has_val(), "expected::error requires the expected to contain an error");
1640 return this->__unex();
1641 }
1642
1643 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& 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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
1650 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1651 !this->__has_val(), "expected::error requires the expected to contain an error");
1652 return std::move(this->__unex());
1653 }
1654
1655 template <class _Up = _Err>
1656 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
1657 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy 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 error();
1663 }
1664
1665 template <class _Up = _Err>
1666 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
1667 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
1668 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
1669 if (has_value()) {
1670 return std::forward<_Up>(__error);
1671 }
1672 return std::move(error());
1673 }
1674
1675 // [expected.void.monadic], monadic
1676 template <class _Func>
1677 requires is_constructible_v<_Err, _Err&>
1678 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1679 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1680 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1681 static_assert(
1682 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1683 if (has_value()) {
1684 return std::invoke(std::forward<_Func>(__f));
1685 }
1686 return _Up(unexpect, error());
1687 }
1688
1689 template <class _Func>
1690 requires is_constructible_v<_Err, const _Err&>
1691 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1692 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1693 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1694 static_assert(
1695 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1696 if (has_value()) {
1697 return std::invoke(std::forward<_Func>(__f));
1698 }
1699 return _Up(unexpect, error());
1700 }
1701
1702 template <class _Func>
1703 requires is_constructible_v<_Err, _Err&&>
1704 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1705 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1706 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1707 static_assert(
1708 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1709 if (has_value()) {
1710 return std::invoke(std::forward<_Func>(__f));
1711 }
1712 return _Up(unexpect, std::move(error()));
1713 }
1714
1715 template <class _Func>
1716 requires is_constructible_v<_Err, const _Err&&>
1717 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1718 using _Up = remove_cvref_t<invoke_result_t<_Func>>;
1719 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");
1720 static_assert(
1721 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1722 if (has_value()) {
1723 return std::invoke(std::forward<_Func>(__f));
1724 }
1725 return _Up(unexpect, std::move(error()));
1726 }
1727
1728 template <class _Func>
1729 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
1730 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
1731 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1732 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1733 "The result of f(error()) must have the same value_type as this expected");
1734 if (has_value()) {
1735 return _Gp();
1736 }
1737 return std::invoke(std::forward<_Func>(__f), error());
1738 }
1739
1740 template <class _Func>
1741 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
1742 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
1743 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
1744 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1745 "The result of f(error()) must have the same value_type as this expected");
1746 if (has_value()) {
1747 return _Gp();
1748 }
1749 return std::invoke(std::forward<_Func>(__f), error());
1750 }
1751
1752 template <class _Func>
1753 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
1754 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1755 static_assert(
1756 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1757 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1758 "The result of f(std::move(error())) must have the same value_type as this expected");
1759 if (has_value()) {
1760 return _Gp();
1761 }
1762 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1763 }
1764
1765 template <class _Func>
1766 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {
1767 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;
1768 static_assert(
1769 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");
1770 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
1771 "The result of f(std::move(error())) must have the same value_type as this expected");
1772 if (has_value()) {
1773 return _Gp();
1774 }
1775 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1776 }
1777
1778 template <class _Func>
1779 requires is_constructible_v<_Err, _Err&>
1780 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1781 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1782 if (!has_value()) {
1783 return expected<_Up, _Err>(unexpect, error());
1784 }
1785 if constexpr (!is_void_v<_Up>) {
1786 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1787 } else {
1788 std::invoke(std::forward<_Func>(__f));
1789 return expected<_Up, _Err>();
1790 }
1791 }
1792
1793 template <class _Func>
1794 requires is_constructible_v<_Err, const _Err&>
1795 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1796 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1797 if (!has_value()) {
1798 return expected<_Up, _Err>(unexpect, error());
1799 }
1800 if constexpr (!is_void_v<_Up>) {
1801 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1802 } else {
1803 std::invoke(std::forward<_Func>(__f));
1804 return expected<_Up, _Err>();
1805 }
1806 }
1807
1808 template <class _Func>
1809 requires is_constructible_v<_Err, _Err&&>
1810 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1811 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1812 if (!has_value()) {
1813 return expected<_Up, _Err>(unexpect, std::move(error()));
1814 }
1815 if constexpr (!is_void_v<_Up>) {
1816 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1817 } else {
1818 std::invoke(std::forward<_Func>(__f));
1819 return expected<_Up, _Err>();
1820 }
1821 }
1822
1823 template <class _Func>
1824 requires is_constructible_v<_Err, const _Err&&>
1825 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1826 using _Up = remove_cv_t<invoke_result_t<_Func>>;
1827 if (!has_value()) {
1828 return expected<_Up, _Err>(unexpect, std::move(error()));
1829 }
1830 if constexpr (!is_void_v<_Up>) {
1831 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1832 } else {
1833 std::invoke(std::forward<_Func>(__f));
1834 return expected<_Up, _Err>();
1835 }
1836 }
1837
1838 template <class _Func>
1839 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {
1840 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;
1841 static_assert(__valid_std_unexpected<_Gp>::value,
1842 "The result of f(error()) must be a valid template argument for unexpected");
1843 if (has_value()) {
1844 return expected<_Tp, _Gp>();
1845 }
1846 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1847 }
1848
1849 template <class _Func>
1850 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {
1851 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;
1852 static_assert(__valid_std_unexpected<_Gp>::value,
1853 "The result of f(error()) must be a valid template argument for unexpected");
1854 if (has_value()) {
1855 return expected<_Tp, _Gp>();
1856 }
1857 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1858 }
1859
1860 template <class _Func>
1861 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {
1862 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;
1863 static_assert(__valid_std_unexpected<_Gp>::value,
1864 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1865 if (has_value()) {
1866 return expected<_Tp, _Gp>();
1867 }
1868 return expected<_Tp, _Gp>(
1869 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1870 }
1871
1872 template <class _Func>
1873 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {
1874 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;
1875 static_assert(__valid_std_unexpected<_Gp>::value,
1876 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1877 if (has_value()) {
1878 return expected<_Tp, _Gp>();
1879 }
1880 return expected<_Tp, _Gp>(
1881 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1882 }
1883
1884 // [expected.void.eq], equality operators
1885 template <class _T2, class _E2>
1886 requires is_void_v<_T2>
1887 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y)
1888# if _LIBCPP_STD_VER >= 26
1889 requires requires {
1890 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1891 }
1892# endif
1893 {
1894 if (__x.__has_val() != __y.__has_val()) {
1895 return false;
1896 } else {
1897 return __x.__has_val() || std::__into_bool(b: __x.__unex() == __y.__unex());
1898 }
1899 }
1900
1901 template <class _E2>
1902 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y)
1903# if _LIBCPP_STD_VER >= 26
1904 requires requires {
1905 { __x.error() == __y.error() } -> __core_convertible_to<bool>;
1906 }
1907# endif
1908 {
1909 return !__x.__has_val() && std::__into_bool(b: __x.__unex() == __y.error());
1910 }
1911};
1912
1913_LIBCPP_END_NAMESPACE_STD
1914
1915#endif // _LIBCPP_STD_VER >= 23
1916
1917_LIBCPP_POP_MACROS
1918
1919#endif // _LIBCPP___EXPECTED_EXPECTED_H
1920