2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___EXPECTED_EXPECTED_H
10 #define _LIBCPP___EXPECTED_EXPECTED_H
14 #include <__expected/bad_expected_access.h>
15 #include <__expected/unexpect.h>
16 #include <__expected/unexpected.h>
17 #include <__functional/invoke.h>
18 #include <__memory/addressof.h>
19 #include <__memory/construct_at.h>
20 #include <__type_traits/conditional.h>
21 #include <__type_traits/conjunction.h>
22 #include <__type_traits/disjunction.h>
23 #include <__type_traits/integral_constant.h>
24 #include <__type_traits/invoke.h>
25 #include <__type_traits/is_assignable.h>
26 #include <__type_traits/is_constructible.h>
27 #include <__type_traits/is_convertible.h>
28 #include <__type_traits/is_function.h>
29 #include <__type_traits/is_nothrow_assignable.h>
30 #include <__type_traits/is_nothrow_constructible.h>
31 #include <__type_traits/is_reference.h>
32 #include <__type_traits/is_same.h>
33 #include <__type_traits/is_swappable.h>
34 #include <__type_traits/is_trivially_constructible.h>
35 #include <__type_traits/is_trivially_destructible.h>
36 #include <__type_traits/is_trivially_relocatable.h>
37 #include <__type_traits/is_void.h>
38 #include <__type_traits/lazy.h>
39 #include <__type_traits/negation.h>
40 #include <__type_traits/remove_cv.h>
41 #include <__type_traits/remove_cvref.h>
42 #include <__utility/as_const.h>
43 #include <__utility/exception_guard.h>
44 #include <__utility/forward.h>
45 #include <__utility/in_place.h>
46 #include <__utility/move.h>
47 #include <__utility/swap.h>
48 #include <__verbose_abort>
49 #include <initializer_list>
51 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
52 # pragma GCC system_header
56 #include <__undef_macros>
58 #if _LIBCPP_STD_VER >= 23
60 _LIBCPP_BEGIN_NAMESPACE_STD
62 template <class _Tp
, class _Err
>
66 struct __is_std_expected
: false_type
{};
68 template <class _Tp
, class _Err
>
69 struct __is_std_expected
<expected
<_Tp
, _Err
>> : true_type
{};
71 struct __expected_construct_in_place_from_invoke_tag
{};
72 struct __expected_construct_unexpected_from_invoke_tag
{};
74 template <class _Err
, class _Arg
>
75 _LIBCPP_HIDE_FROM_ABI
void __throw_bad_expected_access(_Arg
&& __arg
) {
76 # if _LIBCPP_HAS_EXCEPTIONS
77 throw bad_expected_access
<_Err
>(std::forward
<_Arg
>(__arg
));
80 _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");
84 // If parameter type `_Tp` of `__conditional_no_unique_address` is neither
85 // copyable nor movable, a constructor with this tag is provided. For that
86 // constructor, the user has to provide a function and arguments. The function
87 // must return an object of type `_Tp`. When the function is invoked by the
88 // constructor, guaranteed copy elision kicks in and the `_Tp` is constructed
90 struct __conditional_no_unique_address_invoke_tag
{};
92 // This class implements an object with `[[no_unique_address]]` conditionally applied to it,
93 // based on the value of `_NoUnique`.
95 // A member of this class must always have `[[no_unique_address]]` applied to
96 // it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case
97 // would not have any effect. In the `false` case, the `__v` is not
98 // `[[no_unique_address]]`, so nullifies the effects of the "outer"
99 // `[[no_unique_address]]` regarding data layout.
101 // If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`.
102 template <bool _NoUnique
, class _Tp
>
103 struct __conditional_no_unique_address
;
106 struct __conditional_no_unique_address
<true, _Tp
> {
107 template <class... _Args
>
108 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(in_place_t
, _Args
&&... __args
)
109 : __v(std::forward
<_Args
>(__args
)...) {}
111 template <class _Func
, class... _Args
>
112 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(
113 __conditional_no_unique_address_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
114 : __v(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
116 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v
;
120 struct __conditional_no_unique_address
<false, _Tp
> {
121 template <class... _Args
>
122 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(in_place_t
, _Args
&&... __args
)
123 : __v(std::forward
<_Args
>(__args
)...) {}
125 template <class _Func
, class... _Args
>
126 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(
127 __conditional_no_unique_address_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
128 : __v(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
133 // This function returns whether the type `_Second` can be stuffed into the tail padding
134 // of the `_First` type if both of them are given `[[no_unique_address]]`.
135 template <class _First
, class _Second
>
136 inline constexpr bool __fits_in_tail_padding
= []() {
138 _LIBCPP_NO_UNIQUE_ADDRESS _First __first
;
139 _LIBCPP_NO_UNIQUE_ADDRESS _Second __second
;
141 return sizeof(__x
) == sizeof(_First
);
144 // This class implements the storage used by `std::expected`. We have a few
145 // goals for this storage:
146 // 1. Whenever the underlying {_Tp | _Unex} combination has free bytes in its
147 // tail padding, we should reuse it to store the bool discriminator of the
148 // expected, so as to save space.
149 // 2. Whenever the `expected<_Tp, _Unex>` as a whole has free bytes in its tail
150 // padding, we should allow an object following the expected to be stored in
152 // 3. However, we never want a user object (say `X`) that would follow an
153 // `expected<_Tp, _Unex>` to be stored in the padding bytes of the
154 // underlying {_Tp | _Unex} union, if any. That is because we use
155 // `construct_at` on that union, which would end up overwriting the `X`
156 // member if it is stored in the tail padding of the union.
158 // To achieve this, `__expected_base`'s logic is implemented in an inner
159 // `__repr` class. `__expected_base` holds one `__repr` member which is
160 // conditionally `[[no_unique_address]]`. The `__repr` class holds the
161 // underlying {_Tp | _Unex} union and a boolean "has value" flag.
163 // Which one of the `__repr_`/`__union_` members is `[[no_unique_address]]`
164 // depends on whether the "has value" boolean fits into the tail padding of
165 // the underlying {_Tp | _Unex} union:
167 // - In case the "has value" bool fits into the tail padding of the union, the
168 // whole `__repr_` member is _not_ `[[no_unique_address]]` as it needs to be
169 // transparently replaced on `emplace()`/`swap()` etc.
170 // - In case the "has value" bool does not fit into the tail padding of the
171 // union, only the union member must be transparently replaced (therefore is
172 // _not_ `[[no_unique_address]]`) and the "has value" flag must be adjusted
175 // This way, the member that is transparently replaced on mutating operations
176 // is never `[[no_unique_address]]`, satisfying the requirements from
177 // "[basic.life]" in the standard.
179 // Stripped away of all superfluous elements, the layout of `__expected_base`
180 // then looks like this:
182 // template <class Tp, class Err>
183 // class expected_base {
185 // [[no_unique_address]] Tp val;
186 // [[no_unique_address]] Err unex;
189 // static constexpr bool put_flag_in_tail = fits_in_tail_padding<union_t, bool>;
190 // static constexpr bool allow_reusing_expected_tail_padding = !put_flag_in_tail;
194 // // If "has value" fits into the tail, this should be
195 // // `[[no_unique_address]]`, otherwise not.
196 // [[no_unique_address]] conditional_no_unique_address<
198 // union_t>::type union_;
199 // [[no_unique_address]] bool has_val_;
203 // // If "has value" fits into the tail, this must _not_ be
204 // // `[[no_unique_address]]` so that we fill out the
205 // // complete `expected` object.
206 // [[no_unique_address]] conditional_no_unique_address<
207 // allow_reusing_expected_tail_padding,
208 // repr>::type repr_;
211 template <class _Tp
, class _Err
>
212 class __expected_base
{
213 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
214 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
215 // it's not clear that it's implementable, given that the function is allowed to clobber
216 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
218 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&) = delete;
219 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&)
220 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
221 is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>)
223 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&) = delete;
224 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&)
225 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
226 is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>)
228 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(const __union_t
&) = delete;
229 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(__union_t
&&) = delete;
231 template <class... _Args
>
232 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(in_place_t
, _Args
&&... __args
)
233 : __val_(std::forward
<_Args
>(__args
)...) {}
235 template <class... _Args
>
236 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(unexpect_t
, _Args
&&... __args
)
237 : __unex_(std::forward
<_Args
>(__args
)...) {}
239 template <class _Func
, class... _Args
>
240 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
241 std::__expected_construct_in_place_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
242 : __val_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
244 template <class _Func
, class... _Args
>
245 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
246 std::__expected_construct_unexpected_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
247 : __unex_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
249 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
250 requires(is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>)
253 // __repr's destructor handles this
254 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t() {}
256 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_
;
257 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_
;
260 static constexpr bool __put_flag_in_tail
= __fits_in_tail_padding
<__union_t
, bool>;
261 static constexpr bool __allow_reusing_expected_tail_padding
= !__put_flag_in_tail
;
264 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr() = delete;
266 template <class... _Args
>
267 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(in_place_t __tag
, _Args
&&... __args
)
268 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(true) {}
270 template <class... _Args
>
271 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(unexpect_t __tag
, _Args
&&... __args
)
272 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
274 template <class... _Args
>
275 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag
,
277 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(true) {}
279 template <class... _Args
>
280 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag
,
282 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
284 // The return value of `__make_union` must be constructed in place in the
285 // `__v` member of `__union_`, relying on guaranteed copy elision. To do
286 // this, the `__conditional_no_unique_address_invoke_tag` constructor is
287 // called with a lambda that is immediately called inside
288 // `__conditional_no_unique_address`'s constructor.
289 template <class _OtherUnion
>
290 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(bool __has_val
, _OtherUnion
&& __other
)
291 requires(__allow_reusing_expected_tail_padding
)
292 : __union_(__conditional_no_unique_address_invoke_tag
{},
293 [&] { return __make_union(__has_val
, std::forward
<_OtherUnion
>(__other
)); }),
294 __has_val_(__has_val
) {}
296 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&) = delete;
297 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&)
298 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
299 is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>)
301 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&) = delete;
302 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&)
303 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
304 is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>)
307 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(const __repr
&) = delete;
308 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(__repr
&&) = delete;
310 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
311 requires(is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>)
314 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
315 requires(!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>)
317 __destroy_union_member();
320 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
321 requires(__allow_reusing_expected_tail_padding
&&
322 (is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>))
324 // Note: Since the destructor of the union is trivial, this does nothing
325 // except to end the lifetime of the union.
326 std::destroy_at(&__union_
.__v
);
329 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
330 requires(__allow_reusing_expected_tail_padding
&&
331 (!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>))
333 __destroy_union_member();
334 std::destroy_at(&__union_
.__v
);
337 template <class... _Args
>
338 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(in_place_t
, _Args
&&... __args
)
339 requires(__allow_reusing_expected_tail_padding
)
341 std::construct_at(&__union_
.__v
, in_place
, std::forward
<_Args
>(__args
)...);
345 template <class... _Args
>
346 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(unexpect_t
, _Args
&&... __args
)
347 requires(__allow_reusing_expected_tail_padding
)
349 std::construct_at(&__union_
.__v
, unexpect
, std::forward
<_Args
>(__args
)...);
354 template <class, class>
355 friend class __expected_base
;
357 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union_member()
358 requires(!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>)
361 std::destroy_at(std::addressof(__union_
.__v
.__val_
));
363 std::destroy_at(std::addressof(__union_
.__v
.__unex_
));
367 template <class _OtherUnion
>
368 _LIBCPP_HIDE_FROM_ABI
static constexpr __union_t
__make_union(bool __has_val
, _OtherUnion
&& __other
)
369 requires(__allow_reusing_expected_tail_padding
)
372 return __union_t(in_place
, std::forward
<_OtherUnion
>(__other
).__val_
);
374 return __union_t(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
377 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__put_flag_in_tail
, __union_t
> __union_
;
378 _LIBCPP_NO_UNIQUE_ADDRESS
bool __has_val_
;
381 template <class _OtherUnion
>
382 _LIBCPP_HIDE_FROM_ABI
static constexpr __repr
__make_repr(bool __has_val
, _OtherUnion
&& __other
)
383 requires(__put_flag_in_tail
)
386 return __repr(in_place
, std::forward
<_OtherUnion
>(__other
).__val_
);
388 return __repr(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
392 template <class... _Args
>
393 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_base(_Args
&&... __args
)
394 : __repr_(in_place
, std::forward
<_Args
>(__args
)...) {}
396 // In case we copy/move construct from another `expected` we need to create
397 // our `expected` so that it either has a value or not, depending on the "has
398 // value" flag of the other `expected`. To do this without falling back on
399 // `std::construct_at` we rely on guaranteed copy elision using two helper
400 // functions `__make_repr` and `__make_union`. There have to be two since
401 // there are two data layouts with different members being
402 // `[[no_unique_address]]`. GCC (as of version 13) does not do guaranteed
403 // copy elision when initializing `[[no_unique_address]]` members. The two
406 // - `__make_repr`: This is used when the "has value" flag lives in the tail
407 // of the union. In this case, the `__repr` member is _not_
408 // `[[no_unique_address]]`.
409 // - `__make_union`: When the "has value" flag does _not_ fit in the tail of
410 // the union, the `__repr` member is `[[no_unique_address]]` and the union
413 // This constructor "catches" the first case and leaves the second case to
414 // `__union_t`, its constructors and `__make_union`.
415 template <class _OtherUnion
>
416 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_base(bool __has_val
, _OtherUnion
&& __other
)
417 requires(__put_flag_in_tail
)
418 : __repr_(__conditional_no_unique_address_invoke_tag
{},
419 [&] { return __make_repr(__has_val
, std::forward
<_OtherUnion
>(__other
)); }) {}
421 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy() {
422 if constexpr (__put_flag_in_tail
)
423 std::destroy_at(&__repr_
.__v
);
425 __repr_
.__v
.__destroy_union();
428 template <class _Tag
, class... _Args
>
429 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct(_Tag __tag
, _Args
&&... __args
) {
430 if constexpr (__put_flag_in_tail
)
431 std::construct_at(&__repr_
.__v
, __tag
, std::forward
<_Args
>(__args
)...);
433 __repr_
.__v
.__construct_union(__tag
, std::forward
<_Args
>(__args
)...);
436 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_val() const { return __repr_
.__v
.__has_val_
; }
437 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& __union() { return __repr_
.__v
.__union_
.__v
; }
438 _LIBCPP_HIDE_FROM_ABI
constexpr const __union_t
& __union() const { return __repr_
.__v
.__union_
.__v
; }
439 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& __val() { return __repr_
.__v
.__union_
.__v
.__val_
; }
440 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& __val() const { return __repr_
.__v
.__union_
.__v
.__val_
; }
441 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& __unex() { return __repr_
.__v
.__union_
.__v
.__unex_
; }
442 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& __unex() const { return __repr_
.__v
.__union_
.__v
.__unex_
; }
445 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__allow_reusing_expected_tail_padding
, __repr
> __repr_
;
448 template <class _Tp
, class _Err
>
449 class expected
: private __expected_base
<_Tp
, _Err
> {
450 static_assert(!is_reference_v
<_Tp
> && !is_function_v
<_Tp
> && !is_same_v
<remove_cv_t
<_Tp
>, in_place_t
> &&
451 !is_same_v
<remove_cv_t
<_Tp
>, unexpect_t
> && !__is_std_unexpected
<remove_cv_t
<_Tp
>>::value
&&
452 __valid_std_unexpected
<_Err
>::value
,
453 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
454 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
455 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
456 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
457 "template argument for unexpected is ill-formed.");
459 template <class _Up
, class _OtherErr
>
460 friend class expected
;
462 using __base
= __expected_base
<_Tp
, _Err
>;
465 using value_type
= _Tp
;
466 using error_type
= _Err
;
467 using unexpected_type
= unexpected
<_Err
>;
469 using __trivially_relocatable
=
470 __conditional_t
<__libcpp_is_trivially_relocatable
<_Tp
>::value
&& __libcpp_is_trivially_relocatable
<_Err
>::value
,
475 using rebind
= expected
<_Up
, error_type
>;
477 // [expected.object.ctor], constructors
478 _LIBCPP_HIDE_FROM_ABI
constexpr expected() noexcept(is_nothrow_default_constructible_v
<_Tp
>) // strengthened
479 requires is_default_constructible_v
<_Tp
>
480 : __base(in_place
) {}
482 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&) = delete;
484 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&)
485 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Tp
> &&
486 is_trivially_copy_constructible_v
<_Err
>)
489 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
& __other
) noexcept(
490 is_nothrow_copy_constructible_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Err
>) // strengthened
491 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
492 !(is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>))
493 : __base(__other
.__has_val(), __other
.__union()) {}
495 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&&)
496 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Tp
> &&
497 is_trivially_move_constructible_v
<_Err
>)
500 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&& __other
) noexcept(
501 is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_move_constructible_v
<_Err
>)
502 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
503 !(is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>))
504 : __base(__other
.__has_val(), std::move(__other
.__union())) {}
507 template <class _Up
, class _OtherErr
, class _UfQual
, class _OtherErrQual
>
508 using __can_convert
= _And
<
509 is_constructible
<_Tp
, _UfQual
>,
510 is_constructible
<_Err
, _OtherErrQual
>,
511 _If
<_Not
<is_same
<remove_cv_t
<_Tp
>, bool>>::value
,
512 _And
< _Not
<_And
<is_same
<_Tp
, _Up
>, is_same
<_Err
, _OtherErr
>>>, // use the copy constructor instead, see #92676
513 _Not
<is_constructible
<_Tp
, expected
<_Up
, _OtherErr
>&>>,
514 _Not
<is_constructible
<_Tp
, expected
<_Up
, _OtherErr
>>>,
515 _Not
<is_constructible
<_Tp
, const expected
<_Up
, _OtherErr
>&>>,
516 _Not
<is_constructible
<_Tp
, const expected
<_Up
, _OtherErr
>>>,
517 _Not
<is_convertible
<expected
<_Up
, _OtherErr
>&, _Tp
>>,
518 _Not
<is_convertible
<expected
<_Up
, _OtherErr
>&&, _Tp
>>,
519 _Not
<is_convertible
<const expected
<_Up
, _OtherErr
>&, _Tp
>>,
520 _Not
<is_convertible
<const expected
<_Up
, _OtherErr
>&&, _Tp
>>>,
522 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>&>>,
523 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>>>,
524 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>&>>,
525 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>>> >;
527 template <class _Func
, class... _Args
>
528 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
529 std::__expected_construct_in_place_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
530 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
532 template <class _Func
, class... _Args
>
533 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
534 std::__expected_construct_unexpected_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
535 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
538 template <class _Up
, class _OtherErr
>
539 requires __can_convert
<_Up
, _OtherErr
, const _Up
&, const _OtherErr
&>::value
540 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _Up
&, _Tp
> ||
541 !is_convertible_v
<const _OtherErr
&, _Err
>)
542 expected(const expected
<_Up
, _OtherErr
>& __other
) noexcept(
543 is_nothrow_constructible_v
<_Tp
, const _Up
&> &&
544 is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
545 : __base(__other
.__has_val(), __other
.__union()) {}
547 template <class _Up
, class _OtherErr
>
548 requires __can_convert
<_Up
, _OtherErr
, _Up
, _OtherErr
>::value
549 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_Up
, _Tp
> || !is_convertible_v
<_OtherErr
, _Err
>)
550 expected(expected
<_Up
, _OtherErr
>&& __other
) noexcept(
551 is_nothrow_constructible_v
<_Tp
, _Up
> && is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
552 : __base(__other
.__has_val(), std::move(__other
.__union())) {}
554 template <class _Up
= _Tp
>
555 requires(!is_same_v
<remove_cvref_t
<_Up
>, in_place_t
> && !is_same_v
<expected
, remove_cvref_t
<_Up
>> &&
556 is_constructible_v
<_Tp
, _Up
> && !__is_std_unexpected
<remove_cvref_t
<_Up
>>::value
&&
557 (!is_same_v
<remove_cv_t
<_Tp
>, bool> || !__is_std_expected
<remove_cvref_t
<_Up
>>::value
))
558 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_Up
, _Tp
>)
559 expected(_Up
&& __u
) noexcept(is_nothrow_constructible_v
<_Tp
, _Up
>) // strengthened
560 : __base(in_place
, std::forward
<_Up
>(__u
)) {}
562 template <class _OtherErr
>
563 requires is_constructible_v
<_Err
, const _OtherErr
&>
564 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>) expected(
565 const unexpected
<_OtherErr
>& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
566 : __base(unexpect
, __unex
.error()) {}
568 template <class _OtherErr
>
569 requires is_constructible_v
<_Err
, _OtherErr
>
570 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
571 expected(unexpected
<_OtherErr
>&& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
572 : __base(unexpect
, std::move(__unex
.error())) {}
574 template <class... _Args
>
575 requires is_constructible_v
<_Tp
, _Args
...>
576 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
, _Args
&&... __args
) noexcept(
577 is_nothrow_constructible_v
<_Tp
, _Args
...>) // strengthened
578 : __base(in_place
, std::forward
<_Args
>(__args
)...) {}
580 template <class _Up
, class... _Args
>
581 requires is_constructible_v
< _Tp
, initializer_list
<_Up
>&, _Args
... >
582 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
583 is_nothrow_constructible_v
<_Tp
, initializer_list
<_Up
>&, _Args
...>) // strengthened
584 : __base(in_place
, __il
, std::forward
<_Args
>(__args
)...) {}
586 template <class... _Args
>
587 requires is_constructible_v
<_Err
, _Args
...>
588 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, _Args
&&... __args
) noexcept(
589 is_nothrow_constructible_v
<_Err
, _Args
...>) // strengthened
590 : __base(unexpect
, std::forward
<_Args
>(__args
)...) {}
592 template <class _Up
, class... _Args
>
593 requires is_constructible_v
< _Err
, initializer_list
<_Up
>&, _Args
... >
594 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
595 is_nothrow_constructible_v
<_Err
, initializer_list
<_Up
>&, _Args
...>) // strengthened
596 : __base(unexpect
, __il
, std::forward
<_Args
>(__args
)...) {}
598 // [expected.object.dtor], destructor
600 _LIBCPP_HIDE_FROM_ABI
constexpr ~expected() = default;
603 template <class _Tag
, class _OtherTag
, class _T1
, class _T2
, class... _Args
>
604 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(_T2
& __oldval
, _Args
&&... __args
) {
605 if constexpr (is_nothrow_constructible_v
<_T1
, _Args
...>) {
607 this->__construct(_Tag
{}, std::forward
<_Args
>(__args
)...);
608 } else if constexpr (is_nothrow_move_constructible_v
<_T1
>) {
609 _T1
__tmp(std::forward
<_Args
>(__args
)...);
611 this->__construct(_Tag
{}, std::move(__tmp
));
614 is_nothrow_move_constructible_v
<_T2
>,
615 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
616 "be reverted to the previous state in case an exception is thrown during the assignment.");
617 _T2
__tmp(std::move(__oldval
));
619 auto __trans
= std::__make_exception_guard([&] { this->__construct(_OtherTag
{}, std::move(__tmp
)); });
620 this->__construct(_Tag
{}, std::forward
<_Args
>(__args
)...);
621 __trans
.__complete();
626 // [expected.object.assign], assignment
627 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
&) = delete;
629 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
& __rhs
) noexcept(
630 is_nothrow_copy_assignable_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Tp
> && is_nothrow_copy_assignable_v
<_Err
> &&
631 is_nothrow_copy_constructible_v
<_Err
>) // strengthened
632 requires(is_copy_assignable_v
<_Tp
> && is_copy_constructible_v
<_Tp
> && is_copy_assignable_v
<_Err
> &&
633 is_copy_constructible_v
<_Err
> &&
634 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
636 if (this->__has_val() && __rhs
.__has_val()) {
637 this->__val() = __rhs
.__val();
638 } else if (this->__has_val()) {
639 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), __rhs
.__unex());
640 } else if (__rhs
.__has_val()) {
641 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), __rhs
.__val());
643 this->__unex() = __rhs
.__unex();
648 _LIBCPP_HIDE_FROM_ABI
constexpr expected
&
649 operator=(expected
&& __rhs
) noexcept(is_nothrow_move_assignable_v
<_Tp
> && is_nothrow_move_constructible_v
<_Tp
> &&
650 is_nothrow_move_assignable_v
<_Err
> && is_nothrow_move_constructible_v
<_Err
>)
651 requires(is_move_constructible_v
<_Tp
> && is_move_assignable_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
652 is_move_assignable_v
<_Err
> &&
653 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
655 if (this->__has_val() && __rhs
.__has_val()) {
656 this->__val() = std::move(__rhs
.__val());
657 } else if (this->__has_val()) {
658 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), std::move(__rhs
.__unex()));
659 } else if (__rhs
.__has_val()) {
660 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), std::move(__rhs
.__val()));
662 this->__unex() = std::move(__rhs
.__unex());
667 template <class _Up
= _Tp
>
668 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(_Up
&& __v
)
669 requires(!is_same_v
<expected
, remove_cvref_t
<_Up
>> && !__is_std_unexpected
<remove_cvref_t
<_Up
>>::value
&&
670 is_constructible_v
<_Tp
, _Up
> && is_assignable_v
<_Tp
&, _Up
> &&
671 (is_nothrow_constructible_v
<_Tp
, _Up
> || is_nothrow_move_constructible_v
<_Tp
> ||
672 is_nothrow_move_constructible_v
<_Err
>))
674 if (this->__has_val()) {
675 this->__val() = std::forward
<_Up
>(__v
);
677 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), std::forward
<_Up
>(__v
));
683 template <class _OtherErrQual
>
684 static constexpr bool __can_assign_from_unexpected
=
685 _And
< is_constructible
<_Err
, _OtherErrQual
>,
686 is_assignable
<_Err
&, _OtherErrQual
>,
688 is_nothrow_constructible
<_Err
, _OtherErrQual
>,
689 is_nothrow_move_constructible
<_Tp
>,
690 is_nothrow_move_constructible
<_Err
>> >::value
;
693 template <class _OtherErr
>
694 requires(__can_assign_from_unexpected
<const _OtherErr
&>)
695 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const unexpected
<_OtherErr
>& __un
) {
696 if (this->__has_val()) {
697 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), __un
.error());
699 this->__unex() = __un
.error();
704 template <class _OtherErr
>
705 requires(__can_assign_from_unexpected
<_OtherErr
>)
706 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(unexpected
<_OtherErr
>&& __un
) {
707 if (this->__has_val()) {
708 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), std::move(__un
.error()));
710 this->__unex() = std::move(__un
.error());
715 template <class... _Args
>
716 requires is_nothrow_constructible_v
<_Tp
, _Args
...>
717 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& emplace(_Args
&&... __args
) noexcept
{
719 this->__construct(in_place
, std::forward
<_Args
>(__args
)...);
720 return this->__val();
723 template <class _Up
, class... _Args
>
724 requires is_nothrow_constructible_v
<_Tp
, initializer_list
<_Up
>&, _Args
...>
725 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& emplace(initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept
{
727 this->__construct(in_place
, __il
, std::forward
<_Args
>(__args
)...);
728 return this->__val();
732 // [expected.object.swap], swap
733 _LIBCPP_HIDE_FROM_ABI
constexpr void
734 swap(expected
& __rhs
) noexcept(is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_swappable_v
<_Tp
> &&
735 is_nothrow_move_constructible_v
<_Err
> && is_nothrow_swappable_v
<_Err
>)
736 requires(is_swappable_v
<_Tp
> && is_swappable_v
<_Err
> && is_move_constructible_v
<_Tp
> &&
737 is_move_constructible_v
<_Err
> &&
738 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
740 auto __swap_val_unex_impl
= [](expected
& __with_val
, expected
& __with_err
) {
741 if constexpr (is_nothrow_move_constructible_v
<_Err
>) {
742 _Err
__tmp(std::move(__with_err
.__unex()));
743 __with_err
.__destroy();
744 auto __trans
= std::__make_exception_guard([&] { __with_err
.__construct(unexpect
, std::move(__tmp
)); });
745 __with_err
.__construct(in_place
, std::move(__with_val
.__val()));
746 __trans
.__complete();
747 __with_val
.__destroy();
748 __with_val
.__construct(unexpect
, std::move(__tmp
));
750 static_assert(is_nothrow_move_constructible_v
<_Tp
>,
751 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
752 "that it can be reverted to the previous state in case an exception is thrown during swap.");
753 _Tp
__tmp(std::move(__with_val
.__val()));
754 __with_val
.__destroy();
755 auto __trans
= std::__make_exception_guard([&] { __with_val
.__construct(in_place
, std::move(__tmp
)); });
756 __with_val
.__construct(unexpect
, std::move(__with_err
.__unex()));
757 __trans
.__complete();
758 __with_err
.__destroy();
759 __with_err
.__construct(in_place
, std::move(__tmp
));
763 if (this->__has_val()) {
764 if (__rhs
.__has_val()) {
766 swap(this->__val(), __rhs
.__val());
768 __swap_val_unex_impl(*this, __rhs
);
771 if (__rhs
.__has_val()) {
772 __swap_val_unex_impl(__rhs
, *this);
775 swap(this->__unex(), __rhs
.__unex());
780 _LIBCPP_HIDE_FROM_ABI
friend constexpr void swap(expected
& __x
, expected
& __y
) noexcept(noexcept(__x
.swap(__y
)))
781 requires requires
{ __x
.swap(__y
); }
786 // [expected.object.obs], observers
787 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{
788 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
789 this->__has_val(), "expected::operator-> requires the expected to contain a value");
790 return std::addressof(this->__val());
793 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{
794 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
795 this->__has_val(), "expected::operator-> requires the expected to contain a value");
796 return std::addressof(this->__val());
799 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& operator*() const& noexcept
{
800 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
801 this->__has_val(), "expected::operator* requires the expected to contain a value");
802 return this->__val();
805 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() & noexcept
{
806 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
807 this->__has_val(), "expected::operator* requires the expected to contain a value");
808 return this->__val();
811 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
&& operator*() const&& noexcept
{
812 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
813 this->__has_val(), "expected::operator* requires the expected to contain a value");
814 return std::move(this->__val());
817 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
&& operator*() && noexcept
{
818 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
819 this->__has_val(), "expected::operator* requires the expected to contain a value");
820 return std::move(this->__val());
823 _LIBCPP_HIDE_FROM_ABI
constexpr explicit operator bool() const noexcept
{ return this->__has_val(); }
825 _LIBCPP_HIDE_FROM_ABI
constexpr bool has_value() const noexcept
{ return this->__has_val(); }
827 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& value() const& {
828 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
829 if (!this->__has_val()) {
830 std::__throw_bad_expected_access
<_Err
>(std::as_const(error()));
832 return this->__val();
835 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& value() & {
836 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
837 if (!this->__has_val()) {
838 std::__throw_bad_expected_access
<_Err
>(std::as_const(error()));
840 return this->__val();
843 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
&& value() const&& {
844 static_assert(is_copy_constructible_v
<_Err
> && is_constructible_v
<_Err
, decltype(std::move(error()))>,
845 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
846 if (!this->__has_val()) {
847 std::__throw_bad_expected_access
<_Err
>(std::move(error()));
849 return std::move(this->__val());
852 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
&& value() && {
853 static_assert(is_copy_constructible_v
<_Err
> && is_constructible_v
<_Err
, decltype(std::move(error()))>,
854 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
855 if (!this->__has_val()) {
856 std::__throw_bad_expected_access
<_Err
>(std::move(error()));
858 return std::move(this->__val());
861 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& error() const& noexcept
{
862 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
863 !this->__has_val(), "expected::error requires the expected to contain an error");
864 return this->__unex();
867 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& error() & noexcept
{
868 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
869 !this->__has_val(), "expected::error requires the expected to contain an error");
870 return this->__unex();
873 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
&& error() const&& noexcept
{
874 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
875 !this->__has_val(), "expected::error requires the expected to contain an error");
876 return std::move(this->__unex());
879 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
&& error() && noexcept
{
880 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
881 !this->__has_val(), "expected::error requires the expected to contain an error");
882 return std::move(this->__unex());
886 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
value_or(_Up
&& __v
) const& {
887 static_assert(is_copy_constructible_v
<_Tp
>, "value_type has to be copy constructible");
888 static_assert(is_convertible_v
<_Up
, _Tp
>, "argument has to be convertible to value_type");
889 return this->__has_val() ? this->__val() : static_cast<_Tp
>(std::forward
<_Up
>(__v
));
893 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
value_or(_Up
&& __v
) && {
894 static_assert(is_move_constructible_v
<_Tp
>, "value_type has to be move constructible");
895 static_assert(is_convertible_v
<_Up
, _Tp
>, "argument has to be convertible to value_type");
896 return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp
>(std::forward
<_Up
>(__v
));
899 template <class _Up
= _Err
>
900 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) const& {
901 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
902 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
904 return std::forward
<_Up
>(__error
);
908 template <class _Up
= _Err
>
909 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) && {
910 static_assert(is_move_constructible_v
<_Err
>, "error_type has to be move constructible");
911 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
913 return std::forward
<_Up
>(__error
);
914 return std::move(error());
917 // [expected.void.monadic], monadic
918 template <class _Func
>
919 requires is_constructible_v
<_Err
, _Err
&>
920 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) & {
921 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, _Tp
&>>;
922 static_assert(__is_std_expected
<_Up
>::value
, "The result of f(value()) must be a specialization of std::expected");
923 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
924 "The result of f(value()) must have the same error_type as this expected");
926 return std::invoke(std::forward
<_Func
>(__f
), this->__val());
928 return _Up(unexpect
, error());
931 template <class _Func
>
932 requires is_constructible_v
<_Err
, const _Err
&>
933 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const& {
934 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, const _Tp
&>>;
935 static_assert(__is_std_expected
<_Up
>::value
, "The result of f(value()) must be a specialization of std::expected");
936 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
937 "The result of f(value()) must have the same error_type as this expected");
939 return std::invoke(std::forward
<_Func
>(__f
), this->__val());
941 return _Up(unexpect
, error());
944 template <class _Func
>
945 requires is_constructible_v
<_Err
, _Err
&&>
946 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) && {
947 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, _Tp
&&>>;
949 __is_std_expected
<_Up
>::value
, "The result of f(std::move(value())) must be a specialization of std::expected");
950 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
951 "The result of f(std::move(value())) must have the same error_type as this expected");
953 return std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
955 return _Up(unexpect
, std::move(error()));
958 template <class _Func
>
959 requires is_constructible_v
<_Err
, const _Err
&&>
960 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const&& {
961 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, const _Tp
&&>>;
963 __is_std_expected
<_Up
>::value
, "The result of f(std::move(value())) must be a specialization of std::expected");
964 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
965 "The result of f(std::move(value())) must have the same error_type as this expected");
967 return std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
969 return _Up(unexpect
, std::move(error()));
972 template <class _Func
>
973 requires is_constructible_v
<_Tp
, _Tp
&>
974 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) & {
975 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&>>;
976 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
977 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
978 "The result of f(error()) must have the same value_type as this expected");
980 return _Gp(in_place
, this->__val());
982 return std::invoke(std::forward
<_Func
>(__f
), error());
985 template <class _Func
>
986 requires is_constructible_v
<_Tp
, const _Tp
&>
987 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const& {
988 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&>>;
989 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
990 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
991 "The result of f(error()) must have the same value_type as this expected");
993 return _Gp(in_place
, this->__val());
995 return std::invoke(std::forward
<_Func
>(__f
), error());
998 template <class _Func
>
999 requires is_constructible_v
<_Tp
, _Tp
&&>
1000 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) && {
1001 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&&>>;
1003 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1004 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1005 "The result of f(std::move(error())) must have the same value_type as this expected");
1007 return _Gp(in_place
, std::move(this->__val()));
1009 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1012 template <class _Func
>
1013 requires is_constructible_v
<_Tp
, const _Tp
&&>
1014 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const&& {
1015 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1017 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1018 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1019 "The result of f(std::move(error())) must have the same value_type as this expected");
1021 return _Gp(in_place
, std::move(this->__val()));
1023 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1026 template <class _Func
>
1027 requires is_constructible_v
<_Err
, _Err
&>
1028 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) & {
1029 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, _Tp
&>>;
1031 return expected
<_Up
, _Err
>(unexpect
, error());
1033 if constexpr (!is_void_v
<_Up
>) {
1034 return expected
<_Up
, _Err
>(
1035 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), this->__val());
1037 std::invoke(std::forward
<_Func
>(__f
), this->__val());
1038 return expected
<_Up
, _Err
>();
1042 template <class _Func
>
1043 requires is_constructible_v
<_Err
, const _Err
&>
1044 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const& {
1045 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, const _Tp
&>>;
1047 return expected
<_Up
, _Err
>(unexpect
, error());
1049 if constexpr (!is_void_v
<_Up
>) {
1050 return expected
<_Up
, _Err
>(
1051 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), this->__val());
1053 std::invoke(std::forward
<_Func
>(__f
), this->__val());
1054 return expected
<_Up
, _Err
>();
1058 template <class _Func
>
1059 requires is_constructible_v
<_Err
, _Err
&&>
1060 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) && {
1061 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, _Tp
&&>>;
1063 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1065 if constexpr (!is_void_v
<_Up
>) {
1066 return expected
<_Up
, _Err
>(
1067 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(this->__val()));
1069 std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
1070 return expected
<_Up
, _Err
>();
1074 template <class _Func
>
1075 requires is_constructible_v
<_Err
, const _Err
&&>
1076 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const&& {
1077 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, const _Tp
&&>>;
1079 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1081 if constexpr (!is_void_v
<_Up
>) {
1082 return expected
<_Up
, _Err
>(
1083 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(this->__val()));
1085 std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
1086 return expected
<_Up
, _Err
>();
1090 template <class _Func
>
1091 requires is_constructible_v
<_Tp
, _Tp
&>
1092 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) & {
1093 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&>>;
1094 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1095 "The result of f(error()) must be a valid template argument for unexpected");
1097 return expected
<_Tp
, _Gp
>(in_place
, this->__val());
1099 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1102 template <class _Func
>
1103 requires is_constructible_v
<_Tp
, const _Tp
&>
1104 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const& {
1105 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&>>;
1106 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1107 "The result of f(error()) must be a valid template argument for unexpected");
1109 return expected
<_Tp
, _Gp
>(in_place
, this->__val());
1111 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1114 template <class _Func
>
1115 requires is_constructible_v
<_Tp
, _Tp
&&>
1116 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) && {
1117 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&&>>;
1118 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1119 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1121 return expected
<_Tp
, _Gp
>(in_place
, std::move(this->__val()));
1123 return expected
<_Tp
, _Gp
>(
1124 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1127 template <class _Func
>
1128 requires is_constructible_v
<_Tp
, const _Tp
&&>
1129 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const&& {
1130 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1131 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1132 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1134 return expected
<_Tp
, _Gp
>(in_place
, std::move(this->__val()));
1136 return expected
<_Tp
, _Gp
>(
1137 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1140 // [expected.object.eq], equality operators
1141 template <class _T2
, class _E2
>
1142 requires(!is_void_v
<_T2
>)
1143 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const expected
<_T2
, _E2
>& __y
) {
1144 if (__x
.__has_val() != __y
.__has_val()) {
1147 if (__x
.__has_val()) {
1148 return __x
.__val() == __y
.__val();
1150 return __x
.__unex() == __y
.__unex();
1155 template <class _T2
>
1156 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const _T2
& __v
) {
1157 return __x
.__has_val() && static_cast<bool>(__x
.__val() == __v
);
1160 template <class _E2
>
1161 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const unexpected
<_E2
>& __e
) {
1162 return !__x
.__has_val() && static_cast<bool>(__x
.__unex() == __e
.error());
1166 template <class _Err
>
1167 class __expected_void_base
{
1168 struct __empty_t
{};
1169 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
1170 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
1171 // it's not clear that it's implementable, given that the function is allowed to clobber
1172 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
1174 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&) = delete;
1175 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&)
1176 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1178 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&) = delete;
1179 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&)
1180 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1182 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(const __union_t
&) = delete;
1183 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(__union_t
&&) = delete;
1185 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(in_place_t
) : __empty_() {}
1187 template <class... _Args
>
1188 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(unexpect_t
, _Args
&&... __args
)
1189 : __unex_(std::forward
<_Args
>(__args
)...) {}
1191 template <class _Func
, class... _Args
>
1192 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
1193 __expected_construct_unexpected_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
1194 : __unex_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
1196 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
1197 requires(is_trivially_destructible_v
<_Err
>)
1200 // __repr's destructor handles this
1201 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
1202 requires(!is_trivially_destructible_v
<_Err
>)
1205 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_
;
1206 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_
;
1209 static constexpr bool __put_flag_in_tail
= __fits_in_tail_padding
<__union_t
, bool>;
1210 static constexpr bool __allow_reusing_expected_tail_padding
= !__put_flag_in_tail
;
1213 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr() = delete;
1215 template <class... _Args
>
1216 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(in_place_t __tag
) : __union_(in_place
, __tag
), __has_val_(true) {}
1218 template <class... _Args
>
1219 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(unexpect_t __tag
, _Args
&&... __args
)
1220 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
1222 template <class... _Args
>
1223 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag
,
1225 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
1227 template <class _OtherUnion
>
1228 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(bool __has_val
, _OtherUnion
&& __other
)
1229 requires(__allow_reusing_expected_tail_padding
)
1230 : __union_(__conditional_no_unique_address_invoke_tag
{},
1231 [&] { return __make_union(__has_val
, std::forward
<_OtherUnion
>(__other
)); }),
1232 __has_val_(__has_val
) {}
1234 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&) = delete;
1235 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&)
1236 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1238 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&) = delete;
1239 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&)
1240 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1243 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(const __repr
&) = delete;
1244 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(__repr
&&) = delete;
1246 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
1247 requires(is_trivially_destructible_v
<_Err
>)
1250 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
1251 requires(!is_trivially_destructible_v
<_Err
>)
1253 __destroy_union_member();
1256 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
1257 requires(__allow_reusing_expected_tail_padding
&& is_trivially_destructible_v
<_Err
>)
1259 std::destroy_at(&__union_
.__v
);
1262 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
1263 requires(__allow_reusing_expected_tail_padding
&& !is_trivially_destructible_v
<_Err
>)
1265 __destroy_union_member();
1266 std::destroy_at(&__union_
.__v
);
1269 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(in_place_t
)
1270 requires(__allow_reusing_expected_tail_padding
)
1272 std::construct_at(&__union_
.__v
, in_place
);
1276 template <class... _Args
>
1277 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(unexpect_t
, _Args
&&... __args
)
1278 requires(__allow_reusing_expected_tail_padding
)
1280 std::construct_at(&__union_
.__v
, unexpect
, std::forward
<_Args
>(__args
)...);
1286 friend class __expected_void_base
;
1288 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union_member()
1289 requires(!is_trivially_destructible_v
<_Err
>)
1292 std::destroy_at(std::addressof(__union_
.__v
.__unex_
));
1295 template <class _OtherUnion
>
1296 _LIBCPP_HIDE_FROM_ABI
static constexpr __union_t
__make_union(bool __has_val
, _OtherUnion
&& __other
)
1297 requires(__allow_reusing_expected_tail_padding
)
1300 return __union_t(in_place
);
1302 return __union_t(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
1305 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__put_flag_in_tail
, __union_t
> __union_
;
1306 _LIBCPP_NO_UNIQUE_ADDRESS
bool __has_val_
;
1309 template <class _OtherUnion
>
1310 _LIBCPP_HIDE_FROM_ABI
static constexpr __repr
__make_repr(bool __has_val
, _OtherUnion
&& __other
)
1311 requires(__put_flag_in_tail
)
1314 return __repr(in_place
);
1316 return __repr(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
1320 template <class... _Args
>
1321 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_void_base(_Args
&&... __args
)
1322 : __repr_(in_place
, std::forward
<_Args
>(__args
)...) {}
1324 template <class _OtherUnion
>
1325 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_void_base(bool __has_val
, _OtherUnion
&& __other
)
1326 requires(__put_flag_in_tail
)
1327 : __repr_(__conditional_no_unique_address_invoke_tag
{},
1328 [&] { return __make_repr(__has_val
, std::forward
<_OtherUnion
>(__other
)); }) {}
1330 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy() {
1331 if constexpr (__put_flag_in_tail
)
1332 std::destroy_at(&__repr_
.__v
);
1334 __repr_
.__v
.__destroy_union();
1337 template <class _Tag
, class... _Args
>
1338 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct(_Tag __tag
, _Args
&&... __args
) {
1339 if constexpr (__put_flag_in_tail
)
1340 std::construct_at(&__repr_
.__v
, __tag
, std::forward
<_Args
>(__args
)...);
1342 __repr_
.__v
.__construct_union(__tag
, std::forward
<_Args
>(__args
)...);
1345 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_val() const { return __repr_
.__v
.__has_val_
; }
1346 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& __union() { return __repr_
.__v
.__union_
.__v
; }
1347 _LIBCPP_HIDE_FROM_ABI
constexpr const __union_t
& __union() const { return __repr_
.__v
.__union_
.__v
; }
1348 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& __unex() { return __repr_
.__v
.__union_
.__v
.__unex_
; }
1349 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& __unex() const { return __repr_
.__v
.__union_
.__v
.__unex_
; }
1352 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__allow_reusing_expected_tail_padding
, __repr
> __repr_
;
1355 template <class _Tp
, class _Err
>
1356 requires is_void_v
<_Tp
>
1357 class expected
<_Tp
, _Err
> : private __expected_void_base
<_Err
> {
1358 static_assert(__valid_std_unexpected
<_Err
>::value
,
1359 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "
1360 "valid argument for unexpected<E> is ill-formed");
1362 template <class, class>
1363 friend class expected
;
1365 template <class _Up
, class _OtherErr
, class _OtherErrQual
>
1366 using __can_convert
=
1368 is_constructible
<_Err
, _OtherErrQual
>,
1369 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>&>>,
1370 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>>>,
1371 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>&>>,
1372 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>>>>;
1374 using __base
= __expected_void_base
<_Err
>;
1377 using value_type
= _Tp
;
1378 using error_type
= _Err
;
1379 using unexpected_type
= unexpected
<_Err
>;
1381 template <class _Up
>
1382 using rebind
= expected
<_Up
, error_type
>;
1384 // [expected.void.ctor], constructors
1385 _LIBCPP_HIDE_FROM_ABI
constexpr expected() noexcept
: __base(in_place
) {}
1387 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&) = delete;
1389 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&)
1390 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1393 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
& __rhs
) noexcept(
1394 is_nothrow_copy_constructible_v
<_Err
>) // strengthened
1395 requires(is_copy_constructible_v
<_Err
> && !is_trivially_copy_constructible_v
<_Err
>)
1396 : __base(__rhs
.__has_val(), __rhs
.__union()) {}
1398 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&&)
1399 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1402 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&& __rhs
) noexcept(is_nothrow_move_constructible_v
<_Err
>)
1403 requires(is_move_constructible_v
<_Err
> && !is_trivially_move_constructible_v
<_Err
>)
1404 : __base(__rhs
.__has_val(), std::move(__rhs
.__union())) {}
1406 template <class _Up
, class _OtherErr
>
1407 requires __can_convert
<_Up
, _OtherErr
, const _OtherErr
&>::value
1408 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>)
1409 expected(const expected
<_Up
, _OtherErr
>& __rhs
) noexcept(
1410 is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
1411 : __base(__rhs
.__has_val(), __rhs
.__union()) {}
1413 template <class _Up
, class _OtherErr
>
1414 requires __can_convert
<_Up
, _OtherErr
, _OtherErr
>::value
1415 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
1416 expected(expected
<_Up
, _OtherErr
>&& __rhs
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
1417 : __base(__rhs
.__has_val(), std::move(__rhs
.__union())) {}
1419 template <class _OtherErr
>
1420 requires is_constructible_v
<_Err
, const _OtherErr
&>
1421 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>) expected(
1422 const unexpected
<_OtherErr
>& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
1423 : __base(unexpect
, __unex
.error()) {}
1425 template <class _OtherErr
>
1426 requires is_constructible_v
<_Err
, _OtherErr
>
1427 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
1428 expected(unexpected
<_OtherErr
>&& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
1429 : __base(unexpect
, std::move(__unex
.error())) {}
1431 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
) noexcept
: __base(in_place
) {}
1433 template <class... _Args
>
1434 requires is_constructible_v
<_Err
, _Args
...>
1435 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, _Args
&&... __args
) noexcept(
1436 is_nothrow_constructible_v
<_Err
, _Args
...>) // strengthened
1437 : __base(unexpect
, std::forward
<_Args
>(__args
)...) {}
1439 template <class _Up
, class... _Args
>
1440 requires is_constructible_v
< _Err
, initializer_list
<_Up
>&, _Args
... >
1441 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
1442 is_nothrow_constructible_v
<_Err
, initializer_list
<_Up
>&, _Args
...>) // strengthened
1443 : __base(unexpect
, __il
, std::forward
<_Args
>(__args
)...) {}
1446 template <class _Func
, class... _Args
>
1447 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
1448 __expected_construct_unexpected_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
1449 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
1452 // [expected.void.dtor], destructor
1454 _LIBCPP_HIDE_FROM_ABI
constexpr ~expected() = default;
1457 template <class... _Args
>
1458 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(unexpect_t
, _Args
&&... __args
) {
1459 _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");
1462 auto __trans
= std::__make_exception_guard([&] { this->__construct(in_place
); });
1463 this->__construct(unexpect
, std::forward
<_Args
>(__args
)...);
1464 __trans
.__complete();
1467 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(in_place_t
) {
1468 _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");
1471 this->__construct(in_place
);
1475 // [expected.void.assign], assignment
1476 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
&) = delete;
1478 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
& __rhs
) noexcept(
1479 is_nothrow_copy_assignable_v
<_Err
> && is_nothrow_copy_constructible_v
<_Err
>) // strengthened
1480 requires(is_copy_assignable_v
<_Err
> && is_copy_constructible_v
<_Err
>)
1482 if (this->__has_val()) {
1483 if (!__rhs
.__has_val()) {
1484 __reinit_expected(unexpect
, __rhs
.__unex());
1487 if (__rhs
.__has_val()) {
1488 __reinit_expected(in_place
);
1490 this->__unex() = __rhs
.__unex();
1496 _LIBCPP_HIDE_FROM_ABI
constexpr expected
&
1497 operator=(expected
&& __rhs
) noexcept(is_nothrow_move_assignable_v
<_Err
> && is_nothrow_move_constructible_v
<_Err
>)
1498 requires(is_move_assignable_v
<_Err
> && is_move_constructible_v
<_Err
>)
1500 if (this->__has_val()) {
1501 if (!__rhs
.__has_val()) {
1502 __reinit_expected(unexpect
, std::move(__rhs
.__unex()));
1505 if (__rhs
.__has_val()) {
1506 __reinit_expected(in_place
);
1508 this->__unex() = std::move(__rhs
.__unex());
1514 template <class _OtherErr
>
1515 requires(is_constructible_v
<_Err
, const _OtherErr
&> && is_assignable_v
<_Err
&, const _OtherErr
&>)
1516 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const unexpected
<_OtherErr
>& __un
) {
1517 if (this->__has_val()) {
1518 __reinit_expected(unexpect
, __un
.error());
1520 this->__unex() = __un
.error();
1525 template <class _OtherErr
>
1526 requires(is_constructible_v
<_Err
, _OtherErr
> && is_assignable_v
<_Err
&, _OtherErr
>)
1527 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(unexpected
<_OtherErr
>&& __un
) {
1528 if (this->__has_val()) {
1529 __reinit_expected(unexpect
, std::move(__un
.error()));
1531 this->__unex() = std::move(__un
.error());
1536 _LIBCPP_HIDE_FROM_ABI
constexpr void emplace() noexcept
{
1537 if (!this->__has_val()) {
1538 __reinit_expected(in_place
);
1542 // [expected.void.swap], swap
1543 _LIBCPP_HIDE_FROM_ABI
constexpr void
1544 swap(expected
& __rhs
) noexcept(is_nothrow_move_constructible_v
<_Err
> && is_nothrow_swappable_v
<_Err
>)
1545 requires(is_swappable_v
<_Err
> && is_move_constructible_v
<_Err
>)
1547 auto __swap_val_unex_impl
= [](expected
& __with_val
, expected
& __with_err
) {
1548 // May throw, but will re-engage `__with_val` in that case.
1549 __with_val
.__reinit_expected(unexpect
, std::move(__with_err
.__unex()));
1551 __with_err
.__reinit_expected(in_place
);
1554 if (this->__has_val()) {
1555 if (!__rhs
.__has_val()) {
1556 __swap_val_unex_impl(*this, __rhs
);
1559 if (__rhs
.__has_val()) {
1560 __swap_val_unex_impl(__rhs
, *this);
1563 swap(this->__unex(), __rhs
.__unex());
1568 _LIBCPP_HIDE_FROM_ABI
friend constexpr void swap(expected
& __x
, expected
& __y
) noexcept(noexcept(__x
.swap(__y
)))
1569 requires requires
{ __x
.swap(__y
); }
1574 // [expected.void.obs], observers
1575 _LIBCPP_HIDE_FROM_ABI
constexpr explicit operator bool() const noexcept
{ return this->__has_val(); }
1577 _LIBCPP_HIDE_FROM_ABI
constexpr bool has_value() const noexcept
{ return this->__has_val(); }
1579 _LIBCPP_HIDE_FROM_ABI
constexpr void operator*() const noexcept
{
1580 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1581 this->__has_val(), "expected::operator* requires the expected to contain a value");
1584 _LIBCPP_HIDE_FROM_ABI
constexpr void value() const& {
1585 static_assert(is_copy_constructible_v
<_Err
>);
1586 if (!this->__has_val()) {
1587 std::__throw_bad_expected_access
<_Err
>(this->__unex());
1591 _LIBCPP_HIDE_FROM_ABI
constexpr void value() && {
1592 static_assert(is_copy_constructible_v
<_Err
> && is_move_constructible_v
<_Err
>);
1593 if (!this->__has_val()) {
1594 std::__throw_bad_expected_access
<_Err
>(std::move(this->__unex()));
1598 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& error() const& noexcept
{
1599 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1600 !this->__has_val(), "expected::error requires the expected to contain an error");
1601 return this->__unex();
1604 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& error() & noexcept
{
1605 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1606 !this->__has_val(), "expected::error requires the expected to contain an error");
1607 return this->__unex();
1610 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
&& error() const&& noexcept
{
1611 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1612 !this->__has_val(), "expected::error requires the expected to contain an error");
1613 return std::move(this->__unex());
1616 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
&& error() && noexcept
{
1617 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1618 !this->__has_val(), "expected::error requires the expected to contain an error");
1619 return std::move(this->__unex());
1622 template <class _Up
= _Err
>
1623 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) const& {
1624 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
1625 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
1627 return std::forward
<_Up
>(__error
);
1632 template <class _Up
= _Err
>
1633 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) && {
1634 static_assert(is_move_constructible_v
<_Err
>, "error_type has to be move constructible");
1635 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
1637 return std::forward
<_Up
>(__error
);
1639 return std::move(error());
1642 // [expected.void.monadic], monadic
1643 template <class _Func
>
1644 requires is_constructible_v
<_Err
, _Err
&>
1645 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) & {
1646 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
>>;
1647 static_assert(__is_std_expected
<_Up
>::value
, "The result of f() must be a specialization of std::expected");
1649 is_same_v
<typename
_Up::error_type
, _Err
>, "The result of f() must have the same error_type as this expected");
1651 return std::invoke(std::forward
<_Func
>(__f
));
1653 return _Up(unexpect
, error());
1656 template <class _Func
>
1657 requires is_constructible_v
<_Err
, const _Err
&>
1658 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const& {
1659 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
>>;
1660 static_assert(__is_std_expected
<_Up
>::value
, "The result of f() must be a specialization of std::expected");
1662 is_same_v
<typename
_Up::error_type
, _Err
>, "The result of f() must have the same error_type as this expected");
1664 return std::invoke(std::forward
<_Func
>(__f
));
1666 return _Up(unexpect
, error());
1669 template <class _Func
>
1670 requires is_constructible_v
<_Err
, _Err
&&>
1671 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) && {
1672 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
>>;
1673 static_assert(__is_std_expected
<_Up
>::value
, "The result of f() must be a specialization of std::expected");
1675 is_same_v
<typename
_Up::error_type
, _Err
>, "The result of f() must have the same error_type as this expected");
1677 return std::invoke(std::forward
<_Func
>(__f
));
1679 return _Up(unexpect
, std::move(error()));
1682 template <class _Func
>
1683 requires is_constructible_v
<_Err
, const _Err
&&>
1684 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const&& {
1685 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
>>;
1686 static_assert(__is_std_expected
<_Up
>::value
, "The result of f() must be a specialization of std::expected");
1688 is_same_v
<typename
_Up::error_type
, _Err
>, "The result of f() must have the same error_type as this expected");
1690 return std::invoke(std::forward
<_Func
>(__f
));
1692 return _Up(unexpect
, std::move(error()));
1695 template <class _Func
>
1696 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) & {
1697 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&>>;
1698 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
1699 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1700 "The result of f(error()) must have the same value_type as this expected");
1704 return std::invoke(std::forward
<_Func
>(__f
), error());
1707 template <class _Func
>
1708 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const& {
1709 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&>>;
1710 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
1711 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1712 "The result of f(error()) must have the same value_type as this expected");
1716 return std::invoke(std::forward
<_Func
>(__f
), error());
1719 template <class _Func
>
1720 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) && {
1721 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&&>>;
1723 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1724 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1725 "The result of f(std::move(error())) must have the same value_type as this expected");
1729 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1732 template <class _Func
>
1733 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const&& {
1734 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1736 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1737 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1738 "The result of f(std::move(error())) must have the same value_type as this expected");
1742 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1745 template <class _Func
>
1746 requires is_constructible_v
<_Err
, _Err
&>
1747 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) & {
1748 using _Up
= remove_cv_t
<invoke_result_t
<_Func
>>;
1750 return expected
<_Up
, _Err
>(unexpect
, error());
1752 if constexpr (!is_void_v
<_Up
>) {
1753 return expected
<_Up
, _Err
>(__expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
));
1755 std::invoke(std::forward
<_Func
>(__f
));
1756 return expected
<_Up
, _Err
>();
1760 template <class _Func
>
1761 requires is_constructible_v
<_Err
, const _Err
&>
1762 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const& {
1763 using _Up
= remove_cv_t
<invoke_result_t
<_Func
>>;
1765 return expected
<_Up
, _Err
>(unexpect
, error());
1767 if constexpr (!is_void_v
<_Up
>) {
1768 return expected
<_Up
, _Err
>(__expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
));
1770 std::invoke(std::forward
<_Func
>(__f
));
1771 return expected
<_Up
, _Err
>();
1775 template <class _Func
>
1776 requires is_constructible_v
<_Err
, _Err
&&>
1777 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) && {
1778 using _Up
= remove_cv_t
<invoke_result_t
<_Func
>>;
1780 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1782 if constexpr (!is_void_v
<_Up
>) {
1783 return expected
<_Up
, _Err
>(__expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
));
1785 std::invoke(std::forward
<_Func
>(__f
));
1786 return expected
<_Up
, _Err
>();
1790 template <class _Func
>
1791 requires is_constructible_v
<_Err
, const _Err
&&>
1792 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const&& {
1793 using _Up
= remove_cv_t
<invoke_result_t
<_Func
>>;
1795 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1797 if constexpr (!is_void_v
<_Up
>) {
1798 return expected
<_Up
, _Err
>(__expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
));
1800 std::invoke(std::forward
<_Func
>(__f
));
1801 return expected
<_Up
, _Err
>();
1805 template <class _Func
>
1806 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) & {
1807 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&>>;
1808 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1809 "The result of f(error()) must be a valid template argument for unexpected");
1811 return expected
<_Tp
, _Gp
>();
1813 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1816 template <class _Func
>
1817 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const& {
1818 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&>>;
1819 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1820 "The result of f(error()) must be a valid template argument for unexpected");
1822 return expected
<_Tp
, _Gp
>();
1824 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1827 template <class _Func
>
1828 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) && {
1829 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&&>>;
1830 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1831 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1833 return expected
<_Tp
, _Gp
>();
1835 return expected
<_Tp
, _Gp
>(
1836 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1839 template <class _Func
>
1840 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const&& {
1841 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1842 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1843 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1845 return expected
<_Tp
, _Gp
>();
1847 return expected
<_Tp
, _Gp
>(
1848 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1851 // [expected.void.eq], equality operators
1852 template <class _T2
, class _E2
>
1853 requires is_void_v
<_T2
>
1854 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const expected
<_T2
, _E2
>& __y
) {
1855 if (__x
.__has_val() != __y
.__has_val()) {
1858 return __x
.__has_val() || static_cast<bool>(__x
.__unex() == __y
.__unex());
1862 template <class _E2
>
1863 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const unexpected
<_E2
>& __y
) {
1864 return !__x
.__has_val() && static_cast<bool>(__x
.__unex() == __y
.error());
1868 _LIBCPP_END_NAMESPACE_STD
1870 #endif // _LIBCPP_STD_VER >= 23
1874 #endif // _LIBCPP___EXPECTED_EXPECTED_H