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