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
12 #include <__cxx03/__assert>
13 #include <__cxx03/__config>
14 #include <__cxx03/__expected/bad_expected_access.h>
15 #include <__cxx03/__expected/unexpect.h>
16 #include <__cxx03/__expected/unexpected.h>
17 #include <__cxx03/__functional/invoke.h>
18 #include <__cxx03/__memory/addressof.h>
19 #include <__cxx03/__memory/construct_at.h>
20 #include <__cxx03/__type_traits/conjunction.h>
21 #include <__cxx03/__type_traits/disjunction.h>
22 #include <__cxx03/__type_traits/integral_constant.h>
23 #include <__cxx03/__type_traits/is_assignable.h>
24 #include <__cxx03/__type_traits/is_constructible.h>
25 #include <__cxx03/__type_traits/is_convertible.h>
26 #include <__cxx03/__type_traits/is_function.h>
27 #include <__cxx03/__type_traits/is_nothrow_assignable.h>
28 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
29 #include <__cxx03/__type_traits/is_reference.h>
30 #include <__cxx03/__type_traits/is_same.h>
31 #include <__cxx03/__type_traits/is_swappable.h>
32 #include <__cxx03/__type_traits/is_trivially_constructible.h>
33 #include <__cxx03/__type_traits/is_trivially_destructible.h>
34 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
35 #include <__cxx03/__type_traits/is_void.h>
36 #include <__cxx03/__type_traits/lazy.h>
37 #include <__cxx03/__type_traits/negation.h>
38 #include <__cxx03/__type_traits/remove_cv.h>
39 #include <__cxx03/__type_traits/remove_cvref.h>
40 #include <__cxx03/__utility/as_const.h>
41 #include <__cxx03/__utility/exception_guard.h>
42 #include <__cxx03/__utility/forward.h>
43 #include <__cxx03/__utility/in_place.h>
44 #include <__cxx03/__utility/move.h>
45 #include <__cxx03/__utility/swap.h>
46 #include <__cxx03/__verbose_abort>
47 #include <__cxx03/initializer_list>
49 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50 # pragma GCC system_header
54 #include <__cxx03/__undef_macros>
56 #if _LIBCPP_STD_VER >= 23
58 _LIBCPP_BEGIN_NAMESPACE_STD
60 template <class _Tp
, class _Err
>
64 struct __is_std_expected
: false_type
{};
66 template <class _Tp
, class _Err
>
67 struct __is_std_expected
<expected
<_Tp
, _Err
>> : true_type
{};
69 struct __expected_construct_in_place_from_invoke_tag
{};
70 struct __expected_construct_unexpected_from_invoke_tag
{};
72 template <class _Err
, class _Arg
>
73 _LIBCPP_HIDE_FROM_ABI
void __throw_bad_expected_access(_Arg
&& __arg
) {
74 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
75 throw bad_expected_access
<_Err
>(std::forward
<_Arg
>(__arg
));
78 _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");
82 // If parameter type `_Tp` of `__conditional_no_unique_address` is neither
83 // copyable nor movable, a constructor with this tag is provided. For that
84 // constructor, the user has to provide a function and arguments. The function
85 // must return an object of type `_Tp`. When the function is invoked by the
86 // constructor, guaranteed copy elision kicks in and the `_Tp` is constructed
88 struct __conditional_no_unique_address_invoke_tag
{};
90 // This class implements an object with `[[no_unique_address]]` conditionally applied to it,
91 // based on the value of `_NoUnique`.
93 // A member of this class must always have `[[no_unique_address]]` applied to
94 // it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case
95 // would not have any effect. In the `false` case, the `__v` is not
96 // `[[no_unique_address]]`, so nullifies the effects of the "outer"
97 // `[[no_unique_address]]` regarding data layout.
99 // If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`.
100 template <bool _NoUnique
, class _Tp
>
101 struct __conditional_no_unique_address
;
104 struct __conditional_no_unique_address
<true, _Tp
> {
105 template <class... _Args
>
106 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(in_place_t
, _Args
&&... __args
)
107 : __v(std::forward
<_Args
>(__args
)...) {}
109 template <class _Func
, class... _Args
>
110 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(
111 __conditional_no_unique_address_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
112 : __v(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
114 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v
;
118 struct __conditional_no_unique_address
<false, _Tp
> {
119 template <class... _Args
>
120 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(in_place_t
, _Args
&&... __args
)
121 : __v(std::forward
<_Args
>(__args
)...) {}
123 template <class _Func
, class... _Args
>
124 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __conditional_no_unique_address(
125 __conditional_no_unique_address_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
126 : __v(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
131 // This function returns whether the type `_Second` can be stuffed into the tail padding
132 // of the `_First` type if both of them are given `[[no_unique_address]]`.
133 template <class _First
, class _Second
>
134 inline constexpr bool __fits_in_tail_padding
= []() {
136 _LIBCPP_NO_UNIQUE_ADDRESS _First __first
;
137 _LIBCPP_NO_UNIQUE_ADDRESS _Second __second
;
139 return sizeof(__x
) == sizeof(_First
);
142 // This class implements the storage used by `std::expected`. We have a few
143 // goals for this storage:
144 // 1. Whenever the underlying {_Tp | _Unex} combination has free bytes in its
145 // tail padding, we should reuse it to store the bool discriminator of the
146 // expected, so as to save space.
147 // 2. Whenever the `expected<_Tp, _Unex>` as a whole has free bytes in its tail
148 // padding, we should allow an object following the expected to be stored in
150 // 3. However, we never want a user object (say `X`) that would follow an
151 // `expected<_Tp, _Unex>` to be stored in the padding bytes of the
152 // underlying {_Tp | _Unex} union, if any. That is because we use
153 // `construct_at` on that union, which would end up overwriting the `X`
154 // member if it is stored in the tail padding of the union.
156 // To achieve this, `__expected_base`'s logic is implemented in an inner
157 // `__repr` class. `__expected_base` holds one `__repr` member which is
158 // conditionally `[[no_unique_address]]`. The `__repr` class holds the
159 // underlying {_Tp | _Unex} union and a boolean "has value" flag.
161 // Which one of the `__repr_`/`__union_` members is `[[no_unique_address]]`
162 // depends on whether the "has value" boolean fits into the tail padding of
163 // the underlying {_Tp | _Unex} union:
165 // - In case the "has value" bool fits into the tail padding of the union, the
166 // whole `__repr_` member is _not_ `[[no_unique_address]]` as it needs to be
167 // transparently replaced on `emplace()`/`swap()` etc.
168 // - In case the "has value" bool does not fit into the tail padding of the
169 // union, only the union member must be transparently replaced (therefore is
170 // _not_ `[[no_unique_address]]`) and the "has value" flag must be adjusted
173 // This way, the member that is transparently replaced on mutating operations
174 // is never `[[no_unique_address]]`, satisfying the requirements from
175 // "[basic.life]" in the standard.
177 // Stripped away of all superfluous elements, the layout of `__expected_base`
178 // then looks like this:
180 // template <class Tp, class Err>
181 // class expected_base {
183 // [[no_unique_address]] Tp val;
184 // [[no_unique_address]] Err unex;
187 // static constexpr bool put_flag_in_tail = fits_in_tail_padding<union_t, bool>;
188 // static constexpr bool allow_reusing_expected_tail_padding = !put_flag_in_tail;
192 // // If "has value" fits into the tail, this should be
193 // // `[[no_unique_address]]`, otherwise not.
194 // [[no_unique_address]] conditional_no_unique_address<
196 // union_t>::type union_;
197 // [[no_unique_address]] bool has_val_;
201 // // If "has value" fits into the tail, this must _not_ be
202 // // `[[no_unique_address]]` so that we fill out the
203 // // complete `expected` object.
204 // [[no_unique_address]] conditional_no_unique_address<
205 // allow_reusing_expected_tail_padding,
206 // repr>::type repr_;
209 template <class _Tp
, class _Err
>
210 class __expected_base
{
211 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
212 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
213 // it's not clear that it's implementable, given that the function is allowed to clobber
214 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
216 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&) = delete;
217 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&)
218 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
219 is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>)
221 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&) = delete;
222 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&)
223 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
224 is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>)
226 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(const __union_t
&) = delete;
227 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(__union_t
&&) = delete;
229 template <class... _Args
>
230 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(in_place_t
, _Args
&&... __args
)
231 : __val_(std::forward
<_Args
>(__args
)...) {}
233 template <class... _Args
>
234 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(unexpect_t
, _Args
&&... __args
)
235 : __unex_(std::forward
<_Args
>(__args
)...) {}
237 template <class _Func
, class... _Args
>
238 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
239 std::__expected_construct_in_place_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
240 : __val_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
242 template <class _Func
, class... _Args
>
243 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
244 std::__expected_construct_unexpected_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
245 : __unex_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
247 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
248 requires(is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>)
251 // __repr's destructor handles this
252 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t() {}
254 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_
;
255 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_
;
258 static constexpr bool __put_flag_in_tail
= __fits_in_tail_padding
<__union_t
, bool>;
259 static constexpr bool __allow_reusing_expected_tail_padding
= !__put_flag_in_tail
;
262 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr() = delete;
264 template <class... _Args
>
265 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(in_place_t __tag
, _Args
&&... __args
)
266 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(true) {}
268 template <class... _Args
>
269 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(unexpect_t __tag
, _Args
&&... __args
)
270 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
272 template <class... _Args
>
273 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag
,
275 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(true) {}
277 template <class... _Args
>
278 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag
,
280 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
282 // The return value of `__make_union` must be constructed in place in the
283 // `__v` member of `__union_`, relying on guaranteed copy elision. To do
284 // this, the `__conditional_no_unique_address_invoke_tag` constructor is
285 // called with a lambda that is immediately called inside
286 // `__conditional_no_unique_address`'s constructor.
287 template <class _OtherUnion
>
288 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(bool __has_val
, _OtherUnion
&& __other
)
289 requires(__allow_reusing_expected_tail_padding
)
290 : __union_(__conditional_no_unique_address_invoke_tag
{},
291 [&] { return __make_union(__has_val
, std::forward
<_OtherUnion
>(__other
)); }),
292 __has_val_(__has_val
) {}
294 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&) = delete;
295 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&)
296 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
297 is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>)
299 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&) = delete;
300 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&)
301 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
302 is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>)
305 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(const __repr
&) = delete;
306 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(__repr
&&) = delete;
308 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
309 requires(is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>)
312 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
313 requires(!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>)
315 __destroy_union_member();
318 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
319 requires(__allow_reusing_expected_tail_padding
&&
320 (is_trivially_destructible_v
<_Tp
> && is_trivially_destructible_v
<_Err
>))
322 // Note: Since the destructor of the union is trivial, this does nothing
323 // except to end the lifetime of the union.
324 std::destroy_at(&__union_
.__v
);
327 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
328 requires(__allow_reusing_expected_tail_padding
&&
329 (!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>))
331 __destroy_union_member();
332 std::destroy_at(&__union_
.__v
);
335 template <class... _Args
>
336 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(in_place_t
, _Args
&&... __args
)
337 requires(__allow_reusing_expected_tail_padding
)
339 std::construct_at(&__union_
.__v
, in_place
, std::forward
<_Args
>(__args
)...);
343 template <class... _Args
>
344 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(unexpect_t
, _Args
&&... __args
)
345 requires(__allow_reusing_expected_tail_padding
)
347 std::construct_at(&__union_
.__v
, unexpect
, std::forward
<_Args
>(__args
)...);
352 template <class, class>
353 friend class __expected_base
;
355 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union_member()
356 requires(!is_trivially_destructible_v
<_Tp
> || !is_trivially_destructible_v
<_Err
>)
359 std::destroy_at(std::addressof(__union_
.__v
.__val_
));
361 std::destroy_at(std::addressof(__union_
.__v
.__unex_
));
365 template <class _OtherUnion
>
366 _LIBCPP_HIDE_FROM_ABI
static constexpr __union_t
__make_union(bool __has_val
, _OtherUnion
&& __other
)
367 requires(__allow_reusing_expected_tail_padding
)
370 return __union_t(in_place
, std::forward
<_OtherUnion
>(__other
).__val_
);
372 return __union_t(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
375 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__put_flag_in_tail
, __union_t
> __union_
;
376 _LIBCPP_NO_UNIQUE_ADDRESS
bool __has_val_
;
379 template <class _OtherUnion
>
380 _LIBCPP_HIDE_FROM_ABI
static constexpr __repr
__make_repr(bool __has_val
, _OtherUnion
&& __other
)
381 requires(__put_flag_in_tail
)
384 return __repr(in_place
, std::forward
<_OtherUnion
>(__other
).__val_
);
386 return __repr(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
390 template <class... _Args
>
391 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_base(_Args
&&... __args
)
392 : __repr_(in_place
, std::forward
<_Args
>(__args
)...) {}
394 // In case we copy/move construct from another `expected` we need to create
395 // our `expected` so that it either has a value or not, depending on the "has
396 // value" flag of the other `expected`. To do this without falling back on
397 // `std::construct_at` we rely on guaranteed copy elision using two helper
398 // functions `__make_repr` and `__make_union`. There have to be two since
399 // there are two data layouts with different members being
400 // `[[no_unique_address]]`. GCC (as of version 13) does not do guaranteed
401 // copy elision when initializing `[[no_unique_address]]` members. The two
404 // - `__make_repr`: This is used when the "has value" flag lives in the tail
405 // of the union. In this case, the `__repr` member is _not_
406 // `[[no_unique_address]]`.
407 // - `__make_union`: When the "has value" flag does _not_ fit in the tail of
408 // the union, the `__repr` member is `[[no_unique_address]]` and the union
411 // This constructor "catches" the first case and leaves the second case to
412 // `__union_t`, its constructors and `__make_union`.
413 template <class _OtherUnion
>
414 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_base(bool __has_val
, _OtherUnion
&& __other
)
415 requires(__put_flag_in_tail
)
416 : __repr_(__conditional_no_unique_address_invoke_tag
{},
417 [&] { return __make_repr(__has_val
, std::forward
<_OtherUnion
>(__other
)); }) {}
419 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy() {
420 if constexpr (__put_flag_in_tail
)
421 std::destroy_at(&__repr_
.__v
);
423 __repr_
.__v
.__destroy_union();
426 template <class _Tag
, class... _Args
>
427 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct(_Tag __tag
, _Args
&&... __args
) {
428 if constexpr (__put_flag_in_tail
)
429 std::construct_at(&__repr_
.__v
, __tag
, std::forward
<_Args
>(__args
)...);
431 __repr_
.__v
.__construct_union(__tag
, std::forward
<_Args
>(__args
)...);
434 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_val() const { return __repr_
.__v
.__has_val_
; }
435 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& __union() { return __repr_
.__v
.__union_
.__v
; }
436 _LIBCPP_HIDE_FROM_ABI
constexpr const __union_t
& __union() const { return __repr_
.__v
.__union_
.__v
; }
437 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& __val() { return __repr_
.__v
.__union_
.__v
.__val_
; }
438 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& __val() const { return __repr_
.__v
.__union_
.__v
.__val_
; }
439 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& __unex() { return __repr_
.__v
.__union_
.__v
.__unex_
; }
440 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& __unex() const { return __repr_
.__v
.__union_
.__v
.__unex_
; }
443 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__allow_reusing_expected_tail_padding
, __repr
> __repr_
;
446 template <class _Tp
, class _Err
>
447 class expected
: private __expected_base
<_Tp
, _Err
> {
448 static_assert(!is_reference_v
<_Tp
> && !is_function_v
<_Tp
> && !is_same_v
<remove_cv_t
<_Tp
>, in_place_t
> &&
449 !is_same_v
<remove_cv_t
<_Tp
>, unexpect_t
> && !__is_std_unexpected
<remove_cv_t
<_Tp
>>::value
&&
450 __valid_std_unexpected
<_Err
>::value
,
451 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
452 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
453 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
454 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
455 "template argument for unexpected is ill-formed.");
457 template <class _Up
, class _OtherErr
>
458 friend class expected
;
460 using __base
= __expected_base
<_Tp
, _Err
>;
463 using value_type
= _Tp
;
464 using error_type
= _Err
;
465 using unexpected_type
= unexpected
<_Err
>;
467 using __trivially_relocatable
=
468 __conditional_t
<__libcpp_is_trivially_relocatable
<_Tp
>::value
&& __libcpp_is_trivially_relocatable
<_Err
>::value
,
473 using rebind
= expected
<_Up
, error_type
>;
475 // [expected.object.ctor], constructors
476 _LIBCPP_HIDE_FROM_ABI
constexpr expected() noexcept(is_nothrow_default_constructible_v
<_Tp
>) // strengthened
477 requires is_default_constructible_v
<_Tp
>
478 : __base(in_place
) {}
480 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&) = delete;
482 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&)
483 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Tp
> &&
484 is_trivially_copy_constructible_v
<_Err
>)
487 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
& __other
) noexcept(
488 is_nothrow_copy_constructible_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Err
>) // strengthened
489 requires(is_copy_constructible_v
<_Tp
> && is_copy_constructible_v
<_Err
> &&
490 !(is_trivially_copy_constructible_v
<_Tp
> && is_trivially_copy_constructible_v
<_Err
>))
491 : __base(__other
.__has_val(), __other
.__union()) {}
493 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&&)
494 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Tp
> &&
495 is_trivially_move_constructible_v
<_Err
>)
498 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&& __other
) noexcept(
499 is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_move_constructible_v
<_Err
>)
500 requires(is_move_constructible_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
501 !(is_trivially_move_constructible_v
<_Tp
> && is_trivially_move_constructible_v
<_Err
>))
502 : __base(__other
.__has_val(), std::move(__other
.__union())) {}
505 template <class _Up
, class _OtherErr
, class _UfQual
, class _OtherErrQual
>
506 using __can_convert
= _And
<
507 is_constructible
<_Tp
, _UfQual
>,
508 is_constructible
<_Err
, _OtherErrQual
>,
509 _If
<_Not
<is_same
<remove_cv_t
<_Tp
>, bool>>::value
,
510 _And
< _Not
<_And
<is_same
<_Tp
, _Up
>, is_same
<_Err
, _OtherErr
>>>, // use the copy constructor instead, see #92676
511 _Not
<is_constructible
<_Tp
, expected
<_Up
, _OtherErr
>&>>,
512 _Not
<is_constructible
<_Tp
, expected
<_Up
, _OtherErr
>>>,
513 _Not
<is_constructible
<_Tp
, const expected
<_Up
, _OtherErr
>&>>,
514 _Not
<is_constructible
<_Tp
, const expected
<_Up
, _OtherErr
>>>,
515 _Not
<is_convertible
<expected
<_Up
, _OtherErr
>&, _Tp
>>,
516 _Not
<is_convertible
<expected
<_Up
, _OtherErr
>&&, _Tp
>>,
517 _Not
<is_convertible
<const expected
<_Up
, _OtherErr
>&, _Tp
>>,
518 _Not
<is_convertible
<const expected
<_Up
, _OtherErr
>&&, _Tp
>>>,
520 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>&>>,
521 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>>>,
522 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>&>>,
523 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>>> >;
525 template <class _Func
, class... _Args
>
526 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
527 std::__expected_construct_in_place_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
528 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
530 template <class _Func
, class... _Args
>
531 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
532 std::__expected_construct_unexpected_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
533 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
536 template <class _Up
, class _OtherErr
>
537 requires __can_convert
<_Up
, _OtherErr
, const _Up
&, const _OtherErr
&>::value
538 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _Up
&, _Tp
> ||
539 !is_convertible_v
<const _OtherErr
&, _Err
>)
540 expected(const expected
<_Up
, _OtherErr
>& __other
) noexcept(
541 is_nothrow_constructible_v
<_Tp
, const _Up
&> &&
542 is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
543 : __base(__other
.__has_val(), __other
.__union()) {}
545 template <class _Up
, class _OtherErr
>
546 requires __can_convert
<_Up
, _OtherErr
, _Up
, _OtherErr
>::value
547 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_Up
, _Tp
> || !is_convertible_v
<_OtherErr
, _Err
>)
548 expected(expected
<_Up
, _OtherErr
>&& __other
) noexcept(
549 is_nothrow_constructible_v
<_Tp
, _Up
> && is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
550 : __base(__other
.__has_val(), std::move(__other
.__union())) {}
552 template <class _Up
= _Tp
>
553 requires(!is_same_v
<remove_cvref_t
<_Up
>, in_place_t
> && !is_same_v
<expected
, remove_cvref_t
<_Up
>> &&
554 is_constructible_v
<_Tp
, _Up
> && !__is_std_unexpected
<remove_cvref_t
<_Up
>>::value
&&
555 (!is_same_v
<remove_cv_t
<_Tp
>, bool> || !__is_std_expected
<remove_cvref_t
<_Up
>>::value
))
556 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_Up
, _Tp
>)
557 expected(_Up
&& __u
) noexcept(is_nothrow_constructible_v
<_Tp
, _Up
>) // strengthened
558 : __base(in_place
, std::forward
<_Up
>(__u
)) {}
560 template <class _OtherErr
>
561 requires is_constructible_v
<_Err
, const _OtherErr
&>
562 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>) expected(
563 const unexpected
<_OtherErr
>& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
564 : __base(unexpect
, __unex
.error()) {}
566 template <class _OtherErr
>
567 requires is_constructible_v
<_Err
, _OtherErr
>
568 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
569 expected(unexpected
<_OtherErr
>&& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
570 : __base(unexpect
, std::move(__unex
.error())) {}
572 template <class... _Args
>
573 requires is_constructible_v
<_Tp
, _Args
...>
574 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
, _Args
&&... __args
) noexcept(
575 is_nothrow_constructible_v
<_Tp
, _Args
...>) // strengthened
576 : __base(in_place
, std::forward
<_Args
>(__args
)...) {}
578 template <class _Up
, class... _Args
>
579 requires is_constructible_v
< _Tp
, initializer_list
<_Up
>&, _Args
... >
580 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
581 is_nothrow_constructible_v
<_Tp
, initializer_list
<_Up
>&, _Args
...>) // strengthened
582 : __base(in_place
, __il
, std::forward
<_Args
>(__args
)...) {}
584 template <class... _Args
>
585 requires is_constructible_v
<_Err
, _Args
...>
586 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, _Args
&&... __args
) noexcept(
587 is_nothrow_constructible_v
<_Err
, _Args
...>) // strengthened
588 : __base(unexpect
, std::forward
<_Args
>(__args
)...) {}
590 template <class _Up
, class... _Args
>
591 requires is_constructible_v
< _Err
, initializer_list
<_Up
>&, _Args
... >
592 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
593 is_nothrow_constructible_v
<_Err
, initializer_list
<_Up
>&, _Args
...>) // strengthened
594 : __base(unexpect
, __il
, std::forward
<_Args
>(__args
)...) {}
596 // [expected.object.dtor], destructor
598 _LIBCPP_HIDE_FROM_ABI
constexpr ~expected() = default;
601 template <class _Tag
, class _OtherTag
, class _T1
, class _T2
, class... _Args
>
602 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(_T2
& __oldval
, _Args
&&... __args
) {
603 if constexpr (is_nothrow_constructible_v
<_T1
, _Args
...>) {
605 this->__construct(_Tag
{}, std::forward
<_Args
>(__args
)...);
606 } else if constexpr (is_nothrow_move_constructible_v
<_T1
>) {
607 _T1
__tmp(std::forward
<_Args
>(__args
)...);
609 this->__construct(_Tag
{}, std::move(__tmp
));
612 is_nothrow_move_constructible_v
<_T2
>,
613 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
614 "be reverted to the previous state in case an exception is thrown during the assignment.");
615 _T2
__tmp(std::move(__oldval
));
617 auto __trans
= std::__make_exception_guard([&] { this->__construct(_OtherTag
{}, std::move(__tmp
)); });
618 this->__construct(_Tag
{}, std::forward
<_Args
>(__args
)...);
619 __trans
.__complete();
624 // [expected.object.assign], assignment
625 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
&) = delete;
627 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
& __rhs
) noexcept(
628 is_nothrow_copy_assignable_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Tp
> && is_nothrow_copy_assignable_v
<_Err
> &&
629 is_nothrow_copy_constructible_v
<_Err
>) // strengthened
630 requires(is_copy_assignable_v
<_Tp
> && is_copy_constructible_v
<_Tp
> && is_copy_assignable_v
<_Err
> &&
631 is_copy_constructible_v
<_Err
> &&
632 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
634 if (this->__has_val() && __rhs
.__has_val()) {
635 this->__val() = __rhs
.__val();
636 } else if (this->__has_val()) {
637 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), __rhs
.__unex());
638 } else if (__rhs
.__has_val()) {
639 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), __rhs
.__val());
641 this->__unex() = __rhs
.__unex();
646 _LIBCPP_HIDE_FROM_ABI
constexpr expected
&
647 operator=(expected
&& __rhs
) noexcept(is_nothrow_move_assignable_v
<_Tp
> && is_nothrow_move_constructible_v
<_Tp
> &&
648 is_nothrow_move_assignable_v
<_Err
> && is_nothrow_move_constructible_v
<_Err
>)
649 requires(is_move_constructible_v
<_Tp
> && is_move_assignable_v
<_Tp
> && is_move_constructible_v
<_Err
> &&
650 is_move_assignable_v
<_Err
> &&
651 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
653 if (this->__has_val() && __rhs
.__has_val()) {
654 this->__val() = std::move(__rhs
.__val());
655 } else if (this->__has_val()) {
656 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), std::move(__rhs
.__unex()));
657 } else if (__rhs
.__has_val()) {
658 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), std::move(__rhs
.__val()));
660 this->__unex() = std::move(__rhs
.__unex());
665 template <class _Up
= _Tp
>
666 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(_Up
&& __v
)
667 requires(!is_same_v
<expected
, remove_cvref_t
<_Up
>> && !__is_std_unexpected
<remove_cvref_t
<_Up
>>::value
&&
668 is_constructible_v
<_Tp
, _Up
> && is_assignable_v
<_Tp
&, _Up
> &&
669 (is_nothrow_constructible_v
<_Tp
, _Up
> || is_nothrow_move_constructible_v
<_Tp
> ||
670 is_nothrow_move_constructible_v
<_Err
>))
672 if (this->__has_val()) {
673 this->__val() = std::forward
<_Up
>(__v
);
675 __reinit_expected
<in_place_t
, unexpect_t
, _Tp
, _Err
>(this->__unex(), std::forward
<_Up
>(__v
));
681 template <class _OtherErrQual
>
682 static constexpr bool __can_assign_from_unexpected
=
683 _And
< is_constructible
<_Err
, _OtherErrQual
>,
684 is_assignable
<_Err
&, _OtherErrQual
>,
686 is_nothrow_constructible
<_Err
, _OtherErrQual
>,
687 is_nothrow_move_constructible
<_Tp
>,
688 is_nothrow_move_constructible
<_Err
>> >::value
;
691 template <class _OtherErr
>
692 requires(__can_assign_from_unexpected
<const _OtherErr
&>)
693 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const unexpected
<_OtherErr
>& __un
) {
694 if (this->__has_val()) {
695 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), __un
.error());
697 this->__unex() = __un
.error();
702 template <class _OtherErr
>
703 requires(__can_assign_from_unexpected
<_OtherErr
>)
704 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(unexpected
<_OtherErr
>&& __un
) {
705 if (this->__has_val()) {
706 __reinit_expected
<unexpect_t
, in_place_t
, _Err
, _Tp
>(this->__val(), std::move(__un
.error()));
708 this->__unex() = std::move(__un
.error());
713 template <class... _Args
>
714 requires is_nothrow_constructible_v
<_Tp
, _Args
...>
715 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& emplace(_Args
&&... __args
) noexcept
{
717 this->__construct(in_place
, std::forward
<_Args
>(__args
)...);
718 return this->__val();
721 template <class _Up
, class... _Args
>
722 requires is_nothrow_constructible_v
<_Tp
, initializer_list
<_Up
>&, _Args
...>
723 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& emplace(initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept
{
725 this->__construct(in_place
, __il
, std::forward
<_Args
>(__args
)...);
726 return this->__val();
730 // [expected.object.swap], swap
731 _LIBCPP_HIDE_FROM_ABI
constexpr void
732 swap(expected
& __rhs
) noexcept(is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_swappable_v
<_Tp
> &&
733 is_nothrow_move_constructible_v
<_Err
> && is_nothrow_swappable_v
<_Err
>)
734 requires(is_swappable_v
<_Tp
> && is_swappable_v
<_Err
> && is_move_constructible_v
<_Tp
> &&
735 is_move_constructible_v
<_Err
> &&
736 (is_nothrow_move_constructible_v
<_Tp
> || is_nothrow_move_constructible_v
<_Err
>))
738 auto __swap_val_unex_impl
= [](expected
& __with_val
, expected
& __with_err
) {
739 if constexpr (is_nothrow_move_constructible_v
<_Err
>) {
740 _Err
__tmp(std::move(__with_err
.__unex()));
741 __with_err
.__destroy();
742 auto __trans
= std::__make_exception_guard([&] { __with_err
.__construct(unexpect
, std::move(__tmp
)); });
743 __with_err
.__construct(in_place
, std::move(__with_val
.__val()));
744 __trans
.__complete();
745 __with_val
.__destroy();
746 __with_val
.__construct(unexpect
, std::move(__tmp
));
748 static_assert(is_nothrow_move_constructible_v
<_Tp
>,
749 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
750 "that it can be reverted to the previous state in case an exception is thrown during swap.");
751 _Tp
__tmp(std::move(__with_val
.__val()));
752 __with_val
.__destroy();
753 auto __trans
= std::__make_exception_guard([&] { __with_val
.__construct(in_place
, std::move(__tmp
)); });
754 __with_val
.__construct(unexpect
, std::move(__with_err
.__unex()));
755 __trans
.__complete();
756 __with_err
.__destroy();
757 __with_err
.__construct(in_place
, std::move(__tmp
));
761 if (this->__has_val()) {
762 if (__rhs
.__has_val()) {
764 swap(this->__val(), __rhs
.__val());
766 __swap_val_unex_impl(*this, __rhs
);
769 if (__rhs
.__has_val()) {
770 __swap_val_unex_impl(__rhs
, *this);
773 swap(this->__unex(), __rhs
.__unex());
778 _LIBCPP_HIDE_FROM_ABI
friend constexpr void swap(expected
& __x
, expected
& __y
) noexcept(noexcept(__x
.swap(__y
)))
779 requires requires
{ __x
.swap(__y
); }
784 // [expected.object.obs], observers
785 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{
786 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
787 this->__has_val(), "expected::operator-> requires the expected to contain a value");
788 return std::addressof(this->__val());
791 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{
792 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
793 this->__has_val(), "expected::operator-> requires the expected to contain a value");
794 return std::addressof(this->__val());
797 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& operator*() const& noexcept
{
798 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
799 this->__has_val(), "expected::operator* requires the expected to contain a value");
800 return this->__val();
803 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() & noexcept
{
804 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
805 this->__has_val(), "expected::operator* requires the expected to contain a value");
806 return this->__val();
809 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
&& operator*() const&& noexcept
{
810 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
811 this->__has_val(), "expected::operator* requires the expected to contain a value");
812 return std::move(this->__val());
815 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
&& operator*() && noexcept
{
816 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
817 this->__has_val(), "expected::operator* requires the expected to contain a value");
818 return std::move(this->__val());
821 _LIBCPP_HIDE_FROM_ABI
constexpr explicit operator bool() const noexcept
{ return this->__has_val(); }
823 _LIBCPP_HIDE_FROM_ABI
constexpr bool has_value() const noexcept
{ return this->__has_val(); }
825 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
& value() const& {
826 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
827 if (!this->__has_val()) {
828 std::__throw_bad_expected_access
<_Err
>(std::as_const(error()));
830 return this->__val();
833 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& value() & {
834 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
835 if (!this->__has_val()) {
836 std::__throw_bad_expected_access
<_Err
>(std::as_const(error()));
838 return this->__val();
841 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
&& value() const&& {
842 static_assert(is_copy_constructible_v
<_Err
> && is_constructible_v
<_Err
, decltype(std::move(error()))>,
843 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
844 if (!this->__has_val()) {
845 std::__throw_bad_expected_access
<_Err
>(std::move(error()));
847 return std::move(this->__val());
850 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
&& value() && {
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()));
856 return std::move(this->__val());
859 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& error() const& noexcept
{
860 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
861 !this->__has_val(), "expected::error requires the expected to contain an error");
862 return this->__unex();
865 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& error() & noexcept
{
866 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
867 !this->__has_val(), "expected::error requires the expected to contain an error");
868 return this->__unex();
871 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
&& error() const&& noexcept
{
872 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
873 !this->__has_val(), "expected::error requires the expected to contain an error");
874 return std::move(this->__unex());
877 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
&& error() && noexcept
{
878 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
879 !this->__has_val(), "expected::error requires the expected to contain an error");
880 return std::move(this->__unex());
884 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
value_or(_Up
&& __v
) const& {
885 static_assert(is_copy_constructible_v
<_Tp
>, "value_type has to be copy constructible");
886 static_assert(is_convertible_v
<_Up
, _Tp
>, "argument has to be convertible to value_type");
887 return this->__has_val() ? this->__val() : static_cast<_Tp
>(std::forward
<_Up
>(__v
));
891 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
value_or(_Up
&& __v
) && {
892 static_assert(is_move_constructible_v
<_Tp
>, "value_type has to be move constructible");
893 static_assert(is_convertible_v
<_Up
, _Tp
>, "argument has to be convertible to value_type");
894 return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp
>(std::forward
<_Up
>(__v
));
897 template <class _Up
= _Err
>
898 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) const& {
899 static_assert(is_copy_constructible_v
<_Err
>, "error_type has to be copy constructible");
900 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
902 return std::forward
<_Up
>(__error
);
906 template <class _Up
= _Err
>
907 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
error_or(_Up
&& __error
) && {
908 static_assert(is_move_constructible_v
<_Err
>, "error_type has to be move constructible");
909 static_assert(is_convertible_v
<_Up
, _Err
>, "argument has to be convertible to error_type");
911 return std::forward
<_Up
>(__error
);
912 return std::move(error());
915 // [expected.void.monadic], monadic
916 template <class _Func
>
917 requires is_constructible_v
<_Err
, _Err
&>
918 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) & {
919 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, _Tp
&>>;
920 static_assert(__is_std_expected
<_Up
>::value
, "The result of f(**this) must be a specialization of std::expected");
921 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
922 "The result of f(**this) must have the same error_type as this expected");
924 return std::invoke(std::forward
<_Func
>(__f
), this->__val());
926 return _Up(unexpect
, error());
929 template <class _Func
>
930 requires is_constructible_v
<_Err
, const _Err
&>
931 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const& {
932 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, const _Tp
&>>;
933 static_assert(__is_std_expected
<_Up
>::value
, "The result of f(**this) must be a specialization of std::expected");
934 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
935 "The result of f(**this) must have the same error_type as this expected");
937 return std::invoke(std::forward
<_Func
>(__f
), this->__val());
939 return _Up(unexpect
, error());
942 template <class _Func
>
943 requires is_constructible_v
<_Err
, _Err
&&>
944 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) && {
945 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, _Tp
&&>>;
947 __is_std_expected
<_Up
>::value
, "The result of f(std::move(**this)) must be a specialization of std::expected");
948 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
949 "The result of f(std::move(**this)) must have the same error_type as this expected");
951 return std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
953 return _Up(unexpect
, std::move(error()));
956 template <class _Func
>
957 requires is_constructible_v
<_Err
, const _Err
&&>
958 _LIBCPP_HIDE_FROM_ABI
constexpr auto and_then(_Func
&& __f
) const&& {
959 using _Up
= remove_cvref_t
<invoke_result_t
<_Func
, const _Tp
&&>>;
961 __is_std_expected
<_Up
>::value
, "The result of f(std::move(**this)) must be a specialization of std::expected");
962 static_assert(is_same_v
<typename
_Up::error_type
, _Err
>,
963 "The result of f(std::move(**this)) must have the same error_type as this expected");
965 return std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
967 return _Up(unexpect
, std::move(error()));
970 template <class _Func
>
971 requires is_constructible_v
<_Tp
, _Tp
&>
972 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) & {
973 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&>>;
974 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
975 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
976 "The result of f(error()) must have the same value_type as this expected");
978 return _Gp(in_place
, this->__val());
980 return std::invoke(std::forward
<_Func
>(__f
), error());
983 template <class _Func
>
984 requires is_constructible_v
<_Tp
, const _Tp
&>
985 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const& {
986 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&>>;
987 static_assert(__is_std_expected
<_Gp
>::value
, "The result of f(error()) must be a specialization of std::expected");
988 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
989 "The result of f(error()) must have the same value_type as this expected");
991 return _Gp(in_place
, this->__val());
993 return std::invoke(std::forward
<_Func
>(__f
), error());
996 template <class _Func
>
997 requires is_constructible_v
<_Tp
, _Tp
&&>
998 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) && {
999 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, _Err
&&>>;
1001 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1002 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1003 "The result of f(std::move(error())) must have the same value_type as this expected");
1005 return _Gp(in_place
, std::move(this->__val()));
1007 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1010 template <class _Func
>
1011 requires is_constructible_v
<_Tp
, const _Tp
&&>
1012 _LIBCPP_HIDE_FROM_ABI
constexpr auto or_else(_Func
&& __f
) const&& {
1013 using _Gp
= remove_cvref_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1015 __is_std_expected
<_Gp
>::value
, "The result of f(std::move(error())) must be a specialization of std::expected");
1016 static_assert(is_same_v
<typename
_Gp::value_type
, _Tp
>,
1017 "The result of f(std::move(error())) must have the same value_type as this expected");
1019 return _Gp(in_place
, std::move(this->__val()));
1021 return std::invoke(std::forward
<_Func
>(__f
), std::move(error()));
1024 template <class _Func
>
1025 requires is_constructible_v
<_Err
, _Err
&>
1026 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) & {
1027 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, _Tp
&>>;
1029 return expected
<_Up
, _Err
>(unexpect
, error());
1031 if constexpr (!is_void_v
<_Up
>) {
1032 return expected
<_Up
, _Err
>(
1033 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), this->__val());
1035 std::invoke(std::forward
<_Func
>(__f
), this->__val());
1036 return expected
<_Up
, _Err
>();
1040 template <class _Func
>
1041 requires is_constructible_v
<_Err
, const _Err
&>
1042 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const& {
1043 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, const _Tp
&>>;
1045 return expected
<_Up
, _Err
>(unexpect
, error());
1047 if constexpr (!is_void_v
<_Up
>) {
1048 return expected
<_Up
, _Err
>(
1049 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), this->__val());
1051 std::invoke(std::forward
<_Func
>(__f
), this->__val());
1052 return expected
<_Up
, _Err
>();
1056 template <class _Func
>
1057 requires is_constructible_v
<_Err
, _Err
&&>
1058 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) && {
1059 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, _Tp
&&>>;
1061 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1063 if constexpr (!is_void_v
<_Up
>) {
1064 return expected
<_Up
, _Err
>(
1065 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(this->__val()));
1067 std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
1068 return expected
<_Up
, _Err
>();
1072 template <class _Func
>
1073 requires is_constructible_v
<_Err
, const _Err
&&>
1074 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform(_Func
&& __f
) const&& {
1075 using _Up
= remove_cv_t
<invoke_result_t
<_Func
, const _Tp
&&>>;
1077 return expected
<_Up
, _Err
>(unexpect
, std::move(error()));
1079 if constexpr (!is_void_v
<_Up
>) {
1080 return expected
<_Up
, _Err
>(
1081 __expected_construct_in_place_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(this->__val()));
1083 std::invoke(std::forward
<_Func
>(__f
), std::move(this->__val()));
1084 return expected
<_Up
, _Err
>();
1088 template <class _Func
>
1089 requires is_constructible_v
<_Tp
, _Tp
&>
1090 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) & {
1091 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&>>;
1092 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1093 "The result of f(error()) must be a valid template argument for unexpected");
1095 return expected
<_Tp
, _Gp
>(in_place
, this->__val());
1097 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1100 template <class _Func
>
1101 requires is_constructible_v
<_Tp
, const _Tp
&>
1102 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const& {
1103 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&>>;
1104 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1105 "The result of f(error()) must be a valid template argument for unexpected");
1107 return expected
<_Tp
, _Gp
>(in_place
, this->__val());
1109 return expected
<_Tp
, _Gp
>(__expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), error());
1112 template <class _Func
>
1113 requires is_constructible_v
<_Tp
, _Tp
&&>
1114 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) && {
1115 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, _Err
&&>>;
1116 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1117 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1119 return expected
<_Tp
, _Gp
>(in_place
, std::move(this->__val()));
1121 return expected
<_Tp
, _Gp
>(
1122 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1125 template <class _Func
>
1126 requires is_constructible_v
<_Tp
, const _Tp
&&>
1127 _LIBCPP_HIDE_FROM_ABI
constexpr auto transform_error(_Func
&& __f
) const&& {
1128 using _Gp
= remove_cv_t
<invoke_result_t
<_Func
, const _Err
&&>>;
1129 static_assert(__valid_std_unexpected
<_Gp
>::value
,
1130 "The result of f(std::move(error())) must be a valid template argument for unexpected");
1132 return expected
<_Tp
, _Gp
>(in_place
, std::move(this->__val()));
1134 return expected
<_Tp
, _Gp
>(
1135 __expected_construct_unexpected_from_invoke_tag
{}, std::forward
<_Func
>(__f
), std::move(error()));
1138 // [expected.object.eq], equality operators
1139 template <class _T2
, class _E2
>
1140 requires(!is_void_v
<_T2
>)
1141 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const expected
<_T2
, _E2
>& __y
) {
1142 if (__x
.__has_val() != __y
.__has_val()) {
1145 if (__x
.__has_val()) {
1146 return __x
.__val() == __y
.__val();
1148 return __x
.__unex() == __y
.__unex();
1153 template <class _T2
>
1154 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const _T2
& __v
) {
1155 return __x
.__has_val() && static_cast<bool>(__x
.__val() == __v
);
1158 template <class _E2
>
1159 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const expected
& __x
, const unexpected
<_E2
>& __e
) {
1160 return !__x
.__has_val() && static_cast<bool>(__x
.__unex() == __e
.error());
1164 template <class _Err
>
1165 class __expected_void_base
{
1166 struct __empty_t
{};
1167 // use named union because [[no_unique_address]] cannot be applied to an unnamed union,
1168 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and
1169 // it's not clear that it's implementable, given that the function is allowed to clobber
1170 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.
1172 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&) = delete;
1173 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(const __union_t
&)
1174 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1176 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&) = delete;
1177 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t(__union_t
&&)
1178 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1180 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(const __union_t
&) = delete;
1181 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& operator=(__union_t
&&) = delete;
1183 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(in_place_t
) : __empty_() {}
1185 template <class... _Args
>
1186 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(unexpect_t
, _Args
&&... __args
)
1187 : __unex_(std::forward
<_Args
>(__args
)...) {}
1189 template <class _Func
, class... _Args
>
1190 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __union_t(
1191 __expected_construct_unexpected_from_invoke_tag
, _Func
&& __f
, _Args
&&... __args
)
1192 : __unex_(std::invoke(std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...)) {}
1194 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
1195 requires(is_trivially_destructible_v
<_Err
>)
1198 // __repr's destructor handles this
1199 _LIBCPP_HIDE_FROM_ABI
constexpr ~__union_t()
1200 requires(!is_trivially_destructible_v
<_Err
>)
1203 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_
;
1204 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_
;
1207 static constexpr bool __put_flag_in_tail
= __fits_in_tail_padding
<__union_t
, bool>;
1208 static constexpr bool __allow_reusing_expected_tail_padding
= !__put_flag_in_tail
;
1211 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr() = delete;
1213 template <class... _Args
>
1214 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(in_place_t __tag
) : __union_(in_place
, __tag
), __has_val_(true) {}
1216 template <class... _Args
>
1217 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(unexpect_t __tag
, _Args
&&... __args
)
1218 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
1220 template <class... _Args
>
1221 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag
,
1223 : __union_(in_place
, __tag
, std::forward
<_Args
>(__args
)...), __has_val_(false) {}
1225 template <class _OtherUnion
>
1226 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __repr(bool __has_val
, _OtherUnion
&& __other
)
1227 requires(__allow_reusing_expected_tail_padding
)
1228 : __union_(__conditional_no_unique_address_invoke_tag
{},
1229 [&] { return __make_union(__has_val
, std::forward
<_OtherUnion
>(__other
)); }),
1230 __has_val_(__has_val
) {}
1232 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&) = delete;
1233 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(const __repr
&)
1234 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1236 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&) = delete;
1237 _LIBCPP_HIDE_FROM_ABI
constexpr __repr(__repr
&&)
1238 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1241 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(const __repr
&) = delete;
1242 _LIBCPP_HIDE_FROM_ABI
constexpr __repr
& operator=(__repr
&&) = delete;
1244 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
1245 requires(is_trivially_destructible_v
<_Err
>)
1248 _LIBCPP_HIDE_FROM_ABI
constexpr ~__repr()
1249 requires(!is_trivially_destructible_v
<_Err
>)
1251 __destroy_union_member();
1254 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
1255 requires(__allow_reusing_expected_tail_padding
&& is_trivially_destructible_v
<_Err
>)
1257 std::destroy_at(&__union_
.__v
);
1260 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union()
1261 requires(__allow_reusing_expected_tail_padding
&& !is_trivially_destructible_v
<_Err
>)
1263 __destroy_union_member();
1264 std::destroy_at(&__union_
.__v
);
1267 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(in_place_t
)
1268 requires(__allow_reusing_expected_tail_padding
)
1270 std::construct_at(&__union_
.__v
, in_place
);
1274 template <class... _Args
>
1275 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct_union(unexpect_t
, _Args
&&... __args
)
1276 requires(__allow_reusing_expected_tail_padding
)
1278 std::construct_at(&__union_
.__v
, unexpect
, std::forward
<_Args
>(__args
)...);
1284 friend class __expected_void_base
;
1286 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy_union_member()
1287 requires(!is_trivially_destructible_v
<_Err
>)
1290 std::destroy_at(std::addressof(__union_
.__v
.__unex_
));
1293 template <class _OtherUnion
>
1294 _LIBCPP_HIDE_FROM_ABI
static constexpr __union_t
__make_union(bool __has_val
, _OtherUnion
&& __other
)
1295 requires(__allow_reusing_expected_tail_padding
)
1298 return __union_t(in_place
);
1300 return __union_t(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
1303 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__put_flag_in_tail
, __union_t
> __union_
;
1304 _LIBCPP_NO_UNIQUE_ADDRESS
bool __has_val_
;
1307 template <class _OtherUnion
>
1308 _LIBCPP_HIDE_FROM_ABI
static constexpr __repr
__make_repr(bool __has_val
, _OtherUnion
&& __other
)
1309 requires(__put_flag_in_tail
)
1312 return __repr(in_place
);
1314 return __repr(unexpect
, std::forward
<_OtherUnion
>(__other
).__unex_
);
1318 template <class... _Args
>
1319 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_void_base(_Args
&&... __args
)
1320 : __repr_(in_place
, std::forward
<_Args
>(__args
)...) {}
1322 template <class _OtherUnion
>
1323 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __expected_void_base(bool __has_val
, _OtherUnion
&& __other
)
1324 requires(__put_flag_in_tail
)
1325 : __repr_(__conditional_no_unique_address_invoke_tag
{},
1326 [&] { return __make_repr(__has_val
, std::forward
<_OtherUnion
>(__other
)); }) {}
1328 _LIBCPP_HIDE_FROM_ABI
constexpr void __destroy() {
1329 if constexpr (__put_flag_in_tail
)
1330 std::destroy_at(&__repr_
.__v
);
1332 __repr_
.__v
.__destroy_union();
1335 template <class _Tag
, class... _Args
>
1336 _LIBCPP_HIDE_FROM_ABI
constexpr void __construct(_Tag __tag
, _Args
&&... __args
) {
1337 if constexpr (__put_flag_in_tail
)
1338 std::construct_at(&__repr_
.__v
, __tag
, std::forward
<_Args
>(__args
)...);
1340 __repr_
.__v
.__construct_union(__tag
, std::forward
<_Args
>(__args
)...);
1343 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_val() const { return __repr_
.__v
.__has_val_
; }
1344 _LIBCPP_HIDE_FROM_ABI
constexpr __union_t
& __union() { return __repr_
.__v
.__union_
.__v
; }
1345 _LIBCPP_HIDE_FROM_ABI
constexpr const __union_t
& __union() const { return __repr_
.__v
.__union_
.__v
; }
1346 _LIBCPP_HIDE_FROM_ABI
constexpr _Err
& __unex() { return __repr_
.__v
.__union_
.__v
.__unex_
; }
1347 _LIBCPP_HIDE_FROM_ABI
constexpr const _Err
& __unex() const { return __repr_
.__v
.__union_
.__v
.__unex_
; }
1350 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address
<__allow_reusing_expected_tail_padding
, __repr
> __repr_
;
1353 template <class _Tp
, class _Err
>
1354 requires is_void_v
<_Tp
>
1355 class expected
<_Tp
, _Err
> : private __expected_void_base
<_Err
> {
1356 static_assert(__valid_std_unexpected
<_Err
>::value
,
1357 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "
1358 "valid argument for unexpected<E> is ill-formed");
1360 template <class, class>
1361 friend class expected
;
1363 template <class _Up
, class _OtherErr
, class _OtherErrQual
>
1364 using __can_convert
=
1366 is_constructible
<_Err
, _OtherErrQual
>,
1367 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>&>>,
1368 _Not
<is_constructible
<unexpected
<_Err
>, expected
<_Up
, _OtherErr
>>>,
1369 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>&>>,
1370 _Not
<is_constructible
<unexpected
<_Err
>, const expected
<_Up
, _OtherErr
>>>>;
1372 using __base
= __expected_void_base
<_Err
>;
1375 using value_type
= _Tp
;
1376 using error_type
= _Err
;
1377 using unexpected_type
= unexpected
<_Err
>;
1379 template <class _Up
>
1380 using rebind
= expected
<_Up
, error_type
>;
1382 // [expected.void.ctor], constructors
1383 _LIBCPP_HIDE_FROM_ABI
constexpr expected() noexcept
: __base(in_place
) {}
1385 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&) = delete;
1387 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
&)
1388 requires(is_copy_constructible_v
<_Err
> && is_trivially_copy_constructible_v
<_Err
>)
1391 _LIBCPP_HIDE_FROM_ABI
constexpr expected(const expected
& __rhs
) noexcept(
1392 is_nothrow_copy_constructible_v
<_Err
>) // strengthened
1393 requires(is_copy_constructible_v
<_Err
> && !is_trivially_copy_constructible_v
<_Err
>)
1394 : __base(__rhs
.__has_val(), __rhs
.__union()) {}
1396 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&&)
1397 requires(is_move_constructible_v
<_Err
> && is_trivially_move_constructible_v
<_Err
>)
1400 _LIBCPP_HIDE_FROM_ABI
constexpr expected(expected
&& __rhs
) noexcept(is_nothrow_move_constructible_v
<_Err
>)
1401 requires(is_move_constructible_v
<_Err
> && !is_trivially_move_constructible_v
<_Err
>)
1402 : __base(__rhs
.__has_val(), std::move(__rhs
.__union())) {}
1404 template <class _Up
, class _OtherErr
>
1405 requires __can_convert
<_Up
, _OtherErr
, const _OtherErr
&>::value
1406 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>)
1407 expected(const expected
<_Up
, _OtherErr
>& __rhs
) noexcept(
1408 is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
1409 : __base(__rhs
.__has_val(), __rhs
.__union()) {}
1411 template <class _Up
, class _OtherErr
>
1412 requires __can_convert
<_Up
, _OtherErr
, _OtherErr
>::value
1413 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
1414 expected(expected
<_Up
, _OtherErr
>&& __rhs
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
1415 : __base(__rhs
.__has_val(), std::move(__rhs
.__union())) {}
1417 template <class _OtherErr
>
1418 requires is_constructible_v
<_Err
, const _OtherErr
&>
1419 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<const _OtherErr
&, _Err
>) expected(
1420 const unexpected
<_OtherErr
>& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, const _OtherErr
&>) // strengthened
1421 : __base(unexpect
, __unex
.error()) {}
1423 template <class _OtherErr
>
1424 requires is_constructible_v
<_Err
, _OtherErr
>
1425 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherErr
, _Err
>)
1426 expected(unexpected
<_OtherErr
>&& __unex
) noexcept(is_nothrow_constructible_v
<_Err
, _OtherErr
>) // strengthened
1427 : __base(unexpect
, std::move(__unex
.error())) {}
1429 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(in_place_t
) noexcept
: __base(in_place
) {}
1431 template <class... _Args
>
1432 requires is_constructible_v
<_Err
, _Args
...>
1433 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, _Args
&&... __args
) noexcept(
1434 is_nothrow_constructible_v
<_Err
, _Args
...>) // strengthened
1435 : __base(unexpect
, std::forward
<_Args
>(__args
)...) {}
1437 template <class _Up
, class... _Args
>
1438 requires is_constructible_v
< _Err
, initializer_list
<_Up
>&, _Args
... >
1439 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(unexpect_t
, initializer_list
<_Up
> __il
, _Args
&&... __args
) noexcept(
1440 is_nothrow_constructible_v
<_Err
, initializer_list
<_Up
>&, _Args
...>) // strengthened
1441 : __base(unexpect
, __il
, std::forward
<_Args
>(__args
)...) {}
1444 template <class _Func
, class... _Args
>
1445 _LIBCPP_HIDE_FROM_ABI
constexpr explicit expected(
1446 __expected_construct_unexpected_from_invoke_tag __tag
, _Func
&& __f
, _Args
&&... __args
)
1447 : __base(__tag
, std::forward
<_Func
>(__f
), std::forward
<_Args
>(__args
)...) {}
1450 // [expected.void.dtor], destructor
1452 _LIBCPP_HIDE_FROM_ABI
constexpr ~expected() = default;
1455 template <class... _Args
>
1456 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(unexpect_t
, _Args
&&... __args
) {
1457 _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");
1460 auto __trans
= std::__make_exception_guard([&] { this->__construct(in_place
); });
1461 this->__construct(unexpect
, std::forward
<_Args
>(__args
)...);
1462 __trans
.__complete();
1465 _LIBCPP_HIDE_FROM_ABI
constexpr void __reinit_expected(in_place_t
) {
1466 _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");
1469 this->__construct(in_place
);
1473 // [expected.void.assign], assignment
1474 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
&) = delete;
1476 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(const expected
& __rhs
) noexcept(
1477 is_nothrow_copy_assignable_v
<_Err
> && is_nothrow_copy_constructible_v
<_Err
>) // strengthened
1478 requires(is_copy_assignable_v
<_Err
> && is_copy_constructible_v
<_Err
>)
1480 if (this->__has_val()) {
1481 if (!__rhs
.__has_val()) {
1482 __reinit_expected(unexpect
, __rhs
.__unex());
1485 if (__rhs
.__has_val()) {
1486 __reinit_expected(in_place
);
1488 this->__unex() = __rhs
.__unex();
1494 _LIBCPP_HIDE_FROM_ABI
constexpr expected
& operator=(expected
&&) = delete;
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