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