File indexing completed on 2026-05-03 08:13:47
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___EXPECTED_EXPECTED_H
0010 #define _LIBCPP___EXPECTED_EXPECTED_H
0011
0012 #include <__assert>
0013 #include <__config>
0014 #include <__expected/bad_expected_access.h>
0015 #include <__expected/unexpect.h>
0016 #include <__expected/unexpected.h>
0017 #include <__functional/invoke.h>
0018 #include <__memory/addressof.h>
0019 #include <__memory/construct_at.h>
0020 #include <__type_traits/conditional.h>
0021 #include <__type_traits/conjunction.h>
0022 #include <__type_traits/disjunction.h>
0023 #include <__type_traits/integral_constant.h>
0024 #include <__type_traits/invoke.h>
0025 #include <__type_traits/is_assignable.h>
0026 #include <__type_traits/is_constructible.h>
0027 #include <__type_traits/is_convertible.h>
0028 #include <__type_traits/is_function.h>
0029 #include <__type_traits/is_nothrow_assignable.h>
0030 #include <__type_traits/is_nothrow_constructible.h>
0031 #include <__type_traits/is_reference.h>
0032 #include <__type_traits/is_same.h>
0033 #include <__type_traits/is_swappable.h>
0034 #include <__type_traits/is_trivially_constructible.h>
0035 #include <__type_traits/is_trivially_destructible.h>
0036 #include <__type_traits/is_trivially_relocatable.h>
0037 #include <__type_traits/is_void.h>
0038 #include <__type_traits/lazy.h>
0039 #include <__type_traits/negation.h>
0040 #include <__type_traits/remove_cv.h>
0041 #include <__type_traits/remove_cvref.h>
0042 #include <__utility/as_const.h>
0043 #include <__utility/exception_guard.h>
0044 #include <__utility/forward.h>
0045 #include <__utility/in_place.h>
0046 #include <__utility/move.h>
0047 #include <__utility/swap.h>
0048 #include <__verbose_abort>
0049 #include <initializer_list>
0050
0051 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0052 # pragma GCC system_header
0053 #endif
0054
0055 _LIBCPP_PUSH_MACROS
0056 #include <__undef_macros>
0057
0058 #if _LIBCPP_STD_VER >= 23
0059
0060 _LIBCPP_BEGIN_NAMESPACE_STD
0061
0062 template <class _Tp, class _Err>
0063 class expected;
0064
0065 template <class _Tp>
0066 struct __is_std_expected : false_type {};
0067
0068 template <class _Tp, class _Err>
0069 struct __is_std_expected<expected<_Tp, _Err>> : true_type {};
0070
0071 struct __expected_construct_in_place_from_invoke_tag {};
0072 struct __expected_construct_unexpected_from_invoke_tag {};
0073
0074 template <class _Err, class _Arg>
0075 _LIBCPP_HIDE_FROM_ABI void __throw_bad_expected_access(_Arg&& __arg) {
0076 # if _LIBCPP_HAS_EXCEPTIONS
0077 throw bad_expected_access<_Err>(std::forward<_Arg>(__arg));
0078 # else
0079 (void)__arg;
0080 _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");
0081 # endif
0082 }
0083
0084
0085
0086
0087
0088
0089
0090 struct __conditional_no_unique_address_invoke_tag {};
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 template <bool _NoUnique, class _Tp>
0103 struct __conditional_no_unique_address;
0104
0105 template <class _Tp>
0106 struct __conditional_no_unique_address<true, _Tp> {
0107 template <class... _Args>
0108 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
0109 : __v(std::forward<_Args>(__args)...) {}
0110
0111 template <class _Func, class... _Args>
0112 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
0113 __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
0114 : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0115
0116 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v;
0117 };
0118
0119 template <class _Tp>
0120 struct __conditional_no_unique_address<false, _Tp> {
0121 template <class... _Args>
0122 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
0123 : __v(std::forward<_Args>(__args)...) {}
0124
0125 template <class _Func, class... _Args>
0126 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
0127 __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
0128 : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0129
0130 _Tp __v;
0131 };
0132
0133
0134
0135 template <class _First, class _Second>
0136 inline constexpr bool __fits_in_tail_padding = []() {
0137 struct __x {
0138 _LIBCPP_NO_UNIQUE_ADDRESS _First __first;
0139 _LIBCPP_NO_UNIQUE_ADDRESS _Second __second;
0140 };
0141 return sizeof(__x) == sizeof(_First);
0142 }();
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211 template <class _Tp, class _Err>
0212 class __expected_base {
0213
0214
0215
0216
0217 union __union_t {
0218 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
0219 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
0220 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0221 is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
0222 = default;
0223 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
0224 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
0225 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0226 is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
0227 = default;
0228 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
0229 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;
0230
0231 template <class... _Args>
0232 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t, _Args&&... __args)
0233 : __val_(std::forward<_Args>(__args)...) {}
0234
0235 template <class... _Args>
0236 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
0237 : __unex_(std::forward<_Args>(__args)...) {}
0238
0239 template <class _Func, class... _Args>
0240 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
0241 std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args)
0242 : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0243
0244 template <class _Func, class... _Args>
0245 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
0246 std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
0247 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0248
0249 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
0250 requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
0251 = default;
0252
0253
0254 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {}
0255
0256 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
0257 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
0258 };
0259
0260 static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;
0261 static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
0262
0263 struct __repr {
0264 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
0265
0266 template <class... _Args>
0267 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag, _Args&&... __args)
0268 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
0269
0270 template <class... _Args>
0271 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
0272 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
0273
0274 template <class... _Args>
0275 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag,
0276 _Args&&... __args)
0277 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
0278
0279 template <class... _Args>
0280 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
0281 _Args&&... __args)
0282 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
0283
0284
0285
0286
0287
0288
0289 template <class _OtherUnion>
0290 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
0291 requires(__allow_reusing_expected_tail_padding)
0292 : __union_(__conditional_no_unique_address_invoke_tag{},
0293 [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
0294 __has_val_(__has_val) {}
0295
0296 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
0297 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
0298 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0299 is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
0300 = default;
0301 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
0302 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
0303 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0304 is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
0305 = default;
0306
0307 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
0308 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
0309
0310 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
0311 requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
0312 = default;
0313
0314 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
0315 requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
0316 {
0317 __destroy_union_member();
0318 }
0319
0320 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
0321 requires(__allow_reusing_expected_tail_padding &&
0322 (is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>))
0323 {
0324
0325
0326 std::destroy_at(&__union_.__v);
0327 }
0328
0329 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
0330 requires(__allow_reusing_expected_tail_padding &&
0331 (!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>))
0332 {
0333 __destroy_union_member();
0334 std::destroy_at(&__union_.__v);
0335 }
0336
0337 template <class... _Args>
0338 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t, _Args&&... __args)
0339 requires(__allow_reusing_expected_tail_padding)
0340 {
0341 std::construct_at(&__union_.__v, in_place, std::forward<_Args>(__args)...);
0342 __has_val_ = true;
0343 }
0344
0345 template <class... _Args>
0346 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
0347 requires(__allow_reusing_expected_tail_padding)
0348 {
0349 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
0350 __has_val_ = false;
0351 }
0352
0353 private:
0354 template <class, class>
0355 friend class __expected_base;
0356
0357 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
0358 requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
0359 {
0360 if (__has_val_) {
0361 std::destroy_at(std::addressof(__union_.__v.__val_));
0362 } else {
0363 std::destroy_at(std::addressof(__union_.__v.__unex_));
0364 }
0365 }
0366
0367 template <class _OtherUnion>
0368 _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
0369 requires(__allow_reusing_expected_tail_padding)
0370 {
0371 if (__has_val)
0372 return __union_t(in_place, std::forward<_OtherUnion>(__other).__val_);
0373 else
0374 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
0375 }
0376
0377 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
0378 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
0379 };
0380
0381 template <class _OtherUnion>
0382 _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
0383 requires(__put_flag_in_tail)
0384 {
0385 if (__has_val)
0386 return __repr(in_place, std::forward<_OtherUnion>(__other).__val_);
0387 else
0388 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
0389 }
0390
0391 protected:
0392 template <class... _Args>
0393 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(_Args&&... __args)
0394 : __repr_(in_place, std::forward<_Args>(__args)...) {}
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415 template <class _OtherUnion>
0416 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(bool __has_val, _OtherUnion&& __other)
0417 requires(__put_flag_in_tail)
0418 : __repr_(__conditional_no_unique_address_invoke_tag{},
0419 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
0420
0421 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
0422 if constexpr (__put_flag_in_tail)
0423 std::destroy_at(&__repr_.__v);
0424 else
0425 __repr_.__v.__destroy_union();
0426 }
0427
0428 template <class _Tag, class... _Args>
0429 _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
0430 if constexpr (__put_flag_in_tail)
0431 std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
0432 else
0433 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
0434 }
0435
0436 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
0437 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
0438 _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
0439 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __val() { return __repr_.__v.__union_.__v.__val_; }
0440 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& __val() const { return __repr_.__v.__union_.__v.__val_; }
0441 _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
0442 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
0443
0444 private:
0445 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
0446 };
0447
0448 template <class _Tp, class _Err>
0449 class expected : private __expected_base<_Tp, _Err> {
0450 static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> &&
0451 !is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value &&
0452 __valid_std_unexpected<_Err>::value,
0453 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
0454 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
0455 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
0456 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
0457 "template argument for unexpected is ill-formed.");
0458
0459 template <class _Up, class _OtherErr>
0460 friend class expected;
0461
0462 using __base _LIBCPP_NODEBUG = __expected_base<_Tp, _Err>;
0463
0464 public:
0465 using value_type = _Tp;
0466 using error_type = _Err;
0467 using unexpected_type = unexpected<_Err>;
0468
0469 using __trivially_relocatable _LIBCPP_NODEBUG =
0470 __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,
0471 expected,
0472 void>;
0473
0474 template <class _Up>
0475 using rebind = expected<_Up, error_type>;
0476
0477
0478 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>)
0479 requires is_default_constructible_v<_Tp>
0480 : __base(in_place) {}
0481
0482 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
0483
0484 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
0485 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&
0486 is_trivially_copy_constructible_v<_Err>)
0487 = default;
0488
0489 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
0490 is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>)
0491 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0492 !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
0493 : __base(__other.__has_val(), __other.__union()) {}
0494
0495 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
0496 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&
0497 is_trivially_move_constructible_v<_Err>)
0498 = default;
0499
0500 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(
0501 is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)
0502 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0503 !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))
0504 : __base(__other.__has_val(), std::move(__other.__union())) {}
0505
0506 private:
0507 template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>
0508 using __can_convert _LIBCPP_NODEBUG = _And<
0509 is_constructible<_Tp, _UfQual>,
0510 is_constructible<_Err, _OtherErrQual>,
0511 _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
0512 _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>,
0513 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
0514 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
0515 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
0516 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
0517 _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>,
0518 _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>,
0519 _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>,
0520 _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>,
0521 true_type>,
0522 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
0523 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
0524 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
0525 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >;
0526
0527 template <class _Func, class... _Args>
0528 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
0529 std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
0530 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
0531
0532 template <class _Func, class... _Args>
0533 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
0534 std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
0535 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
0536
0537 public:
0538 template <class _Up, class _OtherErr>
0539 requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
0540 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> ||
0541 !is_convertible_v<const _OtherErr&, _Err>)
0542 expected(const expected<_Up, _OtherErr>& __other) noexcept(
0543 is_nothrow_constructible_v<_Tp, const _Up&> &&
0544 is_nothrow_constructible_v<_Err, const _OtherErr&>)
0545 : __base(__other.__has_val(), __other.__union()) {}
0546
0547 template <class _Up, class _OtherErr>
0548 requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value
0549 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>)
0550 expected(expected<_Up, _OtherErr>&& __other) noexcept(
0551 is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>)
0552 : __base(__other.__has_val(), std::move(__other.__union())) {}
0553
0554 template <class _Up = _Tp>
0555 requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&
0556 is_constructible_v<_Tp, _Up> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
0557 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))
0558 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)
0559 expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>)
0560 : __base(in_place, std::forward<_Up>(__u)) {}
0561
0562 template <class _OtherErr>
0563 requires is_constructible_v<_Err, const _OtherErr&>
0564 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
0565 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>)
0566 : __base(unexpect, __unex.error()) {}
0567
0568 template <class _OtherErr>
0569 requires is_constructible_v<_Err, _OtherErr>
0570 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
0571 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>)
0572 : __base(unexpect, std::move(__unex.error())) {}
0573
0574 template <class... _Args>
0575 requires is_constructible_v<_Tp, _Args...>
0576 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(
0577 is_nothrow_constructible_v<_Tp, _Args...>)
0578 : __base(in_place, std::forward<_Args>(__args)...) {}
0579
0580 template <class _Up, class... _Args>
0581 requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >
0582 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
0583 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
0584 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
0585
0586 template <class... _Args>
0587 requires is_constructible_v<_Err, _Args...>
0588 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
0589 is_nothrow_constructible_v<_Err, _Args...>)
0590 : __base(unexpect, std::forward<_Args>(__args)...) {}
0591
0592 template <class _Up, class... _Args>
0593 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
0594 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
0595 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>)
0596 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
0597
0598
0599
0600 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
0601
0602 private:
0603 template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>
0604 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {
0605 if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {
0606 this->__destroy();
0607 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
0608 } else if constexpr (is_nothrow_move_constructible_v<_T1>) {
0609 _T1 __tmp(std::forward<_Args>(__args)...);
0610 this->__destroy();
0611 this->__construct(_Tag{}, std::move(__tmp));
0612 } else {
0613 static_assert(
0614 is_nothrow_move_constructible_v<_T2>,
0615 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
0616 "be reverted to the previous state in case an exception is thrown during the assignment.");
0617 _T2 __tmp(std::move(__oldval));
0618 this->__destroy();
0619 auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });
0620 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
0621 __trans.__complete();
0622 }
0623 }
0624
0625 public:
0626
0627 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
0628
0629 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
0630 is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&
0631 is_nothrow_copy_constructible_v<_Err>)
0632 requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&
0633 is_copy_constructible_v<_Err> &&
0634 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0635 {
0636 if (this->__has_val() && __rhs.__has_val()) {
0637 this->__val() = __rhs.__val();
0638 } else if (this->__has_val()) {
0639 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());
0640 } else if (__rhs.__has_val()) {
0641 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());
0642 } else {
0643 this->__unex() = __rhs.__unex();
0644 }
0645 return *this;
0646 }
0647
0648 _LIBCPP_HIDE_FROM_ABI constexpr expected&
0649 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&
0650 is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
0651 requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&
0652 is_move_assignable_v<_Err> &&
0653 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0654 {
0655 if (this->__has_val() && __rhs.__has_val()) {
0656 this->__val() = std::move(__rhs.__val());
0657 } else if (this->__has_val()) {
0658 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));
0659 } else if (__rhs.__has_val()) {
0660 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));
0661 } else {
0662 this->__unex() = std::move(__rhs.__unex());
0663 }
0664 return *this;
0665 }
0666
0667 template <class _Up = _Tp>
0668 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)
0669 requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
0670 is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&
0671 (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||
0672 is_nothrow_move_constructible_v<_Err>))
0673 {
0674 if (this->__has_val()) {
0675 this->__val() = std::forward<_Up>(__v);
0676 } else {
0677 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));
0678 }
0679 return *this;
0680 }
0681
0682 private:
0683 template <class _OtherErrQual>
0684 static constexpr bool __can_assign_from_unexpected =
0685 _And< is_constructible<_Err, _OtherErrQual>,
0686 is_assignable<_Err&, _OtherErrQual>,
0687 _Lazy<_Or,
0688 is_nothrow_constructible<_Err, _OtherErrQual>,
0689 is_nothrow_move_constructible<_Tp>,
0690 is_nothrow_move_constructible<_Err>> >::value;
0691
0692 public:
0693 template <class _OtherErr>
0694 requires(__can_assign_from_unexpected<const _OtherErr&>)
0695 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
0696 if (this->__has_val()) {
0697 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error());
0698 } else {
0699 this->__unex() = __un.error();
0700 }
0701 return *this;
0702 }
0703
0704 template <class _OtherErr>
0705 requires(__can_assign_from_unexpected<_OtherErr>)
0706 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
0707 if (this->__has_val()) {
0708 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error()));
0709 } else {
0710 this->__unex() = std::move(__un.error());
0711 }
0712 return *this;
0713 }
0714
0715 template <class... _Args>
0716 requires is_nothrow_constructible_v<_Tp, _Args...>
0717 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept {
0718 this->__destroy();
0719 this->__construct(in_place, std::forward<_Args>(__args)...);
0720 return this->__val();
0721 }
0722
0723 template <class _Up, class... _Args>
0724 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
0725 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept {
0726 this->__destroy();
0727 this->__construct(in_place, __il, std::forward<_Args>(__args)...);
0728 return this->__val();
0729 }
0730
0731 public:
0732
0733 _LIBCPP_HIDE_FROM_ABI constexpr void
0734 swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> &&
0735 is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
0736 requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> &&
0737 is_move_constructible_v<_Err> &&
0738 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0739 {
0740 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
0741 if constexpr (is_nothrow_move_constructible_v<_Err>) {
0742 _Err __tmp(std::move(__with_err.__unex()));
0743 __with_err.__destroy();
0744 auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); });
0745 __with_err.__construct(in_place, std::move(__with_val.__val()));
0746 __trans.__complete();
0747 __with_val.__destroy();
0748 __with_val.__construct(unexpect, std::move(__tmp));
0749 } else {
0750 static_assert(is_nothrow_move_constructible_v<_Tp>,
0751 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
0752 "that it can be reverted to the previous state in case an exception is thrown during swap.");
0753 _Tp __tmp(std::move(__with_val.__val()));
0754 __with_val.__destroy();
0755 auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); });
0756 __with_val.__construct(unexpect, std::move(__with_err.__unex()));
0757 __trans.__complete();
0758 __with_err.__destroy();
0759 __with_err.__construct(in_place, std::move(__tmp));
0760 }
0761 };
0762
0763 if (this->__has_val()) {
0764 if (__rhs.__has_val()) {
0765 using std::swap;
0766 swap(this->__val(), __rhs.__val());
0767 } else {
0768 __swap_val_unex_impl(*this, __rhs);
0769 }
0770 } else {
0771 if (__rhs.__has_val()) {
0772 __swap_val_unex_impl(__rhs, *this);
0773 } else {
0774 using std::swap;
0775 swap(this->__unex(), __rhs.__unex());
0776 }
0777 }
0778 }
0779
0780 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))
0781 requires requires { __x.swap(__y); }
0782 {
0783 __x.swap(__y);
0784 }
0785
0786
0787 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept {
0788 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0789 this->__has_val(), "expected::operator-> requires the expected to contain a value");
0790 return std::addressof(this->__val());
0791 }
0792
0793 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept {
0794 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0795 this->__has_val(), "expected::operator-> requires the expected to contain a value");
0796 return std::addressof(this->__val());
0797 }
0798
0799 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
0800 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0801 this->__has_val(), "expected::operator* requires the expected to contain a value");
0802 return this->__val();
0803 }
0804
0805 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
0806 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0807 this->__has_val(), "expected::operator* requires the expected to contain a value");
0808 return this->__val();
0809 }
0810
0811 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
0812 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0813 this->__has_val(), "expected::operator* requires the expected to contain a value");
0814 return std::move(this->__val());
0815 }
0816
0817 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
0818 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0819 this->__has_val(), "expected::operator* requires the expected to contain a value");
0820 return std::move(this->__val());
0821 }
0822
0823 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
0824
0825 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
0826
0827 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
0828 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0829 if (!this->__has_val()) {
0830 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
0831 }
0832 return this->__val();
0833 }
0834
0835 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
0836 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0837 if (!this->__has_val()) {
0838 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
0839 }
0840 return this->__val();
0841 }
0842
0843 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& {
0844 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
0845 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
0846 if (!this->__has_val()) {
0847 std::__throw_bad_expected_access<_Err>(std::move(error()));
0848 }
0849 return std::move(this->__val());
0850 }
0851
0852 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
0853 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
0854 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
0855 if (!this->__has_val()) {
0856 std::__throw_bad_expected_access<_Err>(std::move(error()));
0857 }
0858 return std::move(this->__val());
0859 }
0860
0861 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {
0862 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0863 !this->__has_val(), "expected::error requires the expected to contain an error");
0864 return this->__unex();
0865 }
0866
0867 _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {
0868 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0869 !this->__has_val(), "expected::error requires the expected to contain an error");
0870 return this->__unex();
0871 }
0872
0873 _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {
0874 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0875 !this->__has_val(), "expected::error requires the expected to contain an error");
0876 return std::move(this->__unex());
0877 }
0878
0879 _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
0880 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0881 !this->__has_val(), "expected::error requires the expected to contain an error");
0882 return std::move(this->__unex());
0883 }
0884
0885 template <class _Up>
0886 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
0887 static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible");
0888 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
0889 return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v));
0890 }
0891
0892 template <class _Up>
0893 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
0894 static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible");
0895 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
0896 return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v));
0897 }
0898
0899 template <class _Up = _Err>
0900 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
0901 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0902 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
0903 if (has_value())
0904 return std::forward<_Up>(__error);
0905 return error();
0906 }
0907
0908 template <class _Up = _Err>
0909 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
0910 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
0911 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
0912 if (has_value())
0913 return std::forward<_Up>(__error);
0914 return std::move(error());
0915 }
0916
0917
0918 template <class _Func>
0919 requires is_constructible_v<_Err, _Err&>
0920 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
0921 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;
0922 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected");
0923 static_assert(is_same_v<typename _Up::error_type, _Err>,
0924 "The result of f(value()) must have the same error_type as this expected");
0925 if (has_value()) {
0926 return std::invoke(std::forward<_Func>(__f), this->__val());
0927 }
0928 return _Up(unexpect, error());
0929 }
0930
0931 template <class _Func>
0932 requires is_constructible_v<_Err, const _Err&>
0933 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
0934 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>;
0935 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected");
0936 static_assert(is_same_v<typename _Up::error_type, _Err>,
0937 "The result of f(value()) must have the same error_type as this expected");
0938 if (has_value()) {
0939 return std::invoke(std::forward<_Func>(__f), this->__val());
0940 }
0941 return _Up(unexpect, error());
0942 }
0943
0944 template <class _Func>
0945 requires is_constructible_v<_Err, _Err&&>
0946 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
0947 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>;
0948 static_assert(
0949 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected");
0950 static_assert(is_same_v<typename _Up::error_type, _Err>,
0951 "The result of f(std::move(value())) must have the same error_type as this expected");
0952 if (has_value()) {
0953 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
0954 }
0955 return _Up(unexpect, std::move(error()));
0956 }
0957
0958 template <class _Func>
0959 requires is_constructible_v<_Err, const _Err&&>
0960 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
0961 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
0962 static_assert(
0963 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected");
0964 static_assert(is_same_v<typename _Up::error_type, _Err>,
0965 "The result of f(std::move(value())) must have the same error_type as this expected");
0966 if (has_value()) {
0967 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
0968 }
0969 return _Up(unexpect, std::move(error()));
0970 }
0971
0972 template <class _Func>
0973 requires is_constructible_v<_Tp, _Tp&>
0974 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
0975 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
0976 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
0977 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
0978 "The result of f(error()) must have the same value_type as this expected");
0979 if (has_value()) {
0980 return _Gp(in_place, this->__val());
0981 }
0982 return std::invoke(std::forward<_Func>(__f), error());
0983 }
0984
0985 template <class _Func>
0986 requires is_constructible_v<_Tp, const _Tp&>
0987 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
0988 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
0989 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
0990 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
0991 "The result of f(error()) must have the same value_type as this expected");
0992 if (has_value()) {
0993 return _Gp(in_place, this->__val());
0994 }
0995 return std::invoke(std::forward<_Func>(__f), error());
0996 }
0997
0998 template <class _Func>
0999 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&&>>;
1002 static_assert(
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");
1006 if (has_value()) {
1007 return _Gp(in_place, std::move(this->__val()));
1008 }
1009 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1010 }
1011
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&&>>;
1016 static_assert(
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");
1020 if (has_value()) {
1021 return _Gp(in_place, std::move(this->__val()));
1022 }
1023 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1024 }
1025
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&>>;
1030 if (!has_value()) {
1031 return expected<_Up, _Err>(unexpect, error());
1032 }
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());
1036 } else {
1037 std::invoke(std::forward<_Func>(__f), this->__val());
1038 return expected<_Up, _Err>();
1039 }
1040 }
1041
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&>>;
1046 if (!has_value()) {
1047 return expected<_Up, _Err>(unexpect, error());
1048 }
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());
1052 } else {
1053 std::invoke(std::forward<_Func>(__f), this->__val());
1054 return expected<_Up, _Err>();
1055 }
1056 }
1057
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&&>>;
1062 if (!has_value()) {
1063 return expected<_Up, _Err>(unexpect, std::move(error()));
1064 }
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()));
1068 } else {
1069 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1070 return expected<_Up, _Err>();
1071 }
1072 }
1073
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&&>>;
1078 if (!has_value()) {
1079 return expected<_Up, _Err>(unexpect, std::move(error()));
1080 }
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()));
1084 } else {
1085 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1086 return expected<_Up, _Err>();
1087 }
1088 }
1089
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");
1096 if (has_value()) {
1097 return expected<_Tp, _Gp>(in_place, this->__val());
1098 }
1099 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1100 }
1101
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");
1108 if (has_value()) {
1109 return expected<_Tp, _Gp>(in_place, this->__val());
1110 }
1111 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1112 }
1113
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");
1120 if (has_value()) {
1121 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1122 }
1123 return expected<_Tp, _Gp>(
1124 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1125 }
1126
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");
1133 if (has_value()) {
1134 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1135 }
1136 return expected<_Tp, _Gp>(
1137 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1138 }
1139
1140
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()) {
1145 return false;
1146 } else {
1147 if (__x.__has_val()) {
1148 return __x.__val() == __y.__val();
1149 } else {
1150 return __x.__unex() == __y.__unex();
1151 }
1152 }
1153 }
1154
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);
1158 }
1159
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());
1163 }
1164 };
1165
1166 template <class _Err>
1167 class __expected_void_base {
1168 struct __empty_t {};
1169
1170
1171
1172
1173 union __union_t {
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>)
1177 = default;
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>)
1181 = default;
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;
1184
1185 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}
1186
1187 template <class... _Args>
1188 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
1189 : __unex_(std::forward<_Args>(__args)...) {}
1190
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)...)) {}
1195
1196 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1197 requires(is_trivially_destructible_v<_Err>)
1198 = default;
1199
1200
1201 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1202 requires(!is_trivially_destructible_v<_Err>)
1203 {}
1204
1205 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;
1206 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
1207 };
1208
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;
1211
1212 struct __repr {
1213 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
1214
1215 template <class... _Args>
1216 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}
1217
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) {}
1221
1222 template <class... _Args>
1223 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
1224 _Args&&... __args)
1225 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1226
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) {}
1233
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>)
1237 = default;
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>)
1241 = default;
1242
1243 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
1244 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
1245
1246 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1247 requires(is_trivially_destructible_v<_Err>)
1248 = default;
1249
1250 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1251 requires(!is_trivially_destructible_v<_Err>)
1252 {
1253 __destroy_union_member();
1254 }
1255
1256 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1257 requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>)
1258 {
1259 std::destroy_at(&__union_.__v);
1260 }
1261
1262 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1263 requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)
1264 {
1265 __destroy_union_member();
1266 std::destroy_at(&__union_.__v);
1267 }
1268
1269 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)
1270 requires(__allow_reusing_expected_tail_padding)
1271 {
1272 std::construct_at(&__union_.__v, in_place);
1273 __has_val_ = true;
1274 }
1275
1276 template <class... _Args>
1277 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
1278 requires(__allow_reusing_expected_tail_padding)
1279 {
1280 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
1281 __has_val_ = false;
1282 }
1283
1284 private:
1285 template <class>
1286 friend class __expected_void_base;
1287
1288 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
1289 requires(!is_trivially_destructible_v<_Err>)
1290 {
1291 if (!__has_val_)
1292 std::destroy_at(std::addressof(__union_.__v.__unex_));
1293 }
1294
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)
1298 {
1299 if (__has_val)
1300 return __union_t(in_place);
1301 else
1302 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1303 }
1304
1305 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
1306 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
1307 };
1308
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)
1312 {
1313 if (__has_val)
1314 return __repr(in_place);
1315 else
1316 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1317 }
1318
1319 protected:
1320 template <class... _Args>
1321 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)
1322 : __repr_(in_place, std::forward<_Args>(__args)...) {}
1323
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)); }) {}
1329
1330 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
1331 if constexpr (__put_flag_in_tail)
1332 std::destroy_at(&__repr_.__v);
1333 else
1334 __repr_.__v.__destroy_union();
1335 }
1336
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)...);
1341 else
1342 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
1343 }
1344
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_; }
1350
1351 private:
1352 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
1353 };
1354
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");
1361
1362 template <class, class>
1363 friend class expected;
1364
1365 template <class _Up, class _OtherErr, class _OtherErrQual>
1366 using __can_convert _LIBCPP_NODEBUG =
1367 _And< is_void<_Up>,
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>>>>;
1373
1374 using __base _LIBCPP_NODEBUG = __expected_void_base<_Err>;
1375
1376 public:
1377 using value_type = _Tp;
1378 using error_type = _Err;
1379 using unexpected_type = unexpected<_Err>;
1380
1381 template <class _Up>
1382 using rebind = expected<_Up, error_type>;
1383
1384
1385 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}
1386
1387 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
1388
1389 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
1390 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1391 = default;
1392
1393 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(
1394 is_nothrow_copy_constructible_v<_Err>)
1395 requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)
1396 : __base(__rhs.__has_val(), __rhs.__union()) {}
1397
1398 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
1399 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1400 = default;
1401
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())) {}
1405
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&>)
1411 : __base(__rhs.__has_val(), __rhs.__union()) {}
1412
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>)
1417 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1418
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&>)
1423 : __base(unexpect, __unex.error()) {}
1424
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>)
1429 : __base(unexpect, std::move(__unex.error())) {}
1430
1431 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}
1432
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...>)
1437 : __base(unexpect, std::forward<_Args>(__args)...) {}
1438
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...>)
1443 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
1444
1445 private:
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)...) {}
1450
1451 public:
1452
1453
1454 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
1455
1456 private:
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");
1460
1461 this->__destroy();
1462 auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });
1463 this->__construct(unexpect, std::forward<_Args>(__args)...);
1464 __trans.__complete();
1465 }
1466
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");
1469
1470 this->__destroy();
1471 this->__construct(in_place);
1472 }
1473
1474 public:
1475
1476 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
1477
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>)
1480 requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)
1481 {
1482 if (this->__has_val()) {
1483 if (!__rhs.__has_val()) {
1484 __reinit_expected(unexpect, __rhs.__unex());
1485 }
1486 } else {
1487 if (__rhs.__has_val()) {
1488 __reinit_expected(in_place);
1489 } else {
1490 this->__unex() = __rhs.__unex();
1491 }
1492 }
1493 return *this;
1494 }
1495
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>)
1499 {
1500 if (this->__has_val()) {
1501 if (!__rhs.__has_val()) {
1502 __reinit_expected(unexpect, std::move(__rhs.__unex()));
1503 }
1504 } else {
1505 if (__rhs.__has_val()) {
1506 __reinit_expected(in_place);
1507 } else {
1508 this->__unex() = std::move(__rhs.__unex());
1509 }
1510 }
1511 return *this;
1512 }
1513
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());
1519 } else {
1520 this->__unex() = __un.error();
1521 }
1522 return *this;
1523 }
1524
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()));
1530 } else {
1531 this->__unex() = std::move(__un.error());
1532 }
1533 return *this;
1534 }
1535
1536 _LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept {
1537 if (!this->__has_val()) {
1538 __reinit_expected(in_place);
1539 }
1540 }
1541
1542
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>)
1546 {
1547 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
1548
1549 __with_val.__reinit_expected(unexpect, std::move(__with_err.__unex()));
1550
1551 __with_err.__reinit_expected(in_place);
1552 };
1553
1554 if (this->__has_val()) {
1555 if (!__rhs.__has_val()) {
1556 __swap_val_unex_impl(*this, __rhs);
1557 }
1558 } else {
1559 if (__rhs.__has_val()) {
1560 __swap_val_unex_impl(__rhs, *this);
1561 } else {
1562 using std::swap;
1563 swap(this->__unex(), __rhs.__unex());
1564 }
1565 }
1566 }
1567
1568 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))
1569 requires requires { __x.swap(__y); }
1570 {
1571 __x.swap(__y);
1572 }
1573
1574
1575 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
1576
1577 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
1578
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");
1582 }
1583
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());
1588 }
1589 }
1590
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()));
1595 }
1596 }
1597
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();
1602 }
1603
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();
1608 }
1609
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());
1614 }
1615
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());
1620 }
1621
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");
1626 if (has_value()) {
1627 return std::forward<_Up>(__error);
1628 }
1629 return error();
1630 }
1631
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");
1636 if (has_value()) {
1637 return std::forward<_Up>(__error);
1638 }
1639 return std::move(error());
1640 }
1641
1642
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");
1648 static_assert(
1649 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1650 if (has_value()) {
1651 return std::invoke(std::forward<_Func>(__f));
1652 }
1653 return _Up(unexpect, error());
1654 }
1655
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");
1661 static_assert(
1662 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1663 if (has_value()) {
1664 return std::invoke(std::forward<_Func>(__f));
1665 }
1666 return _Up(unexpect, error());
1667 }
1668
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");
1674 static_assert(
1675 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1676 if (has_value()) {
1677 return std::invoke(std::forward<_Func>(__f));
1678 }
1679 return _Up(unexpect, std::move(error()));
1680 }
1681
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");
1687 static_assert(
1688 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");
1689 if (has_value()) {
1690 return std::invoke(std::forward<_Func>(__f));
1691 }
1692 return _Up(unexpect, std::move(error()));
1693 }
1694
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");
1701 if (has_value()) {
1702 return _Gp();
1703 }
1704 return std::invoke(std::forward<_Func>(__f), error());
1705 }
1706
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");
1713 if (has_value()) {
1714 return _Gp();
1715 }
1716 return std::invoke(std::forward<_Func>(__f), error());
1717 }
1718
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&&>>;
1722 static_assert(
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");
1726 if (has_value()) {
1727 return _Gp();
1728 }
1729 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1730 }
1731
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&&>>;
1735 static_assert(
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");
1739 if (has_value()) {
1740 return _Gp();
1741 }
1742 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1743 }
1744
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>>;
1749 if (!has_value()) {
1750 return expected<_Up, _Err>(unexpect, error());
1751 }
1752 if constexpr (!is_void_v<_Up>) {
1753 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1754 } else {
1755 std::invoke(std::forward<_Func>(__f));
1756 return expected<_Up, _Err>();
1757 }
1758 }
1759
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>>;
1764 if (!has_value()) {
1765 return expected<_Up, _Err>(unexpect, error());
1766 }
1767 if constexpr (!is_void_v<_Up>) {
1768 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1769 } else {
1770 std::invoke(std::forward<_Func>(__f));
1771 return expected<_Up, _Err>();
1772 }
1773 }
1774
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>>;
1779 if (!has_value()) {
1780 return expected<_Up, _Err>(unexpect, std::move(error()));
1781 }
1782 if constexpr (!is_void_v<_Up>) {
1783 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1784 } else {
1785 std::invoke(std::forward<_Func>(__f));
1786 return expected<_Up, _Err>();
1787 }
1788 }
1789
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>>;
1794 if (!has_value()) {
1795 return expected<_Up, _Err>(unexpect, std::move(error()));
1796 }
1797 if constexpr (!is_void_v<_Up>) {
1798 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));
1799 } else {
1800 std::invoke(std::forward<_Func>(__f));
1801 return expected<_Up, _Err>();
1802 }
1803 }
1804
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");
1810 if (has_value()) {
1811 return expected<_Tp, _Gp>();
1812 }
1813 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1814 }
1815
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");
1821 if (has_value()) {
1822 return expected<_Tp, _Gp>();
1823 }
1824 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1825 }
1826
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");
1832 if (has_value()) {
1833 return expected<_Tp, _Gp>();
1834 }
1835 return expected<_Tp, _Gp>(
1836 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1837 }
1838
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");
1844 if (has_value()) {
1845 return expected<_Tp, _Gp>();
1846 }
1847 return expected<_Tp, _Gp>(
1848 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1849 }
1850
1851
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()) {
1856 return false;
1857 } else {
1858 return __x.__has_val() || static_cast<bool>(__x.__unex() == __y.__unex());
1859 }
1860 }
1861
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());
1865 }
1866 };
1867
1868 _LIBCPP_END_NAMESPACE_STD
1869
1870 #endif
1871
1872 _LIBCPP_POP_MACROS
1873
1874 #endif