| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | #ifndef _LIBCPP___EXPECTED_EXPECTED_H |
| 10 | #define _LIBCPP___EXPECTED_EXPECTED_H |
| 11 | |
| 12 | #include <__assert> |
| 13 | #include <__concepts/same_as.h> |
| 14 | #include <__config> |
| 15 | #include <__expected/bad_expected_access.h> |
| 16 | #include <__expected/unexpect.h> |
| 17 | #include <__expected/unexpected.h> |
| 18 | #include <__functional/invoke.h> |
| 19 | #include <__memory/addressof.h> |
| 20 | #include <__memory/construct_at.h> |
| 21 | #include <__type_traits/conditional.h> |
| 22 | #include <__type_traits/conjunction.h> |
| 23 | #include <__type_traits/disjunction.h> |
| 24 | #include <__type_traits/integral_constant.h> |
| 25 | #include <__type_traits/invoke.h> |
| 26 | #include <__type_traits/is_assignable.h> |
| 27 | #include <__type_traits/is_constructible.h> |
| 28 | #include <__type_traits/is_convertible.h> |
| 29 | #include <__type_traits/is_core_convertible.h> |
| 30 | #include <__type_traits/is_function.h> |
| 31 | #include <__type_traits/is_nothrow_assignable.h> |
| 32 | #include <__type_traits/is_nothrow_constructible.h> |
| 33 | #include <__type_traits/is_reference.h> |
| 34 | #include <__type_traits/is_relocatable.h> |
| 35 | #include <__type_traits/is_same.h> |
| 36 | #include <__type_traits/is_swappable.h> |
| 37 | #include <__type_traits/is_trivially_constructible.h> |
| 38 | #include <__type_traits/is_trivially_destructible.h> |
| 39 | #include <__type_traits/is_void.h> |
| 40 | #include <__type_traits/negation.h> |
| 41 | #include <__type_traits/remove_cv.h> |
| 42 | #include <__type_traits/remove_cvref.h> |
| 43 | #include <__utility/as_const.h> |
| 44 | #include <__utility/exception_guard.h> |
| 45 | #include <__utility/forward.h> |
| 46 | #include <__utility/in_place.h> |
| 47 | #include <__utility/move.h> |
| 48 | #include <__utility/swap.h> |
| 49 | #include <__verbose_abort> |
| 50 | #include <initializer_list> |
| 51 | |
| 52 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 53 | # pragma GCC system_header |
| 54 | #endif |
| 55 | |
| 56 | _LIBCPP_PUSH_MACROS |
| 57 | #include <__undef_macros> |
| 58 | |
| 59 | #if _LIBCPP_STD_VER >= 23 |
| 60 | |
| 61 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 62 | |
| 63 | template <class _Tp, class _Err> |
| 64 | class expected; |
| 65 | |
| 66 | template <class _Tp> |
| 67 | struct __is_std_expected : false_type {}; |
| 68 | |
| 69 | template <class _Tp, class _Err> |
| 70 | struct __is_std_expected<expected<_Tp, _Err>> : true_type {}; |
| 71 | |
| 72 | struct __expected_construct_in_place_from_invoke_tag {}; |
| 73 | struct __expected_construct_unexpected_from_invoke_tag {}; |
| 74 | |
| 75 | template <class _Err, class _Arg> |
| 76 | void __throw_bad_expected_access(_Arg&& __arg) { |
| 77 | # if _LIBCPP_HAS_EXCEPTIONS |
| 78 | throw bad_expected_access<_Err>(std::forward<_Arg>(__arg)); |
| 79 | # else |
| 80 | (void)__arg; |
| 81 | _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode" ); |
| 82 | # endif |
| 83 | } |
| 84 | |
| 85 | // If parameter type `_Tp` of `__conditional_no_unique_address` is neither |
| 86 | // copyable nor movable, a constructor with this tag is provided. For that |
| 87 | // constructor, the user has to provide a function and arguments. The function |
| 88 | // must return an object of type `_Tp`. When the function is invoked by the |
| 89 | // constructor, guaranteed copy elision kicks in and the `_Tp` is constructed |
| 90 | // in place. |
| 91 | struct __conditional_no_unique_address_invoke_tag {}; |
| 92 | |
| 93 | // This class implements an object with `[[no_unique_address]]` conditionally applied to it, |
| 94 | // based on the value of `_NoUnique`. |
| 95 | // |
| 96 | // A member of this class must always have `[[no_unique_address]]` applied to |
| 97 | // it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case |
| 98 | // would not have any effect. In the `false` case, the `__v` is not |
| 99 | // `[[no_unique_address]]`, so nullifies the effects of the "outer" |
| 100 | // `[[no_unique_address]]` regarding data layout. |
| 101 | // |
| 102 | // If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`. |
| 103 | template <bool _NoUnique, class _Tp> |
| 104 | struct __conditional_no_unique_address; |
| 105 | |
| 106 | template <class _Tp> |
| 107 | struct __conditional_no_unique_address<true, _Tp> { |
| 108 | template <class... _Args> |
| 109 | constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args) |
| 110 | : __v(std::forward<_Args>(__args)...) {} |
| 111 | |
| 112 | template <class _Func, class... _Args> |
| 113 | constexpr explicit __conditional_no_unique_address( |
| 114 | __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args) |
| 115 | : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 116 | |
| 117 | _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v; |
| 118 | }; |
| 119 | |
| 120 | template <class _Tp> |
| 121 | struct __conditional_no_unique_address<false, _Tp> { |
| 122 | template <class... _Args> |
| 123 | constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args) |
| 124 | : __v(std::forward<_Args>(__args)...) {} |
| 125 | |
| 126 | template <class _Func, class... _Args> |
| 127 | constexpr explicit __conditional_no_unique_address( |
| 128 | __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args) |
| 129 | : __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 130 | |
| 131 | _Tp __v; |
| 132 | }; |
| 133 | |
| 134 | // This function returns whether the type `_Second` can be stuffed into the tail padding |
| 135 | // of the `_First` type if both of them are given `[[no_unique_address]]`. |
| 136 | template <class _First, class _Second> |
| 137 | inline constexpr bool __fits_in_tail_padding = []() { |
| 138 | struct __x { |
| 139 | _LIBCPP_NO_UNIQUE_ADDRESS _First __first; |
| 140 | _LIBCPP_NO_UNIQUE_ADDRESS _Second __second; |
| 141 | }; |
| 142 | return sizeof(__x) == sizeof(_First); |
| 143 | }(); |
| 144 | |
| 145 | // This class implements the storage used by `std::expected`. We have a few |
| 146 | // goals for this storage: |
| 147 | // 1. Whenever the underlying {_Tp | _Unex} combination has free bytes in its |
| 148 | // tail padding, we should reuse it to store the bool discriminator of the |
| 149 | // expected, so as to save space. |
| 150 | // 2. Whenever the `expected<_Tp, _Unex>` as a whole has free bytes in its tail |
| 151 | // padding, we should allow an object following the expected to be stored in |
| 152 | // its tail padding. |
| 153 | // 3. However, we never want a user object (say `X`) that would follow an |
| 154 | // `expected<_Tp, _Unex>` to be stored in the padding bytes of the |
| 155 | // underlying {_Tp | _Unex} union, if any. That is because we use |
| 156 | // `construct_at` on that union, which would end up overwriting the `X` |
| 157 | // member if it is stored in the tail padding of the union. |
| 158 | // |
| 159 | // To achieve this, `__expected_base`'s logic is implemented in an inner |
| 160 | // `__repr` class. `__expected_base` holds one `__repr` member which is |
| 161 | // conditionally `[[no_unique_address]]`. The `__repr` class holds the |
| 162 | // underlying {_Tp | _Unex} union and a boolean "has value" flag. |
| 163 | // |
| 164 | // Which one of the `__repr_`/`__union_` members is `[[no_unique_address]]` |
| 165 | // depends on whether the "has value" boolean fits into the tail padding of |
| 166 | // the underlying {_Tp | _Unex} union: |
| 167 | // |
| 168 | // - In case the "has value" bool fits into the tail padding of the union, the |
| 169 | // whole `__repr_` member is _not_ `[[no_unique_address]]` as it needs to be |
| 170 | // transparently replaced on `emplace()`/`swap()` etc. |
| 171 | // - In case the "has value" bool does not fit into the tail padding of the |
| 172 | // union, only the union member must be transparently replaced (therefore is |
| 173 | // _not_ `[[no_unique_address]]`) and the "has value" flag must be adjusted |
| 174 | // manually. |
| 175 | // |
| 176 | // This way, the member that is transparently replaced on mutating operations |
| 177 | // is never `[[no_unique_address]]`, satisfying the requirements from |
| 178 | // "[basic.life]" in the standard. |
| 179 | // |
| 180 | // Stripped away of all superfluous elements, the layout of `__expected_base` |
| 181 | // then looks like this: |
| 182 | // |
| 183 | // template <class Tp, class Err> |
| 184 | // class expected_base { |
| 185 | // union union_t { |
| 186 | // [[no_unique_address]] Tp val; |
| 187 | // [[no_unique_address]] Err unex; |
| 188 | // }; |
| 189 | // |
| 190 | // static constexpr bool put_flag_in_tail = fits_in_tail_padding<union_t, bool>; |
| 191 | // static constexpr bool allow_reusing_expected_tail_padding = !put_flag_in_tail; |
| 192 | // |
| 193 | // struct repr { |
| 194 | // private: |
| 195 | // // If "has value" fits into the tail, this should be |
| 196 | // // `[[no_unique_address]]`, otherwise not. |
| 197 | // [[no_unique_address]] conditional_no_unique_address< |
| 198 | // put_flag_in_tail, |
| 199 | // union_t>::type union_; |
| 200 | // [[no_unique_address]] bool has_val_; |
| 201 | // }; |
| 202 | // |
| 203 | // protected: |
| 204 | // // If "has value" fits into the tail, this must _not_ be |
| 205 | // // `[[no_unique_address]]` so that we fill out the |
| 206 | // // complete `expected` object. |
| 207 | // [[no_unique_address]] conditional_no_unique_address< |
| 208 | // allow_reusing_expected_tail_padding, |
| 209 | // repr>::type repr_; |
| 210 | // }; |
| 211 | // |
| 212 | template <class _Tp, class _Err> |
| 213 | class __expected_base { |
| 214 | // use named union because [[no_unique_address]] cannot be applied to an unnamed union, |
| 215 | // also guaranteed elision into a potentially-overlapping subobject is unsettled (and |
| 216 | // it's not clear that it's implementable, given that the function is allowed to clobber |
| 217 | // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107. |
| 218 | union __union_t { |
| 219 | constexpr __union_t(const __union_t&) = delete; |
| 220 | constexpr __union_t(const __union_t&) |
| 221 | requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && |
| 222 | is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>) |
| 223 | = default; |
| 224 | constexpr __union_t(__union_t&&) = delete; |
| 225 | constexpr __union_t(__union_t&&) |
| 226 | requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && |
| 227 | is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>) |
| 228 | = default; |
| 229 | constexpr __union_t& operator=(const __union_t&) = delete; |
| 230 | constexpr __union_t& operator=(__union_t&&) = delete; |
| 231 | |
| 232 | template <class... _Args> |
| 233 | constexpr explicit __union_t(in_place_t, _Args&&... __args) : __val_(std::forward<_Args>(__args)...) {} |
| 234 | |
| 235 | template <class... _Args> |
| 236 | constexpr explicit __union_t(unexpect_t, _Args&&... __args) : __unex_(std::forward<_Args>(__args)...) {} |
| 237 | |
| 238 | template <class _Func, class... _Args> |
| 239 | constexpr explicit __union_t(std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args) |
| 240 | : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 241 | |
| 242 | template <class _Func, class... _Args> |
| 243 | constexpr explicit __union_t(std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) |
| 244 | : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 245 | |
| 246 | constexpr ~__union_t() |
| 247 | requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>) |
| 248 | = default; |
| 249 | |
| 250 | // __repr's destructor handles this |
| 251 | constexpr ~__union_t() {} |
| 252 | |
| 253 | _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_; |
| 254 | _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_; |
| 255 | }; |
| 256 | |
| 257 | static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>; |
| 258 | static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail; |
| 259 | |
| 260 | struct __repr { |
| 261 | constexpr explicit __repr() = delete; |
| 262 | |
| 263 | template <class... _Args> |
| 264 | constexpr explicit __repr(in_place_t __tag, _Args&&... __args) |
| 265 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {} |
| 266 | |
| 267 | template <class... _Args> |
| 268 | constexpr explicit __repr(unexpect_t __tag, _Args&&... __args) |
| 269 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {} |
| 270 | |
| 271 | template <class... _Args> |
| 272 | constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag, _Args&&... __args) |
| 273 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {} |
| 274 | |
| 275 | template <class... _Args> |
| 276 | constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag, _Args&&... __args) |
| 277 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {} |
| 278 | |
| 279 | // The return value of `__make_union` must be constructed in place in the |
| 280 | // `__v` member of `__union_`, relying on guaranteed copy elision. To do |
| 281 | // this, the `__conditional_no_unique_address_invoke_tag` constructor is |
| 282 | // called with a lambda that is immediately called inside |
| 283 | // `__conditional_no_unique_address`'s constructor. |
| 284 | template <class _OtherUnion> |
| 285 | constexpr explicit __repr(bool __has_val, _OtherUnion&& __other) |
| 286 | requires(__allow_reusing_expected_tail_padding) |
| 287 | : __union_(__conditional_no_unique_address_invoke_tag{}, |
| 288 | [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }), |
| 289 | __has_val_(__has_val) {} |
| 290 | |
| 291 | constexpr __repr(const __repr&) = delete; |
| 292 | constexpr __repr(const __repr&) |
| 293 | requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && |
| 294 | is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>) |
| 295 | = default; |
| 296 | constexpr __repr(__repr&&) = delete; |
| 297 | constexpr __repr(__repr&&) |
| 298 | requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && |
| 299 | is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>) |
| 300 | = default; |
| 301 | |
| 302 | constexpr __repr& operator=(const __repr&) = delete; |
| 303 | constexpr __repr& operator=(__repr&&) = delete; |
| 304 | |
| 305 | constexpr ~__repr() |
| 306 | requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>) |
| 307 | = default; |
| 308 | |
| 309 | constexpr ~__repr() |
| 310 | requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>) |
| 311 | { |
| 312 | __destroy_union_member(); |
| 313 | } |
| 314 | |
| 315 | constexpr void __destroy_union() |
| 316 | requires(__allow_reusing_expected_tail_padding && |
| 317 | (is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)) |
| 318 | { |
| 319 | // Note: Since the destructor of the union is trivial, this does nothing |
| 320 | // except to end the lifetime of the union. |
| 321 | std::destroy_at(&__union_.__v); |
| 322 | } |
| 323 | |
| 324 | constexpr void __destroy_union() |
| 325 | requires(__allow_reusing_expected_tail_padding && |
| 326 | (!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)) |
| 327 | { |
| 328 | __destroy_union_member(); |
| 329 | std::destroy_at(&__union_.__v); |
| 330 | } |
| 331 | |
| 332 | template <class... _Args> |
| 333 | constexpr void __construct_union(in_place_t, _Args&&... __args) |
| 334 | requires(__allow_reusing_expected_tail_padding) |
| 335 | { |
| 336 | std::construct_at(&__union_.__v, in_place, std::forward<_Args>(__args)...); |
| 337 | __has_val_ = true; |
| 338 | } |
| 339 | |
| 340 | template <class... _Args> |
| 341 | constexpr void __construct_union(unexpect_t, _Args&&... __args) |
| 342 | requires(__allow_reusing_expected_tail_padding) |
| 343 | { |
| 344 | std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...); |
| 345 | __has_val_ = false; |
| 346 | } |
| 347 | |
| 348 | private: |
| 349 | template <class, class> |
| 350 | friend class __expected_base; |
| 351 | |
| 352 | constexpr void __destroy_union_member() |
| 353 | requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>) |
| 354 | { |
| 355 | if (__has_val_) { |
| 356 | std::destroy_at(std::addressof(__union_.__v.__val_)); |
| 357 | } else { |
| 358 | std::destroy_at(std::addressof(__union_.__v.__unex_)); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | template <class _OtherUnion> |
| 363 | static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other) |
| 364 | requires(__allow_reusing_expected_tail_padding) |
| 365 | { |
| 366 | if (__has_val) |
| 367 | return __union_t(in_place, std::forward<_OtherUnion>(__other).__val_); |
| 368 | else |
| 369 | return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_); |
| 370 | } |
| 371 | |
| 372 | _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_; |
| 373 | _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_; |
| 374 | }; |
| 375 | |
| 376 | template <class _OtherUnion> |
| 377 | static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other) |
| 378 | requires(__put_flag_in_tail) |
| 379 | { |
| 380 | if (__has_val) |
| 381 | return __repr(in_place, std::forward<_OtherUnion>(__other).__val_); |
| 382 | else |
| 383 | return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_); |
| 384 | } |
| 385 | |
| 386 | protected: |
| 387 | template <class... _Args> |
| 388 | constexpr explicit __expected_base(_Args&&... __args) : __repr_(in_place, std::forward<_Args>(__args)...) {} |
| 389 | |
| 390 | // In case we copy/move construct from another `expected` we need to create |
| 391 | // our `expected` so that it either has a value or not, depending on the "has |
| 392 | // value" flag of the other `expected`. To do this without falling back on |
| 393 | // `std::construct_at` we rely on guaranteed copy elision using two helper |
| 394 | // functions `__make_repr` and `__make_union`. There have to be two since |
| 395 | // there are two data layouts with different members being |
| 396 | // `[[no_unique_address]]`. GCC (as of version 13) does not do guaranteed |
| 397 | // copy elision when initializing `[[no_unique_address]]` members. The two |
| 398 | // cases are: |
| 399 | // |
| 400 | // - `__make_repr`: This is used when the "has value" flag lives in the tail |
| 401 | // of the union. In this case, the `__repr` member is _not_ |
| 402 | // `[[no_unique_address]]`. |
| 403 | // - `__make_union`: When the "has value" flag does _not_ fit in the tail of |
| 404 | // the union, the `__repr` member is `[[no_unique_address]]` and the union |
| 405 | // is not. |
| 406 | // |
| 407 | // This constructor "catches" the first case and leaves the second case to |
| 408 | // `__union_t`, its constructors and `__make_union`. |
| 409 | template <class _OtherUnion> |
| 410 | constexpr explicit __expected_base(bool __has_val, _OtherUnion&& __other) |
| 411 | requires(__put_flag_in_tail) |
| 412 | : __repr_(__conditional_no_unique_address_invoke_tag{}, |
| 413 | [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {} |
| 414 | |
| 415 | constexpr void __destroy() { |
| 416 | if constexpr (__put_flag_in_tail) |
| 417 | std::destroy_at(&__repr_.__v); |
| 418 | else |
| 419 | __repr_.__v.__destroy_union(); |
| 420 | } |
| 421 | |
| 422 | template <class _Tag, class... _Args> |
| 423 | constexpr void __construct(_Tag __tag, _Args&&... __args) { |
| 424 | if constexpr (__put_flag_in_tail) |
| 425 | std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...); |
| 426 | else |
| 427 | __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...); |
| 428 | } |
| 429 | |
| 430 | constexpr bool __has_val() const { return __repr_.__v.__has_val_; } |
| 431 | constexpr __union_t& __union() { return __repr_.__v.__union_.__v; } |
| 432 | constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; } |
| 433 | constexpr _Tp& __val() { return __repr_.__v.__union_.__v.__val_; } |
| 434 | constexpr const _Tp& __val() const { return __repr_.__v.__union_.__v.__val_; } |
| 435 | constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; } |
| 436 | constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; } |
| 437 | |
| 438 | private: |
| 439 | _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_; |
| 440 | }; |
| 441 | |
| 442 | // Helper to handle comparisons that produce a value whose type is not bool, |
| 443 | // but allows only implicit (and not explicit) conversions to bool. |
| 444 | constexpr bool __into_bool(bool __b) noexcept { return __b; } |
| 445 | |
| 446 | template <class _Tp, class _Err> |
| 447 | class expected : private __expected_base<_Tp, _Err> { |
| 448 | static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> && |
| 449 | !is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value && |
| 450 | __valid_std_unexpected<_Err>::value, |
| 451 | "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a " |
| 452 | "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a " |
| 453 | "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the " |
| 454 | "definition of the template expected<T, E> with a type for the E parameter that is not a valid " |
| 455 | "template argument for unexpected is ill-formed." ); |
| 456 | |
| 457 | template <class _Up, class _OtherErr> |
| 458 | friend class expected; |
| 459 | |
| 460 | using __base _LIBCPP_NODEBUG = __expected_base<_Tp, _Err>; |
| 461 | |
| 462 | public: |
| 463 | using value_type = _Tp; |
| 464 | using error_type = _Err; |
| 465 | using unexpected_type = unexpected<_Err>; |
| 466 | |
| 467 | using __trivially_relocatable _LIBCPP_NODEBUG = |
| 468 | __conditional_t<__is_trivially_relocatable_v<_Tp> && __is_trivially_relocatable_v<_Err>, expected, void>; |
| 469 | |
| 470 | template <class _Up> |
| 471 | using rebind = expected<_Up, error_type>; |
| 472 | |
| 473 | // [expected.object.ctor], constructors |
| 474 | constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened |
| 475 | requires is_default_constructible_v<_Tp> |
| 476 | : __base(in_place) {} |
| 477 | |
| 478 | constexpr expected(const expected&) = delete; |
| 479 | |
| 480 | constexpr expected(const expected&) |
| 481 | requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> && |
| 482 | is_trivially_copy_constructible_v<_Err>) |
| 483 | = default; |
| 484 | |
| 485 | constexpr expected(const expected& __other) noexcept( |
| 486 | is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened |
| 487 | requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && |
| 488 | !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)) |
| 489 | : __base(__other.__has_val(), __other.__union()) {} |
| 490 | |
| 491 | constexpr expected(expected&&) |
| 492 | requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> && |
| 493 | is_trivially_move_constructible_v<_Err>) |
| 494 | = default; |
| 495 | |
| 496 | constexpr expected(expected&& __other) noexcept(is_nothrow_move_constructible_v<_Tp> && |
| 497 | is_nothrow_move_constructible_v<_Err>) |
| 498 | requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && |
| 499 | !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)) |
| 500 | : __base(__other.__has_val(), std::move(__other.__union())) {} |
| 501 | |
| 502 | private: |
| 503 | template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual> |
| 504 | using __can_convert _LIBCPP_NODEBUG = _And< |
| 505 | is_constructible<_Tp, _UfQual>, |
| 506 | is_constructible<_Err, _OtherErrQual>, |
| 507 | _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value, |
| 508 | _And< _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>, // use the copy constructor instead, see #92676 |
| 509 | _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>, |
| 510 | _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>, |
| 511 | _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>, |
| 512 | _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>, |
| 513 | _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>, |
| 514 | _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>, |
| 515 | _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>, |
| 516 | _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>, |
| 517 | true_type>, |
| 518 | _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>, |
| 519 | _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>, |
| 520 | _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>, |
| 521 | _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >; |
| 522 | |
| 523 | template <class _Func, class... _Args> |
| 524 | constexpr explicit expected(std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) |
| 525 | : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {} |
| 526 | |
| 527 | template <class _Func, class... _Args> |
| 528 | constexpr explicit expected( |
| 529 | std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) |
| 530 | : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {} |
| 531 | |
| 532 | public: |
| 533 | template <class _Up, class _OtherErr> |
| 534 | requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value |
| 535 | constexpr explicit(!is_convertible_v<const _Up&, _Tp> || !is_convertible_v<const _OtherErr&, _Err>) |
| 536 | expected(const expected<_Up, _OtherErr>& __other) noexcept( |
| 537 | is_nothrow_constructible_v<_Tp, const _Up&> && |
| 538 | is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened |
| 539 | : __base(__other.__has_val(), __other.__union()) {} |
| 540 | |
| 541 | template <class _Up, class _OtherErr> |
| 542 | requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value |
| 543 | constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>) |
| 544 | expected(expected<_Up, _OtherErr>&& __other) noexcept( |
| 545 | is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened |
| 546 | : __base(__other.__has_val(), std::move(__other.__union())) {} |
| 547 | |
| 548 | template <class _Up = remove_cv_t<_Tp>> |
| 549 | requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> && |
| 550 | !is_same_v<remove_cvref_t<_Up>, unexpect_t> && is_constructible_v<_Tp, _Up> && |
| 551 | !__is_std_unexpected<remove_cvref_t<_Up>>::value && |
| 552 | (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value)) |
| 553 | constexpr explicit(!is_convertible_v<_Up, _Tp>) |
| 554 | expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened |
| 555 | : __base(in_place, std::forward<_Up>(__u)) {} |
| 556 | |
| 557 | template <class _OtherErr> |
| 558 | requires is_constructible_v<_Err, const _OtherErr&> |
| 559 | constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(const unexpected<_OtherErr>& __unex) noexcept( |
| 560 | is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened |
| 561 | : __base(unexpect, __unex.error()) {} |
| 562 | |
| 563 | template <class _OtherErr> |
| 564 | requires is_constructible_v<_Err, _OtherErr> |
| 565 | constexpr explicit(!is_convertible_v<_OtherErr, _Err>) |
| 566 | expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened |
| 567 | : __base(unexpect, std::move(__unex.error())) {} |
| 568 | |
| 569 | template <class... _Args> |
| 570 | requires is_constructible_v<_Tp, _Args...> |
| 571 | constexpr explicit expected(in_place_t, |
| 572 | _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, _Args...>) // strengthened |
| 573 | : __base(in_place, std::forward<_Args>(__args)...) {} |
| 574 | |
| 575 | template <class _Up, class... _Args> |
| 576 | requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... > |
| 577 | constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept( |
| 578 | is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened |
| 579 | : __base(in_place, __il, std::forward<_Args>(__args)...) {} |
| 580 | |
| 581 | template <class... _Args> |
| 582 | requires is_constructible_v<_Err, _Args...> |
| 583 | constexpr explicit expected(unexpect_t, |
| 584 | _Args&&... __args) noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened |
| 585 | : __base(unexpect, std::forward<_Args>(__args)...) {} |
| 586 | |
| 587 | template <class _Up, class... _Args> |
| 588 | requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... > |
| 589 | constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept( |
| 590 | is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened |
| 591 | : __base(unexpect, __il, std::forward<_Args>(__args)...) {} |
| 592 | |
| 593 | // [expected.object.dtor], destructor |
| 594 | |
| 595 | constexpr ~expected() = default; |
| 596 | |
| 597 | private: |
| 598 | template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args> |
| 599 | constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) { |
| 600 | if constexpr (is_nothrow_constructible_v<_T1, _Args...>) { |
| 601 | this->__destroy(); |
| 602 | this->__construct(_Tag{}, std::forward<_Args>(__args)...); |
| 603 | } else if constexpr (is_nothrow_move_constructible_v<_T1>) { |
| 604 | _T1 __tmp(std::forward<_Args>(__args)...); |
| 605 | this->__destroy(); |
| 606 | this->__construct(_Tag{}, std::move(__tmp)); |
| 607 | } else { |
| 608 | static_assert( |
| 609 | is_nothrow_move_constructible_v<_T2>, |
| 610 | "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can " |
| 611 | "be reverted to the previous state in case an exception is thrown during the assignment." ); |
| 612 | _T2 __tmp(std::move(__oldval)); |
| 613 | this->__destroy(); |
| 614 | auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); }); |
| 615 | this->__construct(_Tag{}, std::forward<_Args>(__args)...); |
| 616 | __trans.__complete(); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | public: |
| 621 | // [expected.object.assign], assignment |
| 622 | constexpr expected& operator=(const expected&) = delete; |
| 623 | |
| 624 | constexpr expected& operator=(const expected& __rhs) noexcept( |
| 625 | is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> && |
| 626 | is_nothrow_copy_constructible_v<_Err>) // strengthened |
| 627 | requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> && |
| 628 | is_copy_constructible_v<_Err> && |
| 629 | (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>)) |
| 630 | { |
| 631 | if (this->__has_val() && __rhs.__has_val()) { |
| 632 | this->__val() = __rhs.__val(); |
| 633 | } else if (this->__has_val()) { |
| 634 | __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex()); |
| 635 | } else if (__rhs.__has_val()) { |
| 636 | __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val()); |
| 637 | } else { |
| 638 | this->__unex() = __rhs.__unex(); |
| 639 | } |
| 640 | return *this; |
| 641 | } |
| 642 | |
| 643 | constexpr expected& |
| 644 | operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> && |
| 645 | is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>) |
| 646 | requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> && |
| 647 | is_move_assignable_v<_Err> && |
| 648 | (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>)) |
| 649 | { |
| 650 | if (this->__has_val() && __rhs.__has_val()) { |
| 651 | this->__val() = std::move(__rhs.__val()); |
| 652 | } else if (this->__has_val()) { |
| 653 | __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex())); |
| 654 | } else if (__rhs.__has_val()) { |
| 655 | __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val())); |
| 656 | } else { |
| 657 | this->__unex() = std::move(__rhs.__unex()); |
| 658 | } |
| 659 | return *this; |
| 660 | } |
| 661 | |
| 662 | template <class _Up = remove_cv_t<_Tp>> |
| 663 | constexpr expected& operator=(_Up&& __v) |
| 664 | requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value && |
| 665 | is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> && |
| 666 | (is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> || |
| 667 | is_nothrow_move_constructible_v<_Err>)) |
| 668 | { |
| 669 | if (this->__has_val()) { |
| 670 | this->__val() = std::forward<_Up>(__v); |
| 671 | } else { |
| 672 | __reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v)); |
| 673 | } |
| 674 | return *this; |
| 675 | } |
| 676 | |
| 677 | private: |
| 678 | template <class _OtherErrQual> |
| 679 | static constexpr bool __can_assign_from_unexpected = |
| 680 | _And<is_constructible<_Err, _OtherErrQual>, |
| 681 | is_assignable<_Err&, _OtherErrQual>, |
| 682 | _Or<is_nothrow_constructible<_Err, _OtherErrQual>, |
| 683 | is_nothrow_move_constructible<_Tp>, |
| 684 | is_nothrow_move_constructible<_Err>>>::value; |
| 685 | |
| 686 | public: |
| 687 | template <class _OtherErr> |
| 688 | requires(__can_assign_from_unexpected<const _OtherErr&>) |
| 689 | constexpr expected& operator=(const unexpected<_OtherErr>& __un) { |
| 690 | if (this->__has_val()) { |
| 691 | __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error()); |
| 692 | } else { |
| 693 | this->__unex() = __un.error(); |
| 694 | } |
| 695 | return *this; |
| 696 | } |
| 697 | |
| 698 | template <class _OtherErr> |
| 699 | requires(__can_assign_from_unexpected<_OtherErr>) |
| 700 | constexpr expected& operator=(unexpected<_OtherErr>&& __un) { |
| 701 | if (this->__has_val()) { |
| 702 | __reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error())); |
| 703 | } else { |
| 704 | this->__unex() = std::move(__un.error()); |
| 705 | } |
| 706 | return *this; |
| 707 | } |
| 708 | |
| 709 | template <class... _Args> |
| 710 | requires is_nothrow_constructible_v<_Tp, _Args...> |
| 711 | constexpr _Tp& emplace(_Args&&... __args) noexcept { |
| 712 | this->__destroy(); |
| 713 | this->__construct(in_place, std::forward<_Args>(__args)...); |
| 714 | return this->__val(); |
| 715 | } |
| 716 | |
| 717 | template <class _Up, class... _Args> |
| 718 | requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...> |
| 719 | constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept { |
| 720 | this->__destroy(); |
| 721 | this->__construct(in_place, __il, std::forward<_Args>(__args)...); |
| 722 | return this->__val(); |
| 723 | } |
| 724 | |
| 725 | public: |
| 726 | // [expected.object.swap], swap |
| 727 | constexpr void swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> && |
| 728 | is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>) |
| 729 | requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> && |
| 730 | is_move_constructible_v<_Err> && |
| 731 | (is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>)) |
| 732 | { |
| 733 | auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) { |
| 734 | if constexpr (is_nothrow_move_constructible_v<_Err>) { |
| 735 | _Err __tmp(std::move(__with_err.__unex())); |
| 736 | __with_err.__destroy(); |
| 737 | auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); }); |
| 738 | __with_err.__construct(in_place, std::move(__with_val.__val())); |
| 739 | __trans.__complete(); |
| 740 | __with_val.__destroy(); |
| 741 | __with_val.__construct(unexpect, std::move(__tmp)); |
| 742 | } else { |
| 743 | static_assert(is_nothrow_move_constructible_v<_Tp>, |
| 744 | "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so " |
| 745 | "that it can be reverted to the previous state in case an exception is thrown during swap." ); |
| 746 | _Tp __tmp(std::move(__with_val.__val())); |
| 747 | __with_val.__destroy(); |
| 748 | auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); }); |
| 749 | __with_val.__construct(unexpect, std::move(__with_err.__unex())); |
| 750 | __trans.__complete(); |
| 751 | __with_err.__destroy(); |
| 752 | __with_err.__construct(in_place, std::move(__tmp)); |
| 753 | } |
| 754 | }; |
| 755 | |
| 756 | if (this->__has_val()) { |
| 757 | if (__rhs.__has_val()) { |
| 758 | using std::swap; |
| 759 | swap(this->__val(), __rhs.__val()); |
| 760 | } else { |
| 761 | __swap_val_unex_impl(*this, __rhs); |
| 762 | } |
| 763 | } else { |
| 764 | if (__rhs.__has_val()) { |
| 765 | __swap_val_unex_impl(__rhs, *this); |
| 766 | } else { |
| 767 | using std::swap; |
| 768 | swap(this->__unex(), __rhs.__unex()); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(rhs&: __y))) |
| 774 | requires requires { __x.swap(rhs&: __y); } |
| 775 | { |
| 776 | __x.swap(rhs&: __y); |
| 777 | } |
| 778 | |
| 779 | // [expected.object.obs], observers |
| 780 | constexpr const _Tp* operator->() const noexcept { |
| 781 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 782 | this->__has_val(), "expected::operator-> requires the expected to contain a value" ); |
| 783 | return std::addressof(this->__val()); |
| 784 | } |
| 785 | |
| 786 | constexpr _Tp* operator->() noexcept { |
| 787 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 788 | this->__has_val(), "expected::operator-> requires the expected to contain a value" ); |
| 789 | return std::addressof(this->__val()); |
| 790 | } |
| 791 | |
| 792 | [[nodiscard]] constexpr const _Tp& operator*() const& noexcept { |
| 793 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 794 | this->__has_val(), "expected::operator* requires the expected to contain a value" ); |
| 795 | return this->__val(); |
| 796 | } |
| 797 | |
| 798 | [[nodiscard]] constexpr _Tp& operator*() & noexcept { |
| 799 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 800 | this->__has_val(), "expected::operator* requires the expected to contain a value" ); |
| 801 | return this->__val(); |
| 802 | } |
| 803 | |
| 804 | [[nodiscard]] constexpr const _Tp&& operator*() const&& noexcept { |
| 805 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 806 | this->__has_val(), "expected::operator* requires the expected to contain a value" ); |
| 807 | return std::move(this->__val()); |
| 808 | } |
| 809 | |
| 810 | [[nodiscard]] constexpr _Tp&& operator*() && noexcept { |
| 811 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 812 | this->__has_val(), "expected::operator* requires the expected to contain a value" ); |
| 813 | return std::move(this->__val()); |
| 814 | } |
| 815 | |
| 816 | constexpr explicit operator bool() const noexcept { return this->__has_val(); } |
| 817 | |
| 818 | [[nodiscard]] constexpr bool has_value() const noexcept { return this->__has_val(); } |
| 819 | |
| 820 | # if _LIBCPP_STD_VER >= 29 |
| 821 | [[nodiscard]] constexpr bool has_error() const noexcept { return !this->has_value(); } |
| 822 | # endif |
| 823 | |
| 824 | [[nodiscard]] constexpr const _Tp& value() const& { |
| 825 | static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible" ); |
| 826 | if (!this->__has_val()) { |
| 827 | std::__throw_bad_expected_access<_Err>(std::as_const(error())); |
| 828 | } |
| 829 | return this->__val(); |
| 830 | } |
| 831 | |
| 832 | [[nodiscard]] constexpr _Tp& value() & { |
| 833 | static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible" ); |
| 834 | if (!this->__has_val()) { |
| 835 | std::__throw_bad_expected_access<_Err>(std::as_const(error())); |
| 836 | } |
| 837 | return this->__val(); |
| 838 | } |
| 839 | |
| 840 | [[nodiscard]] constexpr const _Tp&& value() const&& { |
| 841 | static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>, |
| 842 | "error_type has to be both copy constructible and constructible from decltype(std::move(error()))" ); |
| 843 | if (!this->__has_val()) { |
| 844 | std::__throw_bad_expected_access<_Err>(std::move(error())); |
| 845 | } |
| 846 | return std::move(this->__val()); |
| 847 | } |
| 848 | |
| 849 | [[nodiscard]] constexpr _Tp&& value() && { |
| 850 | static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>, |
| 851 | "error_type has to be both copy constructible and constructible from decltype(std::move(error()))" ); |
| 852 | if (!this->__has_val()) { |
| 853 | std::__throw_bad_expected_access<_Err>(std::move(error())); |
| 854 | } |
| 855 | return std::move(this->__val()); |
| 856 | } |
| 857 | |
| 858 | [[nodiscard]] constexpr const _Err& error() const& noexcept { |
| 859 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 860 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 861 | return this->__unex(); |
| 862 | } |
| 863 | |
| 864 | [[nodiscard]] constexpr _Err& error() & noexcept { |
| 865 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 866 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 867 | return this->__unex(); |
| 868 | } |
| 869 | |
| 870 | [[nodiscard]] constexpr const _Err&& error() const&& noexcept { |
| 871 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 872 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 873 | return std::move(this->__unex()); |
| 874 | } |
| 875 | |
| 876 | [[nodiscard]] constexpr _Err&& error() && noexcept { |
| 877 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 878 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 879 | return std::move(this->__unex()); |
| 880 | } |
| 881 | |
| 882 | template <class _Up = remove_cv_t<_Tp>> |
| 883 | [[nodiscard]] constexpr _Tp value_or(_Up&& __v) const& { |
| 884 | static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible" ); |
| 885 | static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type" ); |
| 886 | return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v)); |
| 887 | } |
| 888 | |
| 889 | template <class _Up = remove_cv_t<_Tp>> |
| 890 | [[nodiscard]] constexpr _Tp value_or(_Up&& __v) && { |
| 891 | static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible" ); |
| 892 | static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type" ); |
| 893 | return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v)); |
| 894 | } |
| 895 | |
| 896 | template <class _Up = _Err> |
| 897 | [[nodiscard]] constexpr _Err error_or(_Up&& __error) const& { |
| 898 | static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible" ); |
| 899 | static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type" ); |
| 900 | if (has_value()) |
| 901 | return std::forward<_Up>(__error); |
| 902 | return error(); |
| 903 | } |
| 904 | |
| 905 | template <class _Up = _Err> |
| 906 | [[nodiscard]] constexpr _Err error_or(_Up&& __error) && { |
| 907 | static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible" ); |
| 908 | static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type" ); |
| 909 | if (has_value()) |
| 910 | return std::forward<_Up>(__error); |
| 911 | return std::move(error()); |
| 912 | } |
| 913 | |
| 914 | // [expected.void.monadic], monadic |
| 915 | template <class _Func> |
| 916 | requires is_constructible_v<_Err, _Err&> |
| 917 | [[nodiscard]] constexpr auto and_then(_Func&& __f) & { |
| 918 | using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>; |
| 919 | static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected" ); |
| 920 | static_assert(is_same_v<typename _Up::error_type, _Err>, |
| 921 | "The result of f(value()) must have the same error_type as this expected" ); |
| 922 | if (has_value()) { |
| 923 | return std::invoke(std::forward<_Func>(__f), this->__val()); |
| 924 | } |
| 925 | return _Up(unexpect, error()); |
| 926 | } |
| 927 | |
| 928 | template <class _Func> |
| 929 | requires is_constructible_v<_Err, const _Err&> |
| 930 | [[nodiscard]] constexpr auto and_then(_Func&& __f) const& { |
| 931 | using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>; |
| 932 | static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected" ); |
| 933 | static_assert(is_same_v<typename _Up::error_type, _Err>, |
| 934 | "The result of f(value()) must have the same error_type as this expected" ); |
| 935 | if (has_value()) { |
| 936 | return std::invoke(std::forward<_Func>(__f), this->__val()); |
| 937 | } |
| 938 | return _Up(unexpect, error()); |
| 939 | } |
| 940 | |
| 941 | template <class _Func> |
| 942 | requires is_constructible_v<_Err, _Err&&> |
| 943 | [[nodiscard]] constexpr auto and_then(_Func&& __f) && { |
| 944 | using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>; |
| 945 | static_assert( |
| 946 | __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected" ); |
| 947 | static_assert(is_same_v<typename _Up::error_type, _Err>, |
| 948 | "The result of f(std::move(value())) must have the same error_type as this expected" ); |
| 949 | if (has_value()) { |
| 950 | return std::invoke(std::forward<_Func>(__f), std::move(this->__val())); |
| 951 | } |
| 952 | return _Up(unexpect, std::move(error())); |
| 953 | } |
| 954 | |
| 955 | template <class _Func> |
| 956 | requires is_constructible_v<_Err, const _Err&&> |
| 957 | [[nodiscard]] constexpr auto and_then(_Func&& __f) const&& { |
| 958 | using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>; |
| 959 | static_assert( |
| 960 | __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected" ); |
| 961 | static_assert(is_same_v<typename _Up::error_type, _Err>, |
| 962 | "The result of f(std::move(value())) must have the same error_type as this expected" ); |
| 963 | if (has_value()) { |
| 964 | return std::invoke(std::forward<_Func>(__f), std::move(this->__val())); |
| 965 | } |
| 966 | return _Up(unexpect, std::move(error())); |
| 967 | } |
| 968 | |
| 969 | template <class _Func> |
| 970 | requires is_constructible_v<_Tp, _Tp&> |
| 971 | [[nodiscard]] constexpr auto or_else(_Func&& __f) & { |
| 972 | using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>; |
| 973 | static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected" ); |
| 974 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 975 | "The result of f(error()) must have the same value_type as this expected" ); |
| 976 | if (has_value()) { |
| 977 | return _Gp(in_place, this->__val()); |
| 978 | } |
| 979 | return std::invoke(std::forward<_Func>(__f), error()); |
| 980 | } |
| 981 | |
| 982 | template <class _Func> |
| 983 | requires is_constructible_v<_Tp, const _Tp&> |
| 984 | [[nodiscard]] constexpr auto or_else(_Func&& __f) const& { |
| 985 | using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>; |
| 986 | static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected" ); |
| 987 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 988 | "The result of f(error()) must have the same value_type as this expected" ); |
| 989 | if (has_value()) { |
| 990 | return _Gp(in_place, this->__val()); |
| 991 | } |
| 992 | return std::invoke(std::forward<_Func>(__f), error()); |
| 993 | } |
| 994 | |
| 995 | template <class _Func> |
| 996 | requires is_constructible_v<_Tp, _Tp&&> |
| 997 | [[nodiscard]] constexpr auto or_else(_Func&& __f) && { |
| 998 | using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>; |
| 999 | static_assert( |
| 1000 | __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected" ); |
| 1001 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1002 | "The result of f(std::move(error())) must have the same value_type as this expected" ); |
| 1003 | if (has_value()) { |
| 1004 | return _Gp(in_place, std::move(this->__val())); |
| 1005 | } |
| 1006 | return std::invoke(std::forward<_Func>(__f), std::move(error())); |
| 1007 | } |
| 1008 | |
| 1009 | template <class _Func> |
| 1010 | requires is_constructible_v<_Tp, const _Tp&&> |
| 1011 | [[nodiscard]] constexpr auto or_else(_Func&& __f) const&& { |
| 1012 | using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>; |
| 1013 | static_assert( |
| 1014 | __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected" ); |
| 1015 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1016 | "The result of f(std::move(error())) must have the same value_type as this expected" ); |
| 1017 | if (has_value()) { |
| 1018 | return _Gp(in_place, std::move(this->__val())); |
| 1019 | } |
| 1020 | return std::invoke(std::forward<_Func>(__f), std::move(error())); |
| 1021 | } |
| 1022 | |
| 1023 | template <class _Func> |
| 1024 | requires is_constructible_v<_Err, _Err&> |
| 1025 | [[nodiscard]] constexpr auto transform(_Func&& __f) & { |
| 1026 | using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>; |
| 1027 | if (!has_value()) { |
| 1028 | return expected<_Up, _Err>(unexpect, error()); |
| 1029 | } |
| 1030 | if constexpr (!is_void_v<_Up>) { |
| 1031 | return expected<_Up, _Err>( |
| 1032 | __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val()); |
| 1033 | } else { |
| 1034 | std::invoke(std::forward<_Func>(__f), this->__val()); |
| 1035 | return expected<_Up, _Err>(); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | template <class _Func> |
| 1040 | requires is_constructible_v<_Err, const _Err&> |
| 1041 | [[nodiscard]] constexpr auto transform(_Func&& __f) const& { |
| 1042 | using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>; |
| 1043 | if (!has_value()) { |
| 1044 | return expected<_Up, _Err>(unexpect, error()); |
| 1045 | } |
| 1046 | if constexpr (!is_void_v<_Up>) { |
| 1047 | return expected<_Up, _Err>( |
| 1048 | __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val()); |
| 1049 | } else { |
| 1050 | std::invoke(std::forward<_Func>(__f), this->__val()); |
| 1051 | return expected<_Up, _Err>(); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | template <class _Func> |
| 1056 | requires is_constructible_v<_Err, _Err&&> |
| 1057 | [[nodiscard]] constexpr auto transform(_Func&& __f) && { |
| 1058 | using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>; |
| 1059 | if (!has_value()) { |
| 1060 | return expected<_Up, _Err>(unexpect, std::move(error())); |
| 1061 | } |
| 1062 | if constexpr (!is_void_v<_Up>) { |
| 1063 | return expected<_Up, _Err>( |
| 1064 | __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val())); |
| 1065 | } else { |
| 1066 | std::invoke(std::forward<_Func>(__f), std::move(this->__val())); |
| 1067 | return expected<_Up, _Err>(); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | template <class _Func> |
| 1072 | requires is_constructible_v<_Err, const _Err&&> |
| 1073 | [[nodiscard]] constexpr auto transform(_Func&& __f) const&& { |
| 1074 | using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>; |
| 1075 | if (!has_value()) { |
| 1076 | return expected<_Up, _Err>(unexpect, std::move(error())); |
| 1077 | } |
| 1078 | if constexpr (!is_void_v<_Up>) { |
| 1079 | return expected<_Up, _Err>( |
| 1080 | __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val())); |
| 1081 | } else { |
| 1082 | std::invoke(std::forward<_Func>(__f), std::move(this->__val())); |
| 1083 | return expected<_Up, _Err>(); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | template <class _Func> |
| 1088 | requires is_constructible_v<_Tp, _Tp&> |
| 1089 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) & { |
| 1090 | using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>; |
| 1091 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1092 | "The result of f(error()) must be a valid template argument for unexpected" ); |
| 1093 | if (has_value()) { |
| 1094 | return expected<_Tp, _Gp>(in_place, this->__val()); |
| 1095 | } |
| 1096 | return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); |
| 1097 | } |
| 1098 | |
| 1099 | template <class _Func> |
| 1100 | requires is_constructible_v<_Tp, const _Tp&> |
| 1101 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) const& { |
| 1102 | using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>; |
| 1103 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1104 | "The result of f(error()) must be a valid template argument for unexpected" ); |
| 1105 | if (has_value()) { |
| 1106 | return expected<_Tp, _Gp>(in_place, this->__val()); |
| 1107 | } |
| 1108 | return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); |
| 1109 | } |
| 1110 | |
| 1111 | template <class _Func> |
| 1112 | requires is_constructible_v<_Tp, _Tp&&> |
| 1113 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) && { |
| 1114 | using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>; |
| 1115 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1116 | "The result of f(std::move(error())) must be a valid template argument for unexpected" ); |
| 1117 | if (has_value()) { |
| 1118 | return expected<_Tp, _Gp>(in_place, std::move(this->__val())); |
| 1119 | } |
| 1120 | return expected<_Tp, _Gp>( |
| 1121 | __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); |
| 1122 | } |
| 1123 | |
| 1124 | template <class _Func> |
| 1125 | requires is_constructible_v<_Tp, const _Tp&&> |
| 1126 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) const&& { |
| 1127 | using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>; |
| 1128 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1129 | "The result of f(std::move(error())) must be a valid template argument for unexpected" ); |
| 1130 | if (has_value()) { |
| 1131 | return expected<_Tp, _Gp>(in_place, std::move(this->__val())); |
| 1132 | } |
| 1133 | return expected<_Tp, _Gp>( |
| 1134 | __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); |
| 1135 | } |
| 1136 | |
| 1137 | // [expected.object.eq], equality operators |
| 1138 | template <class _T2, class _E2> |
| 1139 | friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) |
| 1140 | requires(!is_void_v<_T2>) |
| 1141 | # if _LIBCPP_STD_VER >= 26 |
| 1142 | && requires { |
| 1143 | { *__x == *__y }->__core_convertible_to<bool>; |
| 1144 | { __x.error() == __y.error() }->__core_convertible_to<bool>; |
| 1145 | } |
| 1146 | # endif |
| 1147 | { |
| 1148 | if (__x.__has_val() != __y.__has_val()) { |
| 1149 | return false; |
| 1150 | } else { |
| 1151 | if (__x.__has_val()) { |
| 1152 | return __x.__val() == __y.__val(); |
| 1153 | } else { |
| 1154 | return __x.__unex() == __y.__unex(); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // The unusual signature avoids constraint recursion via ADL through |
| 1160 | // std::expected, see https://llvm.org/PR160431. Note that this only |
| 1161 | // triggers with compilers that implement https://wg21.link/CWG2369. |
| 1162 | template <class _T2, same_as<_Tp> _Tp2> |
| 1163 | friend constexpr bool operator==(const expected<_Tp2, _Err>& __x, const _T2& __v) |
| 1164 | # if _LIBCPP_STD_VER >= 26 |
| 1165 | requires(!__is_std_expected<_T2>::value) && requires { |
| 1166 | { *__x == __v }->__core_convertible_to<bool>; |
| 1167 | } |
| 1168 | # endif |
| 1169 | { |
| 1170 | return __x.__has_val() && std::__into_bool(b: __x.__val() == __v); |
| 1171 | } |
| 1172 | |
| 1173 | template <class _E2> |
| 1174 | friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) |
| 1175 | # if _LIBCPP_STD_VER >= 26 |
| 1176 | requires requires { |
| 1177 | { __x.error() == __e.error() }->__core_convertible_to<bool>; |
| 1178 | } |
| 1179 | # endif |
| 1180 | { |
| 1181 | return !__x.__has_val() && std::__into_bool(b: __x.__unex() == __e.error()); |
| 1182 | } |
| 1183 | }; |
| 1184 | |
| 1185 | template <class _Err> |
| 1186 | class __expected_void_base { |
| 1187 | struct __empty_t {}; |
| 1188 | // use named union because [[no_unique_address]] cannot be applied to an unnamed union, |
| 1189 | // also guaranteed elision into a potentially-overlapping subobject is unsettled (and |
| 1190 | // it's not clear that it's implementable, given that the function is allowed to clobber |
| 1191 | // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107. |
| 1192 | union __union_t { |
| 1193 | constexpr __union_t(const __union_t&) = delete; |
| 1194 | constexpr __union_t(const __union_t&) |
| 1195 | requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>) |
| 1196 | = default; |
| 1197 | constexpr __union_t(__union_t&&) = delete; |
| 1198 | constexpr __union_t(__union_t&&) |
| 1199 | requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>) |
| 1200 | = default; |
| 1201 | constexpr __union_t& operator=(const __union_t&) = delete; |
| 1202 | constexpr __union_t& operator=(__union_t&&) = delete; |
| 1203 | |
| 1204 | constexpr explicit __union_t(in_place_t) : __empty_() {} |
| 1205 | |
| 1206 | template <class... _Args> |
| 1207 | constexpr explicit __union_t(unexpect_t, _Args&&... __args) : __unex_(std::forward<_Args>(__args)...) {} |
| 1208 | |
| 1209 | template <class _Func, class... _Args> |
| 1210 | constexpr explicit __union_t(__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) |
| 1211 | : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 1212 | |
| 1213 | constexpr ~__union_t() |
| 1214 | requires(is_trivially_destructible_v<_Err>) |
| 1215 | = default; |
| 1216 | |
| 1217 | // __repr's destructor handles this |
| 1218 | constexpr ~__union_t() |
| 1219 | requires(!is_trivially_destructible_v<_Err>) |
| 1220 | {} |
| 1221 | |
| 1222 | _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_; |
| 1223 | _LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_; |
| 1224 | }; |
| 1225 | |
| 1226 | static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>; |
| 1227 | static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail; |
| 1228 | |
| 1229 | struct __repr { |
| 1230 | constexpr explicit __repr() = delete; |
| 1231 | |
| 1232 | template <class... _Args> |
| 1233 | constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {} |
| 1234 | |
| 1235 | template <class... _Args> |
| 1236 | constexpr explicit __repr(unexpect_t __tag, _Args&&... __args) |
| 1237 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {} |
| 1238 | |
| 1239 | template <class... _Args> |
| 1240 | constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag, _Args&&... __args) |
| 1241 | : __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {} |
| 1242 | |
| 1243 | template <class _OtherUnion> |
| 1244 | constexpr explicit __repr(bool __has_val, _OtherUnion&& __other) |
| 1245 | requires(__allow_reusing_expected_tail_padding) |
| 1246 | : __union_(__conditional_no_unique_address_invoke_tag{}, |
| 1247 | [&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }), |
| 1248 | __has_val_(__has_val) {} |
| 1249 | |
| 1250 | constexpr __repr(const __repr&) = delete; |
| 1251 | constexpr __repr(const __repr&) |
| 1252 | requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>) |
| 1253 | = default; |
| 1254 | constexpr __repr(__repr&&) = delete; |
| 1255 | constexpr __repr(__repr&&) |
| 1256 | requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>) |
| 1257 | = default; |
| 1258 | |
| 1259 | constexpr __repr& operator=(const __repr&) = delete; |
| 1260 | constexpr __repr& operator=(__repr&&) = delete; |
| 1261 | |
| 1262 | constexpr ~__repr() |
| 1263 | requires(is_trivially_destructible_v<_Err>) |
| 1264 | = default; |
| 1265 | |
| 1266 | constexpr ~__repr() |
| 1267 | requires(!is_trivially_destructible_v<_Err>) |
| 1268 | { |
| 1269 | __destroy_union_member(); |
| 1270 | } |
| 1271 | |
| 1272 | constexpr void __destroy_union() |
| 1273 | requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>) |
| 1274 | { |
| 1275 | std::destroy_at(&__union_.__v); |
| 1276 | } |
| 1277 | |
| 1278 | constexpr void __destroy_union() |
| 1279 | requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>) |
| 1280 | { |
| 1281 | __destroy_union_member(); |
| 1282 | std::destroy_at(&__union_.__v); |
| 1283 | } |
| 1284 | |
| 1285 | constexpr void __construct_union(in_place_t) |
| 1286 | requires(__allow_reusing_expected_tail_padding) |
| 1287 | { |
| 1288 | std::construct_at(&__union_.__v, in_place); |
| 1289 | __has_val_ = true; |
| 1290 | } |
| 1291 | |
| 1292 | template <class... _Args> |
| 1293 | constexpr void __construct_union(unexpect_t, _Args&&... __args) |
| 1294 | requires(__allow_reusing_expected_tail_padding) |
| 1295 | { |
| 1296 | std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...); |
| 1297 | __has_val_ = false; |
| 1298 | } |
| 1299 | |
| 1300 | private: |
| 1301 | template <class> |
| 1302 | friend class __expected_void_base; |
| 1303 | |
| 1304 | constexpr void __destroy_union_member() |
| 1305 | requires(!is_trivially_destructible_v<_Err>) |
| 1306 | { |
| 1307 | if (!__has_val_) |
| 1308 | std::destroy_at(std::addressof(__union_.__v.__unex_)); |
| 1309 | } |
| 1310 | |
| 1311 | template <class _OtherUnion> |
| 1312 | static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other) |
| 1313 | requires(__allow_reusing_expected_tail_padding) |
| 1314 | { |
| 1315 | if (__has_val) |
| 1316 | return __union_t(in_place); |
| 1317 | else |
| 1318 | return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_); |
| 1319 | } |
| 1320 | |
| 1321 | _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_; |
| 1322 | _LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_; |
| 1323 | }; |
| 1324 | |
| 1325 | template <class _OtherUnion> |
| 1326 | static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other) |
| 1327 | requires(__put_flag_in_tail) |
| 1328 | { |
| 1329 | if (__has_val) |
| 1330 | return __repr(in_place); |
| 1331 | else |
| 1332 | return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_); |
| 1333 | } |
| 1334 | |
| 1335 | protected: |
| 1336 | template <class... _Args> |
| 1337 | constexpr explicit __expected_void_base(_Args&&... __args) : __repr_(in_place, std::forward<_Args>(__args)...) {} |
| 1338 | |
| 1339 | template <class _OtherUnion> |
| 1340 | constexpr explicit __expected_void_base(bool __has_val, _OtherUnion&& __other) |
| 1341 | requires(__put_flag_in_tail) |
| 1342 | : __repr_(__conditional_no_unique_address_invoke_tag{}, |
| 1343 | [&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {} |
| 1344 | |
| 1345 | constexpr void __destroy() { |
| 1346 | if constexpr (__put_flag_in_tail) |
| 1347 | std::destroy_at(&__repr_.__v); |
| 1348 | else |
| 1349 | __repr_.__v.__destroy_union(); |
| 1350 | } |
| 1351 | |
| 1352 | template <class _Tag, class... _Args> |
| 1353 | constexpr void __construct(_Tag __tag, _Args&&... __args) { |
| 1354 | if constexpr (__put_flag_in_tail) |
| 1355 | std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...); |
| 1356 | else |
| 1357 | __repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...); |
| 1358 | } |
| 1359 | |
| 1360 | constexpr bool __has_val() const { return __repr_.__v.__has_val_; } |
| 1361 | constexpr __union_t& __union() { return __repr_.__v.__union_.__v; } |
| 1362 | constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; } |
| 1363 | constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; } |
| 1364 | constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; } |
| 1365 | |
| 1366 | private: |
| 1367 | _LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_; |
| 1368 | }; |
| 1369 | |
| 1370 | template <class _Tp, class _Err> |
| 1371 | requires is_void_v<_Tp> |
| 1372 | class expected<_Tp, _Err> : private __expected_void_base<_Err> { |
| 1373 | static_assert(__valid_std_unexpected<_Err>::value, |
| 1374 | "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a " |
| 1375 | "valid argument for unexpected<E> is ill-formed" ); |
| 1376 | |
| 1377 | template <class, class> |
| 1378 | friend class expected; |
| 1379 | |
| 1380 | template <class _Up, class _OtherErr, class _OtherErrQual> |
| 1381 | using __can_convert _LIBCPP_NODEBUG = |
| 1382 | _And< is_void<_Up>, |
| 1383 | is_constructible<_Err, _OtherErrQual>, |
| 1384 | _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>, |
| 1385 | _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>, |
| 1386 | _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>, |
| 1387 | _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>; |
| 1388 | |
| 1389 | using __base _LIBCPP_NODEBUG = __expected_void_base<_Err>; |
| 1390 | |
| 1391 | public: |
| 1392 | using value_type = _Tp; |
| 1393 | using error_type = _Err; |
| 1394 | using unexpected_type = unexpected<_Err>; |
| 1395 | |
| 1396 | template <class _Up> |
| 1397 | using rebind = expected<_Up, error_type>; |
| 1398 | |
| 1399 | // [expected.void.ctor], constructors |
| 1400 | constexpr expected() noexcept : __base(in_place) {} |
| 1401 | |
| 1402 | constexpr expected(const expected&) = delete; |
| 1403 | |
| 1404 | constexpr expected(const expected&) |
| 1405 | requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>) |
| 1406 | = default; |
| 1407 | |
| 1408 | constexpr expected(const expected& __rhs) noexcept(is_nothrow_copy_constructible_v<_Err>) // strengthened |
| 1409 | requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>) |
| 1410 | : __base(__rhs.__has_val(), __rhs.__union()) {} |
| 1411 | |
| 1412 | constexpr expected(expected&&) |
| 1413 | requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>) |
| 1414 | = default; |
| 1415 | |
| 1416 | constexpr expected(expected&& __rhs) noexcept(is_nothrow_move_constructible_v<_Err>) |
| 1417 | requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>) |
| 1418 | : __base(__rhs.__has_val(), std::move(__rhs.__union())) {} |
| 1419 | |
| 1420 | template <class _Up, class _OtherErr> |
| 1421 | requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value |
| 1422 | constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) |
| 1423 | expected(const expected<_Up, _OtherErr>& __rhs) noexcept( |
| 1424 | is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened |
| 1425 | : __base(__rhs.__has_val(), __rhs.__union()) {} |
| 1426 | |
| 1427 | template <class _Up, class _OtherErr> |
| 1428 | requires __can_convert<_Up, _OtherErr, _OtherErr>::value |
| 1429 | constexpr explicit(!is_convertible_v<_OtherErr, _Err>) |
| 1430 | expected(expected<_Up, _OtherErr>&& __rhs) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened |
| 1431 | : __base(__rhs.__has_val(), std::move(__rhs.__union())) {} |
| 1432 | |
| 1433 | template <class _OtherErr> |
| 1434 | requires is_constructible_v<_Err, const _OtherErr&> |
| 1435 | constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(const unexpected<_OtherErr>& __unex) noexcept( |
| 1436 | is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened |
| 1437 | : __base(unexpect, __unex.error()) {} |
| 1438 | |
| 1439 | template <class _OtherErr> |
| 1440 | requires is_constructible_v<_Err, _OtherErr> |
| 1441 | constexpr explicit(!is_convertible_v<_OtherErr, _Err>) |
| 1442 | expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened |
| 1443 | : __base(unexpect, std::move(__unex.error())) {} |
| 1444 | |
| 1445 | constexpr explicit expected(in_place_t) noexcept : __base(in_place) {} |
| 1446 | |
| 1447 | template <class... _Args> |
| 1448 | requires is_constructible_v<_Err, _Args...> |
| 1449 | constexpr explicit expected(unexpect_t, |
| 1450 | _Args&&... __args) noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened |
| 1451 | : __base(unexpect, std::forward<_Args>(__args)...) {} |
| 1452 | |
| 1453 | template <class _Up, class... _Args> |
| 1454 | requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... > |
| 1455 | constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept( |
| 1456 | is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened |
| 1457 | : __base(unexpect, __il, std::forward<_Args>(__args)...) {} |
| 1458 | |
| 1459 | private: |
| 1460 | template <class _Func, class... _Args> |
| 1461 | constexpr explicit expected(__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) |
| 1462 | : __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {} |
| 1463 | |
| 1464 | public: |
| 1465 | // [expected.void.dtor], destructor |
| 1466 | |
| 1467 | constexpr ~expected() = default; |
| 1468 | |
| 1469 | private: |
| 1470 | template <class... _Args> |
| 1471 | constexpr void __reinit_expected(unexpect_t, _Args&&... __args) { |
| 1472 | _LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set" ); |
| 1473 | |
| 1474 | this->__destroy(); |
| 1475 | auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); }); |
| 1476 | this->__construct(unexpect, std::forward<_Args>(__args)...); |
| 1477 | __trans.__complete(); |
| 1478 | } |
| 1479 | |
| 1480 | constexpr void __reinit_expected(in_place_t) { |
| 1481 | _LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset" ); |
| 1482 | |
| 1483 | this->__destroy(); |
| 1484 | this->__construct(in_place); |
| 1485 | } |
| 1486 | |
| 1487 | public: |
| 1488 | // [expected.void.assign], assignment |
| 1489 | constexpr expected& operator=(const expected&) = delete; |
| 1490 | |
| 1491 | constexpr expected& operator=(const expected& __rhs) noexcept( |
| 1492 | is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened |
| 1493 | requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>) |
| 1494 | { |
| 1495 | if (this->__has_val()) { |
| 1496 | if (!__rhs.__has_val()) { |
| 1497 | __reinit_expected(unexpect, __rhs.__unex()); |
| 1498 | } |
| 1499 | } else { |
| 1500 | if (__rhs.__has_val()) { |
| 1501 | __reinit_expected(in_place); |
| 1502 | } else { |
| 1503 | this->__unex() = __rhs.__unex(); |
| 1504 | } |
| 1505 | } |
| 1506 | return *this; |
| 1507 | } |
| 1508 | |
| 1509 | constexpr expected& |
| 1510 | operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>) |
| 1511 | requires(is_move_assignable_v<_Err> && is_move_constructible_v<_Err>) |
| 1512 | { |
| 1513 | if (this->__has_val()) { |
| 1514 | if (!__rhs.__has_val()) { |
| 1515 | __reinit_expected(unexpect, std::move(__rhs.__unex())); |
| 1516 | } |
| 1517 | } else { |
| 1518 | if (__rhs.__has_val()) { |
| 1519 | __reinit_expected(in_place); |
| 1520 | } else { |
| 1521 | this->__unex() = std::move(__rhs.__unex()); |
| 1522 | } |
| 1523 | } |
| 1524 | return *this; |
| 1525 | } |
| 1526 | |
| 1527 | template <class _OtherErr> |
| 1528 | requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>) |
| 1529 | constexpr expected& operator=(const unexpected<_OtherErr>& __un) { |
| 1530 | if (this->__has_val()) { |
| 1531 | __reinit_expected(unexpect, __un.error()); |
| 1532 | } else { |
| 1533 | this->__unex() = __un.error(); |
| 1534 | } |
| 1535 | return *this; |
| 1536 | } |
| 1537 | |
| 1538 | template <class _OtherErr> |
| 1539 | requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>) |
| 1540 | constexpr expected& operator=(unexpected<_OtherErr>&& __un) { |
| 1541 | if (this->__has_val()) { |
| 1542 | __reinit_expected(unexpect, std::move(__un.error())); |
| 1543 | } else { |
| 1544 | this->__unex() = std::move(__un.error()); |
| 1545 | } |
| 1546 | return *this; |
| 1547 | } |
| 1548 | |
| 1549 | constexpr void emplace() noexcept { |
| 1550 | if (!this->__has_val()) { |
| 1551 | __reinit_expected(in_place); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | // [expected.void.swap], swap |
| 1556 | constexpr void swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>) |
| 1557 | requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>) |
| 1558 | { |
| 1559 | auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) { |
| 1560 | // May throw, but will re-engage `__with_val` in that case. |
| 1561 | __with_val.__reinit_expected(unexpect, std::move(__with_err.__unex())); |
| 1562 | // Will not throw. |
| 1563 | __with_err.__reinit_expected(in_place); |
| 1564 | }; |
| 1565 | |
| 1566 | if (this->__has_val()) { |
| 1567 | if (!__rhs.__has_val()) { |
| 1568 | __swap_val_unex_impl(*this, __rhs); |
| 1569 | } |
| 1570 | } else { |
| 1571 | if (__rhs.__has_val()) { |
| 1572 | __swap_val_unex_impl(__rhs, *this); |
| 1573 | } else { |
| 1574 | using std::swap; |
| 1575 | swap(this->__unex(), __rhs.__unex()); |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(rhs&: __y))) |
| 1581 | requires requires { __x.swap(rhs&: __y); } |
| 1582 | { |
| 1583 | __x.swap(rhs&: __y); |
| 1584 | } |
| 1585 | |
| 1586 | // [expected.void.obs], observers |
| 1587 | constexpr explicit operator bool() const noexcept { return this->__has_val(); } |
| 1588 | |
| 1589 | [[nodiscard]] constexpr bool has_value() const noexcept { return this->__has_val(); } |
| 1590 | |
| 1591 | # if _LIBCPP_STD_VER >= 29 |
| 1592 | [[nodiscard]] constexpr bool has_error() const noexcept { return !this->has_value(); } |
| 1593 | # endif |
| 1594 | |
| 1595 | constexpr void operator*() const noexcept { |
| 1596 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 1597 | this->__has_val(), "expected::operator* requires the expected to contain a value" ); |
| 1598 | } |
| 1599 | |
| 1600 | constexpr void value() const& { |
| 1601 | static_assert(is_copy_constructible_v<_Err>); |
| 1602 | if (!this->__has_val()) { |
| 1603 | std::__throw_bad_expected_access<_Err>(this->__unex()); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | constexpr void value() && { |
| 1608 | static_assert(is_copy_constructible_v<_Err> && is_move_constructible_v<_Err>); |
| 1609 | if (!this->__has_val()) { |
| 1610 | std::__throw_bad_expected_access<_Err>(std::move(this->__unex())); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | [[nodiscard]] constexpr const _Err& error() const& noexcept { |
| 1615 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 1616 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 1617 | return this->__unex(); |
| 1618 | } |
| 1619 | |
| 1620 | [[nodiscard]] constexpr _Err& error() & noexcept { |
| 1621 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 1622 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 1623 | return this->__unex(); |
| 1624 | } |
| 1625 | |
| 1626 | [[nodiscard]] constexpr const _Err&& error() const&& noexcept { |
| 1627 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 1628 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 1629 | return std::move(this->__unex()); |
| 1630 | } |
| 1631 | |
| 1632 | [[nodiscard]] constexpr _Err&& error() && noexcept { |
| 1633 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 1634 | !this->__has_val(), "expected::error requires the expected to contain an error" ); |
| 1635 | return std::move(this->__unex()); |
| 1636 | } |
| 1637 | |
| 1638 | template <class _Up = _Err> |
| 1639 | [[nodiscard]] constexpr _Err error_or(_Up&& __error) const& { |
| 1640 | static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible" ); |
| 1641 | static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type" ); |
| 1642 | if (has_value()) { |
| 1643 | return std::forward<_Up>(__error); |
| 1644 | } |
| 1645 | return error(); |
| 1646 | } |
| 1647 | |
| 1648 | template <class _Up = _Err> |
| 1649 | [[nodiscard]] constexpr _Err error_or(_Up&& __error) && { |
| 1650 | static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible" ); |
| 1651 | static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type" ); |
| 1652 | if (has_value()) { |
| 1653 | return std::forward<_Up>(__error); |
| 1654 | } |
| 1655 | return std::move(error()); |
| 1656 | } |
| 1657 | |
| 1658 | // [expected.void.monadic], monadic |
| 1659 | template <class _Func> |
| 1660 | requires is_constructible_v<_Err, _Err&> |
| 1661 | [[nodiscard]] constexpr auto and_then(_Func&& __f) & { |
| 1662 | using _Up = remove_cvref_t<invoke_result_t<_Func>>; |
| 1663 | static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected" ); |
| 1664 | static_assert( |
| 1665 | is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected" ); |
| 1666 | if (has_value()) { |
| 1667 | return std::invoke(std::forward<_Func>(__f)); |
| 1668 | } |
| 1669 | return _Up(unexpect, error()); |
| 1670 | } |
| 1671 | |
| 1672 | template <class _Func> |
| 1673 | requires is_constructible_v<_Err, const _Err&> |
| 1674 | [[nodiscard]] constexpr auto and_then(_Func&& __f) const& { |
| 1675 | using _Up = remove_cvref_t<invoke_result_t<_Func>>; |
| 1676 | static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected" ); |
| 1677 | static_assert( |
| 1678 | is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected" ); |
| 1679 | if (has_value()) { |
| 1680 | return std::invoke(std::forward<_Func>(__f)); |
| 1681 | } |
| 1682 | return _Up(unexpect, error()); |
| 1683 | } |
| 1684 | |
| 1685 | template <class _Func> |
| 1686 | requires is_constructible_v<_Err, _Err&&> |
| 1687 | [[nodiscard]] constexpr auto and_then(_Func&& __f) && { |
| 1688 | using _Up = remove_cvref_t<invoke_result_t<_Func>>; |
| 1689 | static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected" ); |
| 1690 | static_assert( |
| 1691 | is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected" ); |
| 1692 | if (has_value()) { |
| 1693 | return std::invoke(std::forward<_Func>(__f)); |
| 1694 | } |
| 1695 | return _Up(unexpect, std::move(error())); |
| 1696 | } |
| 1697 | |
| 1698 | template <class _Func> |
| 1699 | requires is_constructible_v<_Err, const _Err&&> |
| 1700 | [[nodiscard]] constexpr auto and_then(_Func&& __f) const&& { |
| 1701 | using _Up = remove_cvref_t<invoke_result_t<_Func>>; |
| 1702 | static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected" ); |
| 1703 | static_assert( |
| 1704 | is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected" ); |
| 1705 | if (has_value()) { |
| 1706 | return std::invoke(std::forward<_Func>(__f)); |
| 1707 | } |
| 1708 | return _Up(unexpect, std::move(error())); |
| 1709 | } |
| 1710 | |
| 1711 | template <class _Func> |
| 1712 | [[nodiscard]] constexpr auto or_else(_Func&& __f) & { |
| 1713 | using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>; |
| 1714 | static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected" ); |
| 1715 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1716 | "The result of f(error()) must have the same value_type as this expected" ); |
| 1717 | if (has_value()) { |
| 1718 | return _Gp(); |
| 1719 | } |
| 1720 | return std::invoke(std::forward<_Func>(__f), error()); |
| 1721 | } |
| 1722 | |
| 1723 | template <class _Func> |
| 1724 | [[nodiscard]] constexpr auto or_else(_Func&& __f) const& { |
| 1725 | using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>; |
| 1726 | static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected" ); |
| 1727 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1728 | "The result of f(error()) must have the same value_type as this expected" ); |
| 1729 | if (has_value()) { |
| 1730 | return _Gp(); |
| 1731 | } |
| 1732 | return std::invoke(std::forward<_Func>(__f), error()); |
| 1733 | } |
| 1734 | |
| 1735 | template <class _Func> |
| 1736 | [[nodiscard]] constexpr auto or_else(_Func&& __f) && { |
| 1737 | using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>; |
| 1738 | static_assert( |
| 1739 | __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected" ); |
| 1740 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1741 | "The result of f(std::move(error())) must have the same value_type as this expected" ); |
| 1742 | if (has_value()) { |
| 1743 | return _Gp(); |
| 1744 | } |
| 1745 | return std::invoke(std::forward<_Func>(__f), std::move(error())); |
| 1746 | } |
| 1747 | |
| 1748 | template <class _Func> |
| 1749 | [[nodiscard]] constexpr auto or_else(_Func&& __f) const&& { |
| 1750 | using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>; |
| 1751 | static_assert( |
| 1752 | __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected" ); |
| 1753 | static_assert(is_same_v<typename _Gp::value_type, _Tp>, |
| 1754 | "The result of f(std::move(error())) must have the same value_type as this expected" ); |
| 1755 | if (has_value()) { |
| 1756 | return _Gp(); |
| 1757 | } |
| 1758 | return std::invoke(std::forward<_Func>(__f), std::move(error())); |
| 1759 | } |
| 1760 | |
| 1761 | template <class _Func> |
| 1762 | requires is_constructible_v<_Err, _Err&> |
| 1763 | [[nodiscard]] constexpr auto transform(_Func&& __f) & { |
| 1764 | using _Up = remove_cv_t<invoke_result_t<_Func>>; |
| 1765 | if (!has_value()) { |
| 1766 | return expected<_Up, _Err>(unexpect, error()); |
| 1767 | } |
| 1768 | if constexpr (!is_void_v<_Up>) { |
| 1769 | return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); |
| 1770 | } else { |
| 1771 | std::invoke(std::forward<_Func>(__f)); |
| 1772 | return expected<_Up, _Err>(); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | template <class _Func> |
| 1777 | requires is_constructible_v<_Err, const _Err&> |
| 1778 | [[nodiscard]] constexpr auto transform(_Func&& __f) const& { |
| 1779 | using _Up = remove_cv_t<invoke_result_t<_Func>>; |
| 1780 | if (!has_value()) { |
| 1781 | return expected<_Up, _Err>(unexpect, error()); |
| 1782 | } |
| 1783 | if constexpr (!is_void_v<_Up>) { |
| 1784 | return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); |
| 1785 | } else { |
| 1786 | std::invoke(std::forward<_Func>(__f)); |
| 1787 | return expected<_Up, _Err>(); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | template <class _Func> |
| 1792 | requires is_constructible_v<_Err, _Err&&> |
| 1793 | [[nodiscard]] constexpr auto transform(_Func&& __f) && { |
| 1794 | using _Up = remove_cv_t<invoke_result_t<_Func>>; |
| 1795 | if (!has_value()) { |
| 1796 | return expected<_Up, _Err>(unexpect, std::move(error())); |
| 1797 | } |
| 1798 | if constexpr (!is_void_v<_Up>) { |
| 1799 | return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); |
| 1800 | } else { |
| 1801 | std::invoke(std::forward<_Func>(__f)); |
| 1802 | return expected<_Up, _Err>(); |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | template <class _Func> |
| 1807 | requires is_constructible_v<_Err, const _Err&&> |
| 1808 | [[nodiscard]] constexpr auto transform(_Func&& __f) const&& { |
| 1809 | using _Up = remove_cv_t<invoke_result_t<_Func>>; |
| 1810 | if (!has_value()) { |
| 1811 | return expected<_Up, _Err>(unexpect, std::move(error())); |
| 1812 | } |
| 1813 | if constexpr (!is_void_v<_Up>) { |
| 1814 | return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); |
| 1815 | } else { |
| 1816 | std::invoke(std::forward<_Func>(__f)); |
| 1817 | return expected<_Up, _Err>(); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | template <class _Func> |
| 1822 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) & { |
| 1823 | using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>; |
| 1824 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1825 | "The result of f(error()) must be a valid template argument for unexpected" ); |
| 1826 | if (has_value()) { |
| 1827 | return expected<_Tp, _Gp>(); |
| 1828 | } |
| 1829 | return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); |
| 1830 | } |
| 1831 | |
| 1832 | template <class _Func> |
| 1833 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) const& { |
| 1834 | using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>; |
| 1835 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1836 | "The result of f(error()) must be a valid template argument for unexpected" ); |
| 1837 | if (has_value()) { |
| 1838 | return expected<_Tp, _Gp>(); |
| 1839 | } |
| 1840 | return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); |
| 1841 | } |
| 1842 | |
| 1843 | template <class _Func> |
| 1844 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) && { |
| 1845 | using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>; |
| 1846 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1847 | "The result of f(std::move(error())) must be a valid template argument for unexpected" ); |
| 1848 | if (has_value()) { |
| 1849 | return expected<_Tp, _Gp>(); |
| 1850 | } |
| 1851 | return expected<_Tp, _Gp>( |
| 1852 | __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); |
| 1853 | } |
| 1854 | |
| 1855 | template <class _Func> |
| 1856 | [[nodiscard]] constexpr auto transform_error(_Func&& __f) const&& { |
| 1857 | using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>; |
| 1858 | static_assert(__valid_std_unexpected<_Gp>::value, |
| 1859 | "The result of f(std::move(error())) must be a valid template argument for unexpected" ); |
| 1860 | if (has_value()) { |
| 1861 | return expected<_Tp, _Gp>(); |
| 1862 | } |
| 1863 | return expected<_Tp, _Gp>( |
| 1864 | __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); |
| 1865 | } |
| 1866 | |
| 1867 | // [expected.void.eq], equality operators |
| 1868 | template <class _T2, class _E2> |
| 1869 | requires is_void_v<_T2> |
| 1870 | friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) |
| 1871 | # if _LIBCPP_STD_VER >= 26 |
| 1872 | requires requires { |
| 1873 | { __x.error() == __y.error() }->__core_convertible_to<bool>; |
| 1874 | } |
| 1875 | # endif |
| 1876 | { |
| 1877 | if (__x.__has_val() != __y.__has_val()) { |
| 1878 | return false; |
| 1879 | } else { |
| 1880 | return __x.__has_val() || std::__into_bool(b: __x.__unex() == __y.__unex()); |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | template <class _E2> |
| 1885 | friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) |
| 1886 | # if _LIBCPP_STD_VER >= 26 |
| 1887 | requires requires { |
| 1888 | { __x.error() == __y.error() }->__core_convertible_to<bool>; |
| 1889 | } |
| 1890 | # endif |
| 1891 | { |
| 1892 | return !__x.__has_val() && std::__into_bool(b: __x.__unex() == __y.error()); |
| 1893 | } |
| 1894 | }; |
| 1895 | |
| 1896 | _LIBCPP_END_NAMESPACE_STD |
| 1897 | |
| 1898 | #endif // _LIBCPP_STD_VER >= 23 |
| 1899 | |
| 1900 | _LIBCPP_POP_MACROS |
| 1901 | |
| 1902 | #endif // _LIBCPP___EXPECTED_EXPECTED_H |
| 1903 | |