File indexing completed on 2026-05-03 08:13:27
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___EXPECTED_EXPECTED_H
0010 #define _LIBCPP___CXX03___EXPECTED_EXPECTED_H
0011
0012 #include <__cxx03/__assert>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__expected/bad_expected_access.h>
0015 #include <__cxx03/__expected/unexpect.h>
0016 #include <__cxx03/__expected/unexpected.h>
0017 #include <__cxx03/__functional/invoke.h>
0018 #include <__cxx03/__memory/addressof.h>
0019 #include <__cxx03/__memory/construct_at.h>
0020 #include <__cxx03/__type_traits/conjunction.h>
0021 #include <__cxx03/__type_traits/disjunction.h>
0022 #include <__cxx03/__type_traits/integral_constant.h>
0023 #include <__cxx03/__type_traits/is_assignable.h>
0024 #include <__cxx03/__type_traits/is_constructible.h>
0025 #include <__cxx03/__type_traits/is_convertible.h>
0026 #include <__cxx03/__type_traits/is_function.h>
0027 #include <__cxx03/__type_traits/is_nothrow_assignable.h>
0028 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
0029 #include <__cxx03/__type_traits/is_reference.h>
0030 #include <__cxx03/__type_traits/is_same.h>
0031 #include <__cxx03/__type_traits/is_swappable.h>
0032 #include <__cxx03/__type_traits/is_trivially_constructible.h>
0033 #include <__cxx03/__type_traits/is_trivially_destructible.h>
0034 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
0035 #include <__cxx03/__type_traits/is_void.h>
0036 #include <__cxx03/__type_traits/lazy.h>
0037 #include <__cxx03/__type_traits/negation.h>
0038 #include <__cxx03/__type_traits/remove_cv.h>
0039 #include <__cxx03/__type_traits/remove_cvref.h>
0040 #include <__cxx03/__utility/as_const.h>
0041 #include <__cxx03/__utility/exception_guard.h>
0042 #include <__cxx03/__utility/forward.h>
0043 #include <__cxx03/__utility/in_place.h>
0044 #include <__cxx03/__utility/move.h>
0045 #include <__cxx03/__utility/swap.h>
0046 #include <__cxx03/__verbose_abort>
0047 #include <__cxx03/initializer_list>
0048
0049 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0050 # pragma GCC system_header
0051 #endif
0052
0053 _LIBCPP_PUSH_MACROS
0054 #include <__cxx03/__undef_macros>
0055
0056 #if _LIBCPP_STD_VER >= 23
0057
0058 _LIBCPP_BEGIN_NAMESPACE_STD
0059
0060 template <class _Tp, class _Err>
0061 class expected;
0062
0063 template <class _Tp>
0064 struct __is_std_expected : false_type {};
0065
0066 template <class _Tp, class _Err>
0067 struct __is_std_expected<expected<_Tp, _Err>> : true_type {};
0068
0069 struct __expected_construct_in_place_from_invoke_tag {};
0070 struct __expected_construct_unexpected_from_invoke_tag {};
0071
0072 template <class _Err, class _Arg>
0073 _LIBCPP_HIDE_FROM_ABI void __throw_bad_expected_access(_Arg&& __arg) {
0074 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0075 throw bad_expected_access<_Err>(std::forward<_Arg>(__arg));
0076 # else
0077 (void)__arg;
0078 _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");
0079 # endif
0080 }
0081
0082
0083
0084
0085
0086
0087
0088 struct __conditional_no_unique_address_invoke_tag {};
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 template <bool _NoUnique, class _Tp>
0101 struct __conditional_no_unique_address;
0102
0103 template <class _Tp>
0104 struct __conditional_no_unique_address<true, _Tp> {
0105 template <class... _Args>
0106 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
0107 : __v(std::forward<_Args>(__args)...) {}
0108
0109 template <class _Func, class... _Args>
0110 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
0111 __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
0112 : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0113
0114 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v;
0115 };
0116
0117 template <class _Tp>
0118 struct __conditional_no_unique_address<false, _Tp> {
0119 template <class... _Args>
0120 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)
0121 : __v(std::forward<_Args>(__args)...) {}
0122
0123 template <class _Func, class... _Args>
0124 _LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(
0125 __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)
0126 : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0127
0128 _Tp __v;
0129 };
0130
0131
0132
0133 template <class _First, class _Second>
0134 inline constexpr bool __fits_in_tail_padding = []() {
0135 struct __x {
0136 _LIBCPP_NO_UNIQUE_ADDRESS _First __first;
0137 _LIBCPP_NO_UNIQUE_ADDRESS _Second __second;
0138 };
0139 return sizeof(__x) == sizeof(_First);
0140 }();
0141
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 template <class _Tp, class _Err>
0210 class __expected_base {
0211
0212
0213
0214
0215 union __union_t {
0216 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;
0217 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)
0218 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0219 is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
0220 = default;
0221 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;
0222 _LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)
0223 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0224 is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
0225 = default;
0226 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;
0227 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;
0228
0229 template <class... _Args>
0230 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t, _Args&&... __args)
0231 : __val_(std::forward<_Args>(__args)...) {}
0232
0233 template <class... _Args>
0234 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
0235 : __unex_(std::forward<_Args>(__args)...) {}
0236
0237 template <class _Func, class... _Args>
0238 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
0239 std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args)
0240 : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0241
0242 template <class _Func, class... _Args>
0243 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(
0244 std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)
0245 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}
0246
0247 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
0248 requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
0249 = default;
0250
0251
0252 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {}
0253
0254 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
0255 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
0256 };
0257
0258 static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;
0259 static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;
0260
0261 struct __repr {
0262 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
0263
0264 template <class... _Args>
0265 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag, _Args&&... __args)
0266 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
0267
0268 template <class... _Args>
0269 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)
0270 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
0271
0272 template <class... _Args>
0273 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag,
0274 _Args&&... __args)
0275 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}
0276
0277 template <class... _Args>
0278 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
0279 _Args&&... __args)
0280 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
0281
0282
0283
0284
0285
0286
0287 template <class _OtherUnion>
0288 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)
0289 requires(__allow_reusing_expected_tail_padding)
0290 : __union_(__conditional_no_unique_address_invoke_tag{},
0291 [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),
0292 __has_val_(__has_val) {}
0293
0294 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;
0295 _LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)
0296 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0297 is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)
0298 = default;
0299 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;
0300 _LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)
0301 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0302 is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)
0303 = default;
0304
0305 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
0306 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
0307
0308 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
0309 requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)
0310 = default;
0311
0312 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
0313 requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
0314 {
0315 __destroy_union_member();
0316 }
0317
0318 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
0319 requires(__allow_reusing_expected_tail_padding &&
0320 (is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>))
0321 {
0322
0323
0324 std::destroy_at(&__union_.__v);
0325 }
0326
0327 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
0328 requires(__allow_reusing_expected_tail_padding &&
0329 (!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>))
0330 {
0331 __destroy_union_member();
0332 std::destroy_at(&__union_.__v);
0333 }
0334
0335 template <class... _Args>
0336 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t, _Args&&... __args)
0337 requires(__allow_reusing_expected_tail_padding)
0338 {
0339 std::construct_at(&__union_.__v, in_place, std::forward<_Args>(__args)...);
0340 __has_val_ = true;
0341 }
0342
0343 template <class... _Args>
0344 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
0345 requires(__allow_reusing_expected_tail_padding)
0346 {
0347 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
0348 __has_val_ = false;
0349 }
0350
0351 private:
0352 template <class, class>
0353 friend class __expected_base;
0354
0355 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
0356 requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)
0357 {
0358 if (__has_val_) {
0359 std::destroy_at(std::addressof(__union_.__v.__val_));
0360 } else {
0361 std::destroy_at(std::addressof(__union_.__v.__unex_));
0362 }
0363 }
0364
0365 template <class _OtherUnion>
0366 _LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)
0367 requires(__allow_reusing_expected_tail_padding)
0368 {
0369 if (__has_val)
0370 return __union_t(in_place, std::forward<_OtherUnion>(__other).__val_);
0371 else
0372 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
0373 }
0374
0375 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
0376 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
0377 };
0378
0379 template <class _OtherUnion>
0380 _LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)
0381 requires(__put_flag_in_tail)
0382 {
0383 if (__has_val)
0384 return __repr(in_place, std::forward<_OtherUnion>(__other).__val_);
0385 else
0386 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
0387 }
0388
0389 protected:
0390 template <class... _Args>
0391 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(_Args&&... __args)
0392 : __repr_(in_place, std::forward<_Args>(__args)...) {}
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413 template <class _OtherUnion>
0414 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(bool __has_val, _OtherUnion&& __other)
0415 requires(__put_flag_in_tail)
0416 : __repr_(__conditional_no_unique_address_invoke_tag{},
0417 [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}
0418
0419 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
0420 if constexpr (__put_flag_in_tail)
0421 std::destroy_at(&__repr_.__v);
0422 else
0423 __repr_.__v.__destroy_union();
0424 }
0425
0426 template <class _Tag, class... _Args>
0427 _LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {
0428 if constexpr (__put_flag_in_tail)
0429 std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);
0430 else
0431 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
0432 }
0433
0434 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }
0435 _LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }
0436 _LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }
0437 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __val() { return __repr_.__v.__union_.__v.__val_; }
0438 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& __val() const { return __repr_.__v.__union_.__v.__val_; }
0439 _LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }
0440 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }
0441
0442 private:
0443 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
0444 };
0445
0446 template <class _Tp, class _Err>
0447 class expected : private __expected_base<_Tp, _Err> {
0448 static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> &&
0449 !is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value &&
0450 __valid_std_unexpected<_Err>::value,
0451 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "
0452 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "
0453 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "
0454 "definition of the template expected<T, E> with a type for the E parameter that is not a valid "
0455 "template argument for unexpected is ill-formed.");
0456
0457 template <class _Up, class _OtherErr>
0458 friend class expected;
0459
0460 using __base = __expected_base<_Tp, _Err>;
0461
0462 public:
0463 using value_type = _Tp;
0464 using error_type = _Err;
0465 using unexpected_type = unexpected<_Err>;
0466
0467 using __trivially_relocatable =
0468 __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,
0469 expected,
0470 void>;
0471
0472 template <class _Up>
0473 using rebind = expected<_Up, error_type>;
0474
0475
0476 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>)
0477 requires is_default_constructible_v<_Tp>
0478 : __base(in_place) {}
0479
0480 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
0481
0482 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
0483 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&
0484 is_trivially_copy_constructible_v<_Err>)
0485 = default;
0486
0487 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
0488 is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>)
0489 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
0490 !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
0491 : __base(__other.__has_val(), __other.__union()) {}
0492
0493 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
0494 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&
0495 is_trivially_move_constructible_v<_Err>)
0496 = default;
0497
0498 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(
0499 is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)
0500 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&
0501 !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))
0502 : __base(__other.__has_val(), std::move(__other.__union())) {}
0503
0504 private:
0505 template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>
0506 using __can_convert = _And<
0507 is_constructible<_Tp, _UfQual>,
0508 is_constructible<_Err, _OtherErrQual>,
0509 _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
0510 _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>,
0511 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
0512 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
0513 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
0514 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
0515 _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>,
0516 _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>,
0517 _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>,
0518 _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>,
0519 true_type>,
0520 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,
0521 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,
0522 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,
0523 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >;
0524
0525 template <class _Func, class... _Args>
0526 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
0527 std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
0528 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
0529
0530 template <class _Func, class... _Args>
0531 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(
0532 std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)
0533 : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}
0534
0535 public:
0536 template <class _Up, class _OtherErr>
0537 requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
0538 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> ||
0539 !is_convertible_v<const _OtherErr&, _Err>)
0540 expected(const expected<_Up, _OtherErr>& __other) noexcept(
0541 is_nothrow_constructible_v<_Tp, const _Up&> &&
0542 is_nothrow_constructible_v<_Err, const _OtherErr&>)
0543 : __base(__other.__has_val(), __other.__union()) {}
0544
0545 template <class _Up, class _OtherErr>
0546 requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value
0547 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>)
0548 expected(expected<_Up, _OtherErr>&& __other) noexcept(
0549 is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>)
0550 : __base(__other.__has_val(), std::move(__other.__union())) {}
0551
0552 template <class _Up = _Tp>
0553 requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&
0554 is_constructible_v<_Tp, _Up> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
0555 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))
0556 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)
0557 expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>)
0558 : __base(in_place, std::forward<_Up>(__u)) {}
0559
0560 template <class _OtherErr>
0561 requires is_constructible_v<_Err, const _OtherErr&>
0562 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(
0563 const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>)
0564 : __base(unexpect, __unex.error()) {}
0565
0566 template <class _OtherErr>
0567 requires is_constructible_v<_Err, _OtherErr>
0568 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)
0569 expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>)
0570 : __base(unexpect, std::move(__unex.error())) {}
0571
0572 template <class... _Args>
0573 requires is_constructible_v<_Tp, _Args...>
0574 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(
0575 is_nothrow_constructible_v<_Tp, _Args...>)
0576 : __base(in_place, std::forward<_Args>(__args)...) {}
0577
0578 template <class _Up, class... _Args>
0579 requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >
0580 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
0581 is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
0582 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
0583
0584 template <class... _Args>
0585 requires is_constructible_v<_Err, _Args...>
0586 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(
0587 is_nothrow_constructible_v<_Err, _Args...>)
0588 : __base(unexpect, std::forward<_Args>(__args)...) {}
0589
0590 template <class _Up, class... _Args>
0591 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >
0592 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(
0593 is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>)
0594 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
0595
0596
0597
0598 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
0599
0600 private:
0601 template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>
0602 _LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {
0603 if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {
0604 this->__destroy();
0605 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
0606 } else if constexpr (is_nothrow_move_constructible_v<_T1>) {
0607 _T1 __tmp(std::forward<_Args>(__args)...);
0608 this->__destroy();
0609 this->__construct(_Tag{}, std::move(__tmp));
0610 } else {
0611 static_assert(
0612 is_nothrow_move_constructible_v<_T2>,
0613 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "
0614 "be reverted to the previous state in case an exception is thrown during the assignment.");
0615 _T2 __tmp(std::move(__oldval));
0616 this->__destroy();
0617 auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });
0618 this->__construct(_Tag{}, std::forward<_Args>(__args)...);
0619 __trans.__complete();
0620 }
0621 }
0622
0623 public:
0624
0625 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
0626
0627 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(
0628 is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&
0629 is_nothrow_copy_constructible_v<_Err>)
0630 requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&
0631 is_copy_constructible_v<_Err> &&
0632 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0633 {
0634 if (this->__has_val() && __rhs.__has_val()) {
0635 this->__val() = __rhs.__val();
0636 } else if (this->__has_val()) {
0637 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());
0638 } else if (__rhs.__has_val()) {
0639 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());
0640 } else {
0641 this->__unex() = __rhs.__unex();
0642 }
0643 return *this;
0644 }
0645
0646 _LIBCPP_HIDE_FROM_ABI constexpr expected&
0647 operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&
0648 is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)
0649 requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&
0650 is_move_assignable_v<_Err> &&
0651 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0652 {
0653 if (this->__has_val() && __rhs.__has_val()) {
0654 this->__val() = std::move(__rhs.__val());
0655 } else if (this->__has_val()) {
0656 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));
0657 } else if (__rhs.__has_val()) {
0658 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));
0659 } else {
0660 this->__unex() = std::move(__rhs.__unex());
0661 }
0662 return *this;
0663 }
0664
0665 template <class _Up = _Tp>
0666 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)
0667 requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&
0668 is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&
0669 (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||
0670 is_nothrow_move_constructible_v<_Err>))
0671 {
0672 if (this->__has_val()) {
0673 this->__val() = std::forward<_Up>(__v);
0674 } else {
0675 __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));
0676 }
0677 return *this;
0678 }
0679
0680 private:
0681 template <class _OtherErrQual>
0682 static constexpr bool __can_assign_from_unexpected =
0683 _And< is_constructible<_Err, _OtherErrQual>,
0684 is_assignable<_Err&, _OtherErrQual>,
0685 _Lazy<_Or,
0686 is_nothrow_constructible<_Err, _OtherErrQual>,
0687 is_nothrow_move_constructible<_Tp>,
0688 is_nothrow_move_constructible<_Err>> >::value;
0689
0690 public:
0691 template <class _OtherErr>
0692 requires(__can_assign_from_unexpected<const _OtherErr&>)
0693 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {
0694 if (this->__has_val()) {
0695 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error());
0696 } else {
0697 this->__unex() = __un.error();
0698 }
0699 return *this;
0700 }
0701
0702 template <class _OtherErr>
0703 requires(__can_assign_from_unexpected<_OtherErr>)
0704 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {
0705 if (this->__has_val()) {
0706 __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error()));
0707 } else {
0708 this->__unex() = std::move(__un.error());
0709 }
0710 return *this;
0711 }
0712
0713 template <class... _Args>
0714 requires is_nothrow_constructible_v<_Tp, _Args...>
0715 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept {
0716 this->__destroy();
0717 this->__construct(in_place, std::forward<_Args>(__args)...);
0718 return this->__val();
0719 }
0720
0721 template <class _Up, class... _Args>
0722 requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
0723 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept {
0724 this->__destroy();
0725 this->__construct(in_place, __il, std::forward<_Args>(__args)...);
0726 return this->__val();
0727 }
0728
0729 public:
0730
0731 _LIBCPP_HIDE_FROM_ABI constexpr void
0732 swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> &&
0733 is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)
0734 requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> &&
0735 is_move_constructible_v<_Err> &&
0736 (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))
0737 {
0738 auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {
0739 if constexpr (is_nothrow_move_constructible_v<_Err>) {
0740 _Err __tmp(std::move(__with_err.__unex()));
0741 __with_err.__destroy();
0742 auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); });
0743 __with_err.__construct(in_place, std::move(__with_val.__val()));
0744 __trans.__complete();
0745 __with_val.__destroy();
0746 __with_val.__construct(unexpect, std::move(__tmp));
0747 } else {
0748 static_assert(is_nothrow_move_constructible_v<_Tp>,
0749 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "
0750 "that it can be reverted to the previous state in case an exception is thrown during swap.");
0751 _Tp __tmp(std::move(__with_val.__val()));
0752 __with_val.__destroy();
0753 auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); });
0754 __with_val.__construct(unexpect, std::move(__with_err.__unex()));
0755 __trans.__complete();
0756 __with_err.__destroy();
0757 __with_err.__construct(in_place, std::move(__tmp));
0758 }
0759 };
0760
0761 if (this->__has_val()) {
0762 if (__rhs.__has_val()) {
0763 using std::swap;
0764 swap(this->__val(), __rhs.__val());
0765 } else {
0766 __swap_val_unex_impl(*this, __rhs);
0767 }
0768 } else {
0769 if (__rhs.__has_val()) {
0770 __swap_val_unex_impl(__rhs, *this);
0771 } else {
0772 using std::swap;
0773 swap(this->__unex(), __rhs.__unex());
0774 }
0775 }
0776 }
0777
0778 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))
0779 requires requires { __x.swap(__y); }
0780 {
0781 __x.swap(__y);
0782 }
0783
0784
0785 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept {
0786 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0787 this->__has_val(), "expected::operator-> requires the expected to contain a value");
0788 return std::addressof(this->__val());
0789 }
0790
0791 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept {
0792 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0793 this->__has_val(), "expected::operator-> requires the expected to contain a value");
0794 return std::addressof(this->__val());
0795 }
0796
0797 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
0798 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0799 this->__has_val(), "expected::operator* requires the expected to contain a value");
0800 return this->__val();
0801 }
0802
0803 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
0804 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0805 this->__has_val(), "expected::operator* requires the expected to contain a value");
0806 return this->__val();
0807 }
0808
0809 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
0810 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0811 this->__has_val(), "expected::operator* requires the expected to contain a value");
0812 return std::move(this->__val());
0813 }
0814
0815 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
0816 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0817 this->__has_val(), "expected::operator* requires the expected to contain a value");
0818 return std::move(this->__val());
0819 }
0820
0821 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }
0822
0823 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
0824
0825 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
0826 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0827 if (!this->__has_val()) {
0828 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
0829 }
0830 return this->__val();
0831 }
0832
0833 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
0834 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0835 if (!this->__has_val()) {
0836 std::__throw_bad_expected_access<_Err>(std::as_const(error()));
0837 }
0838 return this->__val();
0839 }
0840
0841 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& {
0842 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
0843 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
0844 if (!this->__has_val()) {
0845 std::__throw_bad_expected_access<_Err>(std::move(error()));
0846 }
0847 return std::move(this->__val());
0848 }
0849
0850 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
0851 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,
0852 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))");
0853 if (!this->__has_val()) {
0854 std::__throw_bad_expected_access<_Err>(std::move(error()));
0855 }
0856 return std::move(this->__val());
0857 }
0858
0859 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {
0860 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0861 !this->__has_val(), "expected::error requires the expected to contain an error");
0862 return this->__unex();
0863 }
0864
0865 _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {
0866 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0867 !this->__has_val(), "expected::error requires the expected to contain an error");
0868 return this->__unex();
0869 }
0870
0871 _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {
0872 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0873 !this->__has_val(), "expected::error requires the expected to contain an error");
0874 return std::move(this->__unex());
0875 }
0876
0877 _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {
0878 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0879 !this->__has_val(), "expected::error requires the expected to contain an error");
0880 return std::move(this->__unex());
0881 }
0882
0883 template <class _Up>
0884 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
0885 static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible");
0886 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
0887 return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v));
0888 }
0889
0890 template <class _Up>
0891 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
0892 static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible");
0893 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");
0894 return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v));
0895 }
0896
0897 template <class _Up = _Err>
0898 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {
0899 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
0900 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
0901 if (has_value())
0902 return std::forward<_Up>(__error);
0903 return error();
0904 }
0905
0906 template <class _Up = _Err>
0907 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {
0908 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");
0909 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");
0910 if (has_value())
0911 return std::forward<_Up>(__error);
0912 return std::move(error());
0913 }
0914
0915
0916 template <class _Func>
0917 requires is_constructible_v<_Err, _Err&>
0918 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
0919 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;
0920 static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");
0921 static_assert(is_same_v<typename _Up::error_type, _Err>,
0922 "The result of f(**this) must have the same error_type as this expected");
0923 if (has_value()) {
0924 return std::invoke(std::forward<_Func>(__f), this->__val());
0925 }
0926 return _Up(unexpect, error());
0927 }
0928
0929 template <class _Func>
0930 requires is_constructible_v<_Err, const _Err&>
0931 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
0932 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>;
0933 static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");
0934 static_assert(is_same_v<typename _Up::error_type, _Err>,
0935 "The result of f(**this) must have the same error_type as this expected");
0936 if (has_value()) {
0937 return std::invoke(std::forward<_Func>(__f), this->__val());
0938 }
0939 return _Up(unexpect, error());
0940 }
0941
0942 template <class _Func>
0943 requires is_constructible_v<_Err, _Err&&>
0944 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
0945 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>;
0946 static_assert(
0947 __is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");
0948 static_assert(is_same_v<typename _Up::error_type, _Err>,
0949 "The result of f(std::move(**this)) must have the same error_type as this expected");
0950 if (has_value()) {
0951 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
0952 }
0953 return _Up(unexpect, std::move(error()));
0954 }
0955
0956 template <class _Func>
0957 requires is_constructible_v<_Err, const _Err&&>
0958 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
0959 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
0960 static_assert(
0961 __is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");
0962 static_assert(is_same_v<typename _Up::error_type, _Err>,
0963 "The result of f(std::move(**this)) must have the same error_type as this expected");
0964 if (has_value()) {
0965 return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
0966 }
0967 return _Up(unexpect, std::move(error()));
0968 }
0969
0970 template <class _Func>
0971 requires is_constructible_v<_Tp, _Tp&>
0972 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {
0973 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;
0974 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
0975 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
0976 "The result of f(error()) must have the same value_type as this expected");
0977 if (has_value()) {
0978 return _Gp(in_place, this->__val());
0979 }
0980 return std::invoke(std::forward<_Func>(__f), error());
0981 }
0982
0983 template <class _Func>
0984 requires is_constructible_v<_Tp, const _Tp&>
0985 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {
0986 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;
0987 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");
0988 static_assert(is_same_v<typename _Gp::value_type, _Tp>,
0989 "The result of f(error()) must have the same value_type as this expected");
0990 if (has_value()) {
0991 return _Gp(in_place, this->__val());
0992 }
0993 return std::invoke(std::forward<_Func>(__f), error());
0994 }
0995
0996 template <class _Func>
0997 requires is_constructible_v<_Tp, _Tp&&>
0998 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {
0999 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;
1000 static_assert(
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");
1004 if (has_value()) {
1005 return _Gp(in_place, std::move(this->__val()));
1006 }
1007 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1008 }
1009
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&&>>;
1014 static_assert(
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");
1018 if (has_value()) {
1019 return _Gp(in_place, std::move(this->__val()));
1020 }
1021 return std::invoke(std::forward<_Func>(__f), std::move(error()));
1022 }
1023
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&>>;
1028 if (!has_value()) {
1029 return expected<_Up, _Err>(unexpect, error());
1030 }
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());
1034 } else {
1035 std::invoke(std::forward<_Func>(__f), this->__val());
1036 return expected<_Up, _Err>();
1037 }
1038 }
1039
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&>>;
1044 if (!has_value()) {
1045 return expected<_Up, _Err>(unexpect, error());
1046 }
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());
1050 } else {
1051 std::invoke(std::forward<_Func>(__f), this->__val());
1052 return expected<_Up, _Err>();
1053 }
1054 }
1055
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&&>>;
1060 if (!has_value()) {
1061 return expected<_Up, _Err>(unexpect, std::move(error()));
1062 }
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()));
1066 } else {
1067 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1068 return expected<_Up, _Err>();
1069 }
1070 }
1071
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&&>>;
1076 if (!has_value()) {
1077 return expected<_Up, _Err>(unexpect, std::move(error()));
1078 }
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()));
1082 } else {
1083 std::invoke(std::forward<_Func>(__f), std::move(this->__val()));
1084 return expected<_Up, _Err>();
1085 }
1086 }
1087
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");
1094 if (has_value()) {
1095 return expected<_Tp, _Gp>(in_place, this->__val());
1096 }
1097 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1098 }
1099
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");
1106 if (has_value()) {
1107 return expected<_Tp, _Gp>(in_place, this->__val());
1108 }
1109 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());
1110 }
1111
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");
1118 if (has_value()) {
1119 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1120 }
1121 return expected<_Tp, _Gp>(
1122 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1123 }
1124
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");
1131 if (has_value()) {
1132 return expected<_Tp, _Gp>(in_place, std::move(this->__val()));
1133 }
1134 return expected<_Tp, _Gp>(
1135 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));
1136 }
1137
1138
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()) {
1143 return false;
1144 } else {
1145 if (__x.__has_val()) {
1146 return __x.__val() == __y.__val();
1147 } else {
1148 return __x.__unex() == __y.__unex();
1149 }
1150 }
1151 }
1152
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);
1156 }
1157
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());
1161 }
1162 };
1163
1164 template <class _Err>
1165 class __expected_void_base {
1166 struct __empty_t {};
1167
1168
1169
1170
1171 union __union_t {
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>)
1175 = default;
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>)
1179 = default;
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;
1182
1183 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}
1184
1185 template <class... _Args>
1186 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)
1187 : __unex_(std::forward<_Args>(__args)...) {}
1188
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)...)) {}
1193
1194 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1195 requires(is_trivially_destructible_v<_Err>)
1196 = default;
1197
1198
1199 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()
1200 requires(!is_trivially_destructible_v<_Err>)
1201 {}
1202
1203 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;
1204 _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;
1205 };
1206
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;
1209
1210 struct __repr {
1211 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;
1212
1213 template <class... _Args>
1214 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}
1215
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) {}
1219
1220 template <class... _Args>
1221 _LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,
1222 _Args&&... __args)
1223 : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}
1224
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) {}
1231
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>)
1235 = default;
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>)
1239 = default;
1240
1241 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;
1242 _LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;
1243
1244 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1245 requires(is_trivially_destructible_v<_Err>)
1246 = default;
1247
1248 _LIBCPP_HIDE_FROM_ABI constexpr ~__repr()
1249 requires(!is_trivially_destructible_v<_Err>)
1250 {
1251 __destroy_union_member();
1252 }
1253
1254 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1255 requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>)
1256 {
1257 std::destroy_at(&__union_.__v);
1258 }
1259
1260 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()
1261 requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)
1262 {
1263 __destroy_union_member();
1264 std::destroy_at(&__union_.__v);
1265 }
1266
1267 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)
1268 requires(__allow_reusing_expected_tail_padding)
1269 {
1270 std::construct_at(&__union_.__v, in_place);
1271 __has_val_ = true;
1272 }
1273
1274 template <class... _Args>
1275 _LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)
1276 requires(__allow_reusing_expected_tail_padding)
1277 {
1278 std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);
1279 __has_val_ = false;
1280 }
1281
1282 private:
1283 template <class>
1284 friend class __expected_void_base;
1285
1286 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()
1287 requires(!is_trivially_destructible_v<_Err>)
1288 {
1289 if (!__has_val_)
1290 std::destroy_at(std::addressof(__union_.__v.__unex_));
1291 }
1292
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)
1296 {
1297 if (__has_val)
1298 return __union_t(in_place);
1299 else
1300 return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1301 }
1302
1303 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;
1304 _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;
1305 };
1306
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)
1310 {
1311 if (__has_val)
1312 return __repr(in_place);
1313 else
1314 return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);
1315 }
1316
1317 protected:
1318 template <class... _Args>
1319 _LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)
1320 : __repr_(in_place, std::forward<_Args>(__args)...) {}
1321
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)); }) {}
1327
1328 _LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {
1329 if constexpr (__put_flag_in_tail)
1330 std::destroy_at(&__repr_.__v);
1331 else
1332 __repr_.__v.__destroy_union();
1333 }
1334
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)...);
1339 else
1340 __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);
1341 }
1342
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_; }
1348
1349 private:
1350 _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;
1351 };
1352
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");
1359
1360 template <class, class>
1361 friend class expected;
1362
1363 template <class _Up, class _OtherErr, class _OtherErrQual>
1364 using __can_convert =
1365 _And< is_void<_Up>,
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>>>>;
1371
1372 using __base = __expected_void_base<_Err>;
1373
1374 public:
1375 using value_type = _Tp;
1376 using error_type = _Err;
1377 using unexpected_type = unexpected<_Err>;
1378
1379 template <class _Up>
1380 using rebind = expected<_Up, error_type>;
1381
1382
1383 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}
1384
1385 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;
1386
1387 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)
1388 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)
1389 = default;
1390
1391 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(
1392 is_nothrow_copy_constructible_v<_Err>)
1393 requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)
1394 : __base(__rhs.__has_val(), __rhs.__union()) {}
1395
1396 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)
1397 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)
1398 = default;
1399
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())) {}
1403
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&>)
1409 : __base(__rhs.__has_val(), __rhs.__union()) {}
1410
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>)
1415 : __base(__rhs.__has_val(), std::move(__rhs.__union())) {}
1416
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&>)
1421 : __base(unexpect, __unex.error()) {}
1422
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>)
1427 : __base(unexpect, std::move(__unex.error())) {}
1428
1429 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}
1430
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...>)
1435 : __base(unexpect, std::forward<_Args>(__args)...) {}
1436
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...>)
1441 : __base(unexpect, __il, std::forward<_Args>(__args)...) {}
1442
1443 private:
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)...) {}
1448
1449 public:
1450
1451
1452 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;
1453
1454 private:
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");
1458
1459 this->__destroy();
1460 auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });
1461 this->__construct(unexpect, std::forward<_Args>(__args)...);
1462 __trans.__complete();
1463 }
1464
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");
1467
1468 this->__destroy();
1469 this->__construct(in_place);
1470 }
1471
1472 public:
1473
1474 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;
1475
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>)
1478 requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)
1479 {
1480 if (this->__has_val()) {
1481 if (!__rhs.__has_val()) {
1482 __reinit_expected(unexpect, __rhs.__unex());
1483 }
1484 } else {
1485 if (__rhs.__has_val()) {
1486 __reinit_expected(in_place);
1487 } else {
1488 this->__unex() = __rhs.__unex();
1489 }
1490 }
1491 return *this;
1492 }
1493
1494 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&&) = delete;
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