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/conjunction.h> 21 #include <__type_traits/disjunction.h> 22 #include <__type_traits/integral_constant.h> 23 #include <__type_traits/is_assignable.h> 24 #include <__type_traits/is_constructible.h> 25 #include <__type_traits/is_convertible.h> 26 #include <__type_traits/is_copy_assignable.h> 27 #include <__type_traits/is_copy_constructible.h> 28 #include <__type_traits/is_default_constructible.h> 29 #include <__type_traits/is_function.h> 30 #include <__type_traits/is_move_assignable.h> 31 #include <__type_traits/is_move_constructible.h> 32 #include <__type_traits/is_nothrow_constructible.h> 33 #include <__type_traits/is_nothrow_copy_assignable.h> 34 #include <__type_traits/is_nothrow_copy_constructible.h> 35 #include <__type_traits/is_nothrow_default_constructible.h> 36 #include <__type_traits/is_nothrow_move_assignable.h> 37 #include <__type_traits/is_nothrow_move_constructible.h> 38 #include <__type_traits/is_reference.h> 39 #include <__type_traits/is_same.h> 40 #include <__type_traits/is_swappable.h> 41 #include <__type_traits/is_trivially_copy_constructible.h> 42 #include <__type_traits/is_trivially_destructible.h> 43 #include <__type_traits/is_trivially_move_constructible.h> 44 #include <__type_traits/is_void.h> 45 #include <__type_traits/lazy.h> 46 #include <__type_traits/negation.h> 47 #include <__type_traits/remove_cv.h> 48 #include <__type_traits/remove_cvref.h> 49 #include <__utility/as_const.h> 50 #include <__utility/exception_guard.h> 51 #include <__utility/forward.h> 52 #include <__utility/in_place.h> 53 #include <__utility/move.h> 54 #include <__utility/swap.h> 55 #include <__verbose_abort> 56 #include <initializer_list> 57 58 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 59 # pragma GCC system_header 60 #endif 61 62 _LIBCPP_PUSH_MACROS 63 #include <__undef_macros> 64 65 #if _LIBCPP_STD_VER >= 23 66 67 _LIBCPP_BEGIN_NAMESPACE_STD 68 69 template <class _Tp, class _Err> 70 class expected; 71 72 template <class _Tp> 73 struct __is_std_expected : false_type {}; 74 75 template <class _Tp, class _Err> 76 struct __is_std_expected<expected<_Tp, _Err>> : true_type {}; 77 78 struct __expected_construct_in_place_from_invoke_tag {}; 79 struct __expected_construct_unexpected_from_invoke_tag {}; 80 81 template <class _Err, class _Arg> 82 _LIBCPP_HIDE_FROM_ABI void __throw_bad_expected_access(_Arg&& __arg) { 83 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS 84 throw bad_expected_access<_Err>(std::forward<_Arg>(__arg)); 85 # else 86 (void)__arg; 87 _LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode"); 88 # endif 89 } 90 91 template <class _Tp, class _Err> 92 class expected { 93 static_assert( 94 !is_reference_v<_Tp> && 95 !is_function_v<_Tp> && 96 !is_same_v<remove_cv_t<_Tp>, in_place_t> && 97 !is_same_v<remove_cv_t<_Tp>, unexpect_t> && 98 !__is_std_unexpected<remove_cv_t<_Tp>>::value && 99 __valid_std_unexpected<_Err>::value 100 , 101 "[expected.object.general] A program that instantiates the definition of template expected<T, E> for a " 102 "reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a " 103 "specialization of unexpected for the T parameter is ill-formed. A program that instantiates the " 104 "definition of the template expected<T, E> with a type for the E parameter that is not a valid " 105 "template argument for unexpected is ill-formed."); 106 107 template <class _Up, class _OtherErr> 108 friend class expected; 109 110 public: 111 using value_type = _Tp; 112 using error_type = _Err; 113 using unexpected_type = unexpected<_Err>; 114 115 template <class _Up> 116 using rebind = expected<_Up, error_type>; 117 118 // [expected.object.ctor], constructors 119 _LIBCPP_HIDE_FROM_ABI constexpr expected() 120 noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened 121 requires is_default_constructible_v<_Tp> 122 : __has_val_(true) { 123 std::construct_at(std::addressof(__union_.__val_)); 124 } 125 126 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete; 127 128 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) 129 requires(is_copy_constructible_v<_Tp> && 130 is_copy_constructible_v<_Err> && 131 is_trivially_copy_constructible_v<_Tp> && 132 is_trivially_copy_constructible_v<_Err>) 133 = default; 134 135 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) 136 noexcept(is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened 137 requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && 138 !(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)) 139 : __has_val_(__other.__has_val_) { 140 if (__has_val_) { 141 std::construct_at(std::addressof(__union_.__val_), __other.__union_.__val_); 142 } else { 143 std::construct_at(std::addressof(__union_.__unex_), __other.__union_.__unex_); 144 } 145 } 146 147 148 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&) 149 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> 150 && is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>) 151 = default; 152 153 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) 154 noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>) 155 requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && 156 !(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)) 157 : __has_val_(__other.__has_val_) { 158 if (__has_val_) { 159 std::construct_at(std::addressof(__union_.__val_), std::move(__other.__union_.__val_)); 160 } else { 161 std::construct_at(std::addressof(__union_.__unex_), std::move(__other.__union_.__unex_)); 162 } 163 } 164 165 private: 166 template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual> 167 using __can_convert = 168 _And< is_constructible<_Tp, _UfQual>, 169 is_constructible<_Err, _OtherErrQual>, 170 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>, 171 _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>, 172 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>, 173 _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>, 174 _Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>, 175 _Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>, 176 _Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>, 177 _Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>, 178 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>, 179 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>, 180 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>, 181 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >; 182 183 template <class _Func, class... _Args> 184 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected( 185 std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) 186 : __union_(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...), __has_val_(true) {} 187 188 template <class _Func, class... _Args> 189 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected( 190 std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) 191 : __union_(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...), __has_val_(false) {} 192 193 public: 194 template <class _Up, class _OtherErr> 195 requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value 196 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> || 197 !is_convertible_v<const _OtherErr&, _Err>) 198 expected(const expected<_Up, _OtherErr>& __other) 199 noexcept(is_nothrow_constructible_v<_Tp, const _Up&> && 200 is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened 201 : __has_val_(__other.__has_val_) { 202 if (__has_val_) { 203 std::construct_at(std::addressof(__union_.__val_), __other.__union_.__val_); 204 } else { 205 std::construct_at(std::addressof(__union_.__unex_), __other.__union_.__unex_); 206 } 207 } 208 209 template <class _Up, class _OtherErr> 210 requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value 211 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>) 212 expected(expected<_Up, _OtherErr>&& __other) 213 noexcept(is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened 214 : __has_val_(__other.__has_val_) { 215 if (__has_val_) { 216 std::construct_at(std::addressof(__union_.__val_), std::move(__other.__union_.__val_)); 217 } else { 218 std::construct_at(std::addressof(__union_.__unex_), std::move(__other.__union_.__unex_)); 219 } 220 } 221 222 template <class _Up = _Tp> 223 requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> && 224 !__is_std_unexpected<remove_cvref_t<_Up>>::value && is_constructible_v<_Tp, _Up>) 225 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>) 226 expected(_Up&& __u) 227 noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened 228 : __has_val_(true) { 229 std::construct_at(std::addressof(__union_.__val_), std::forward<_Up>(__u)); 230 } 231 232 233 template <class _OtherErr> 234 requires is_constructible_v<_Err, const _OtherErr&> 235 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) 236 expected(const unexpected<_OtherErr>& __unex) 237 noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened 238 : __has_val_(false) { 239 std::construct_at(std::addressof(__union_.__unex_), __unex.error()); 240 } 241 242 template <class _OtherErr> 243 requires is_constructible_v<_Err, _OtherErr> 244 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>) 245 expected(unexpected<_OtherErr>&& __unex) 246 noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened 247 : __has_val_(false) { 248 std::construct_at(std::addressof(__union_.__unex_), std::move(__unex.error())); 249 } 250 251 template <class... _Args> 252 requires is_constructible_v<_Tp, _Args...> 253 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) 254 noexcept(is_nothrow_constructible_v<_Tp, _Args...>) // strengthened 255 : __has_val_(true) { 256 std::construct_at(std::addressof(__union_.__val_), std::forward<_Args>(__args)...); 257 } 258 259 template <class _Up, class... _Args> 260 requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... > 261 _LIBCPP_HIDE_FROM_ABI constexpr explicit 262 expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) 263 noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened 264 : __has_val_(true) { 265 std::construct_at(std::addressof(__union_.__val_), __il, std::forward<_Args>(__args)...); 266 } 267 268 template <class... _Args> 269 requires is_constructible_v<_Err, _Args...> 270 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) 271 noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened 272 : __has_val_(false) { 273 std::construct_at(std::addressof(__union_.__unex_), std::forward<_Args>(__args)...); 274 } 275 276 template <class _Up, class... _Args> 277 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... > 278 _LIBCPP_HIDE_FROM_ABI constexpr explicit 279 expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) 280 noexcept(is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened 281 : __has_val_(false) { 282 std::construct_at(std::addressof(__union_.__unex_), __il, std::forward<_Args>(__args)...); 283 } 284 285 // [expected.object.dtor], destructor 286 287 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() 288 requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>) 289 = default; 290 291 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() 292 requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>) 293 { 294 if (__has_val_) { 295 std::destroy_at(std::addressof(__union_.__val_)); 296 } else { 297 std::destroy_at(std::addressof(__union_.__unex_)); 298 } 299 } 300 301 private: 302 template <class _T1, class _T2, class... _Args> 303 _LIBCPP_HIDE_FROM_ABI static constexpr void __reinit_expected(_T1& __newval, _T2& __oldval, _Args&&... __args) { 304 if constexpr (is_nothrow_constructible_v<_T1, _Args...>) { 305 std::destroy_at(std::addressof(__oldval)); 306 std::construct_at(std::addressof(__newval), std::forward<_Args>(__args)...); 307 } else if constexpr (is_nothrow_move_constructible_v<_T1>) { 308 _T1 __tmp(std::forward<_Args>(__args)...); 309 std::destroy_at(std::addressof(__oldval)); 310 std::construct_at(std::addressof(__newval), std::move(__tmp)); 311 } else { 312 static_assert( 313 is_nothrow_move_constructible_v<_T2>, 314 "To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can " 315 "be reverted to the previous state in case an exception is thrown during the assignment."); 316 _T2 __tmp(std::move(__oldval)); 317 std::destroy_at(std::addressof(__oldval)); 318 auto __trans = 319 std::__make_exception_guard([&] { std::construct_at(std::addressof(__oldval), std::move(__tmp)); }); 320 std::construct_at(std::addressof(__newval), std::forward<_Args>(__args)...); 321 __trans.__complete(); 322 } 323 } 324 325 public: 326 // [expected.object.assign], assignment 327 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete; 328 329 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) 330 noexcept(is_nothrow_copy_assignable_v<_Tp> && 331 is_nothrow_copy_constructible_v<_Tp> && 332 is_nothrow_copy_assignable_v<_Err> && 333 is_nothrow_copy_constructible_v<_Err>) // strengthened 334 requires(is_copy_assignable_v<_Tp> && 335 is_copy_constructible_v<_Tp> && 336 is_copy_assignable_v<_Err> && 337 is_copy_constructible_v<_Err> && 338 (is_nothrow_move_constructible_v<_Tp> || 339 is_nothrow_move_constructible_v<_Err>)) 340 { 341 if (__has_val_ && __rhs.__has_val_) { 342 __union_.__val_ = __rhs.__union_.__val_; 343 } else if (__has_val_) { 344 __reinit_expected(__union_.__unex_, __union_.__val_, __rhs.__union_.__unex_); 345 } else if (__rhs.__has_val_) { 346 __reinit_expected(__union_.__val_, __union_.__unex_, __rhs.__union_.__val_); 347 } else { 348 __union_.__unex_ = __rhs.__union_.__unex_; 349 } 350 // note: only reached if no exception+rollback was done inside __reinit_expected 351 __has_val_ = __rhs.__has_val_; 352 return *this; 353 } 354 355 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&& __rhs) 356 noexcept(is_nothrow_move_assignable_v<_Tp> && 357 is_nothrow_move_constructible_v<_Tp> && 358 is_nothrow_move_assignable_v<_Err> && 359 is_nothrow_move_constructible_v<_Err>) 360 requires(is_move_constructible_v<_Tp> && 361 is_move_assignable_v<_Tp> && 362 is_move_constructible_v<_Err> && 363 is_move_assignable_v<_Err> && 364 (is_nothrow_move_constructible_v<_Tp> || 365 is_nothrow_move_constructible_v<_Err>)) 366 { 367 if (__has_val_ && __rhs.__has_val_) { 368 __union_.__val_ = std::move(__rhs.__union_.__val_); 369 } else if (__has_val_) { 370 __reinit_expected(__union_.__unex_, __union_.__val_, std::move(__rhs.__union_.__unex_)); 371 } else if (__rhs.__has_val_) { 372 __reinit_expected(__union_.__val_, __union_.__unex_, std::move(__rhs.__union_.__val_)); 373 } else { 374 __union_.__unex_ = std::move(__rhs.__union_.__unex_); 375 } 376 // note: only reached if no exception+rollback was done inside __reinit_expected 377 __has_val_ = __rhs.__has_val_; 378 return *this; 379 } 380 381 template <class _Up = _Tp> 382 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v) 383 requires(!is_same_v<expected, remove_cvref_t<_Up>> && 384 !__is_std_unexpected<remove_cvref_t<_Up>>::value && 385 is_constructible_v<_Tp, _Up> && 386 is_assignable_v<_Tp&, _Up> && 387 (is_nothrow_constructible_v<_Tp, _Up> || 388 is_nothrow_move_constructible_v<_Tp> || 389 is_nothrow_move_constructible_v<_Err>)) 390 { 391 if (__has_val_) { 392 __union_.__val_ = std::forward<_Up>(__v); 393 } else { 394 __reinit_expected(__union_.__val_, __union_.__unex_, std::forward<_Up>(__v)); 395 __has_val_ = true; 396 } 397 return *this; 398 } 399 400 private: 401 template <class _OtherErrQual> 402 static constexpr bool __can_assign_from_unexpected = 403 _And< is_constructible<_Err, _OtherErrQual>, 404 is_assignable<_Err&, _OtherErrQual>, 405 _Lazy<_Or, 406 is_nothrow_constructible<_Err, _OtherErrQual>, 407 is_nothrow_move_constructible<_Tp>, 408 is_nothrow_move_constructible<_Err>> >::value; 409 410 public: 411 template <class _OtherErr> 412 requires(__can_assign_from_unexpected<const _OtherErr&>) 413 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) { 414 if (__has_val_) { 415 __reinit_expected(__union_.__unex_, __union_.__val_, __un.error()); 416 __has_val_ = false; 417 } else { 418 __union_.__unex_ = __un.error(); 419 } 420 return *this; 421 } 422 423 template <class _OtherErr> 424 requires(__can_assign_from_unexpected<_OtherErr>) 425 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) { 426 if (__has_val_) { 427 __reinit_expected(__union_.__unex_, __union_.__val_, std::move(__un.error())); 428 __has_val_ = false; 429 } else { 430 __union_.__unex_ = std::move(__un.error()); 431 } 432 return *this; 433 } 434 435 template <class... _Args> 436 requires is_nothrow_constructible_v<_Tp, _Args...> 437 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept { 438 if (__has_val_) { 439 std::destroy_at(std::addressof(__union_.__val_)); 440 } else { 441 std::destroy_at(std::addressof(__union_.__unex_)); 442 __has_val_ = true; 443 } 444 return *std::construct_at(std::addressof(__union_.__val_), std::forward<_Args>(__args)...); 445 } 446 447 template <class _Up, class... _Args> 448 requires is_nothrow_constructible_v< _Tp, initializer_list<_Up>&, _Args... > 449 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept { 450 if (__has_val_) { 451 std::destroy_at(std::addressof(__union_.__val_)); 452 } else { 453 std::destroy_at(std::addressof(__union_.__unex_)); 454 __has_val_ = true; 455 } 456 return *std::construct_at(std::addressof(__union_.__val_), __il, std::forward<_Args>(__args)...); 457 } 458 459 460 public: 461 // [expected.object.swap], swap 462 _LIBCPP_HIDE_FROM_ABI constexpr void swap(expected& __rhs) 463 noexcept(is_nothrow_move_constructible_v<_Tp> && 464 is_nothrow_swappable_v<_Tp> && 465 is_nothrow_move_constructible_v<_Err> && 466 is_nothrow_swappable_v<_Err>) 467 requires(is_swappable_v<_Tp> && 468 is_swappable_v<_Err> && 469 is_move_constructible_v<_Tp> && 470 is_move_constructible_v<_Err> && 471 (is_nothrow_move_constructible_v<_Tp> || 472 is_nothrow_move_constructible_v<_Err>)) 473 { 474 auto __swap_val_unex_impl = [&](expected& __with_val, expected& __with_err) { 475 if constexpr (is_nothrow_move_constructible_v<_Err>) { 476 _Err __tmp(std::move(__with_err.__union_.__unex_)); 477 std::destroy_at(std::addressof(__with_err.__union_.__unex_)); 478 auto __trans = std::__make_exception_guard([&] { 479 std::construct_at(std::addressof(__with_err.__union_.__unex_), std::move(__tmp)); 480 }); 481 std::construct_at(std::addressof(__with_err.__union_.__val_), std::move(__with_val.__union_.__val_)); 482 __trans.__complete(); 483 std::destroy_at(std::addressof(__with_val.__union_.__val_)); 484 std::construct_at(std::addressof(__with_val.__union_.__unex_), std::move(__tmp)); 485 } else { 486 static_assert(is_nothrow_move_constructible_v<_Tp>, 487 "To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so " 488 "that it can be reverted to the previous state in case an exception is thrown during swap."); 489 _Tp __tmp(std::move(__with_val.__union_.__val_)); 490 std::destroy_at(std::addressof(__with_val.__union_.__val_)); 491 auto __trans = std::__make_exception_guard([&] { 492 std::construct_at(std::addressof(__with_val.__union_.__val_), std::move(__tmp)); 493 }); 494 std::construct_at(std::addressof(__with_val.__union_.__unex_), std::move(__with_err.__union_.__unex_)); 495 __trans.__complete(); 496 std::destroy_at(std::addressof(__with_err.__union_.__unex_)); 497 std::construct_at(std::addressof(__with_err.__union_.__val_), std::move(__tmp)); 498 } 499 __with_val.__has_val_ = false; 500 __with_err.__has_val_ = true; 501 }; 502 503 if (__has_val_) { 504 if (__rhs.__has_val_) { 505 using std::swap; 506 swap(__union_.__val_, __rhs.__union_.__val_); 507 } else { 508 __swap_val_unex_impl(*this, __rhs); 509 } 510 } else { 511 if (__rhs.__has_val_) { 512 __swap_val_unex_impl(__rhs, *this); 513 } else { 514 using std::swap; 515 swap(__union_.__unex_, __rhs.__union_.__unex_); 516 } 517 } 518 } 519 520 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) 521 noexcept(noexcept(__x.swap(__y))) 522 requires requires { __x.swap(__y); } 523 { 524 __x.swap(__y); 525 } 526 527 // [expected.object.obs], observers 528 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { 529 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator-> requires the expected to contain a value"); 530 return std::addressof(__union_.__val_); 531 } 532 533 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { 534 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator-> requires the expected to contain a value"); 535 return std::addressof(__union_.__val_); 536 } 537 538 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept { 539 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator* requires the expected to contain a value"); 540 return __union_.__val_; 541 } 542 543 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept { 544 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator* requires the expected to contain a value"); 545 return __union_.__val_; 546 } 547 548 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept { 549 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator* requires the expected to contain a value"); 550 return std::move(__union_.__val_); 551 } 552 553 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept { 554 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator* requires the expected to contain a value"); 555 return std::move(__union_.__val_); 556 } 557 558 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return __has_val_; } 559 560 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __has_val_; } 561 562 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& { 563 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible"); 564 if (!__has_val_) { 565 std::__throw_bad_expected_access<_Err>(std::as_const(error())); 566 } 567 return __union_.__val_; 568 } 569 570 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & { 571 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible"); 572 if (!__has_val_) { 573 std::__throw_bad_expected_access<_Err>(std::as_const(error())); 574 } 575 return __union_.__val_; 576 } 577 578 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& { 579 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>, 580 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))"); 581 if (!__has_val_) { 582 std::__throw_bad_expected_access<_Err>(std::move(error())); 583 } 584 return std::move(__union_.__val_); 585 } 586 587 _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && { 588 static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>, 589 "error_type has to be both copy constructible and constructible from decltype(std::move(error()))"); 590 if (!__has_val_) { 591 std::__throw_bad_expected_access<_Err>(std::move(error())); 592 } 593 return std::move(__union_.__val_); 594 } 595 596 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept { 597 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 598 return __union_.__unex_; 599 } 600 601 _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept { 602 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 603 return __union_.__unex_; 604 } 605 606 _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept { 607 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 608 return std::move(__union_.__unex_); 609 } 610 611 _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept { 612 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 613 return std::move(__union_.__unex_); 614 } 615 616 template <class _Up> 617 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& { 618 static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible"); 619 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type"); 620 return __has_val_ ? __union_.__val_ : static_cast<_Tp>(std::forward<_Up>(__v)); 621 } 622 623 template <class _Up> 624 _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && { 625 static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible"); 626 static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type"); 627 return __has_val_ ? std::move(__union_.__val_) : static_cast<_Tp>(std::forward<_Up>(__v)); 628 } 629 630 template <class _Up = _Err> 631 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& { 632 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible"); 633 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type"); 634 if (has_value()) 635 return std::forward<_Up>(__error); 636 return error(); 637 } 638 639 template <class _Up = _Err> 640 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && { 641 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible"); 642 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type"); 643 if (has_value()) 644 return std::forward<_Up>(__error); 645 return std::move(error()); 646 } 647 648 // [expected.void.monadic], monadic 649 template <class _Func> 650 requires is_constructible_v<_Err, _Err&> 651 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & { 652 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>; 653 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected"); 654 static_assert(is_same_v<typename _Up::error_type, _Err>, 655 "The result of f(value()) must have the same error_type as this expected"); 656 if (has_value()) { 657 return std::invoke(std::forward<_Func>(__f), value()); 658 } 659 return _Up(unexpect, error()); 660 } 661 662 template <class _Func> 663 requires is_constructible_v<_Err, const _Err&> 664 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& { 665 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>; 666 static_assert(__is_std_expected<_Up>::value, "The result of f(value()) must be a specialization of std::expected"); 667 static_assert(is_same_v<typename _Up::error_type, _Err>, 668 "The result of f(value()) must have the same error_type as this expected"); 669 if (has_value()) { 670 return std::invoke(std::forward<_Func>(__f), value()); 671 } 672 return _Up(unexpect, error()); 673 } 674 675 template <class _Func> 676 requires is_constructible_v<_Err, _Err&&> 677 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && { 678 using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>; 679 static_assert( 680 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected"); 681 static_assert(is_same_v<typename _Up::error_type, _Err>, 682 "The result of f(std::move(value())) must have the same error_type as this expected"); 683 if (has_value()) { 684 return std::invoke(std::forward<_Func>(__f), std::move(value())); 685 } 686 return _Up(unexpect, std::move(error())); 687 } 688 689 template <class _Func> 690 requires is_constructible_v<_Err, const _Err&&> 691 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& { 692 using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>; 693 static_assert( 694 __is_std_expected<_Up>::value, "The result of f(std::move(value())) must be a specialization of std::expected"); 695 static_assert(is_same_v<typename _Up::error_type, _Err>, 696 "The result of f(std::move(value())) must have the same error_type as this expected"); 697 if (has_value()) { 698 return std::invoke(std::forward<_Func>(__f), std::move(value())); 699 } 700 return _Up(unexpect, std::move(error())); 701 } 702 703 template <class _Func> 704 requires is_constructible_v<_Tp, _Tp&> 705 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & { 706 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>; 707 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected"); 708 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 709 "The result of f(error()) must have the same value_type as this expected"); 710 if (has_value()) { 711 return _Gp(in_place, value()); 712 } 713 return std::invoke(std::forward<_Func>(__f), error()); 714 } 715 716 template <class _Func> 717 requires is_constructible_v<_Tp, const _Tp&> 718 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& { 719 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>; 720 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected"); 721 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 722 "The result of f(error()) must have the same value_type as this expected"); 723 if (has_value()) { 724 return _Gp(in_place, value()); 725 } 726 return std::invoke(std::forward<_Func>(__f), error()); 727 } 728 729 template <class _Func> 730 requires is_constructible_v<_Tp, _Tp&&> 731 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && { 732 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>; 733 static_assert( 734 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected"); 735 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 736 "The result of f(std::move(error())) must have the same value_type as this expected"); 737 if (has_value()) { 738 return _Gp(in_place, std::move(value())); 739 } 740 return std::invoke(std::forward<_Func>(__f), std::move(error())); 741 } 742 743 template <class _Func> 744 requires is_constructible_v<_Tp, const _Tp&&> 745 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& { 746 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>; 747 static_assert( 748 __is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected"); 749 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 750 "The result of f(std::move(error())) must have the same value_type as this expected"); 751 if (has_value()) { 752 return _Gp(in_place, std::move(value())); 753 } 754 return std::invoke(std::forward<_Func>(__f), std::move(error())); 755 } 756 757 template <class _Func> 758 requires is_constructible_v<_Err, _Err&> 759 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & { 760 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>; 761 if (!has_value()) { 762 return expected<_Up, _Err>(unexpect, error()); 763 } 764 if constexpr (!is_void_v<_Up>) { 765 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), value()); 766 } else { 767 std::invoke(std::forward<_Func>(__f), value()); 768 return expected<_Up, _Err>(); 769 } 770 } 771 772 template <class _Func> 773 requires is_constructible_v<_Err, const _Err&> 774 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& { 775 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>; 776 if (!has_value()) { 777 return expected<_Up, _Err>(unexpect, error()); 778 } 779 if constexpr (!is_void_v<_Up>) { 780 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), value()); 781 } else { 782 std::invoke(std::forward<_Func>(__f), value()); 783 return expected<_Up, _Err>(); 784 } 785 } 786 787 template <class _Func> 788 requires is_constructible_v<_Err, _Err&&> 789 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && { 790 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>; 791 if (!has_value()) { 792 return expected<_Up, _Err>(unexpect, std::move(error())); 793 } 794 if constexpr (!is_void_v<_Up>) { 795 return expected<_Up, _Err>( 796 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); 797 } else { 798 std::invoke(std::forward<_Func>(__f), std::move(value())); 799 return expected<_Up, _Err>(); 800 } 801 } 802 803 template <class _Func> 804 requires is_constructible_v<_Err, const _Err&&> 805 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& { 806 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>; 807 if (!has_value()) { 808 return expected<_Up, _Err>(unexpect, std::move(error())); 809 } 810 if constexpr (!is_void_v<_Up>) { 811 return expected<_Up, _Err>( 812 __expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value())); 813 } else { 814 std::invoke(std::forward<_Func>(__f), std::move(value())); 815 return expected<_Up, _Err>(); 816 } 817 } 818 819 template <class _Func> 820 requires is_constructible_v<_Tp, _Tp&> 821 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & { 822 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>; 823 static_assert(__valid_std_unexpected<_Gp>::value, 824 "The result of f(error()) must be a valid template argument for unexpected"); 825 if (has_value()) { 826 return expected<_Tp, _Gp>(in_place, value()); 827 } 828 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); 829 } 830 831 template <class _Func> 832 requires is_constructible_v<_Tp, const _Tp&> 833 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& { 834 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>; 835 static_assert(__valid_std_unexpected<_Gp>::value, 836 "The result of f(error()) must be a valid template argument for unexpected"); 837 if (has_value()) { 838 return expected<_Tp, _Gp>(in_place, value()); 839 } 840 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); 841 } 842 843 template <class _Func> 844 requires is_constructible_v<_Tp, _Tp&&> 845 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && { 846 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>; 847 static_assert(__valid_std_unexpected<_Gp>::value, 848 "The result of f(std::move(error())) must be a valid template argument for unexpected"); 849 if (has_value()) { 850 return expected<_Tp, _Gp>(in_place, std::move(value())); 851 } 852 return expected<_Tp, _Gp>( 853 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); 854 } 855 856 template <class _Func> 857 requires is_constructible_v<_Tp, const _Tp&&> 858 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& { 859 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>; 860 static_assert(__valid_std_unexpected<_Gp>::value, 861 "The result of f(std::move(error())) must be a valid template argument for unexpected"); 862 if (has_value()) { 863 return expected<_Tp, _Gp>(in_place, std::move(value())); 864 } 865 return expected<_Tp, _Gp>( 866 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); 867 } 868 869 // [expected.object.eq], equality operators 870 template <class _T2, class _E2> 871 requires(!is_void_v<_T2>) 872 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) { 873 if (__x.__has_val_ != __y.__has_val_) { 874 return false; 875 } else { 876 if (__x.__has_val_) { 877 return __x.__union_.__val_ == __y.__union_.__val_; 878 } else { 879 return __x.__union_.__unex_ == __y.__union_.__unex_; 880 } 881 } 882 } 883 884 template <class _T2> 885 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const _T2& __v) { 886 return __x.__has_val_ && static_cast<bool>(__x.__union_.__val_ == __v); 887 } 888 889 template <class _E2> 890 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) { 891 return !__x.__has_val_ && static_cast<bool>(__x.__union_.__unex_ == __e.error()); 892 } 893 894 private: 895 struct __empty_t {}; 896 897 template <class _ValueType, class _ErrorType> 898 union __union_t { 899 _LIBCPP_HIDE_FROM_ABI constexpr __union_t() {} 900 901 template <class _Func, class... _Args> 902 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 903 std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args) 904 : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 905 906 template <class _Func, class... _Args> 907 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 908 std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) 909 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 910 911 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 912 requires(is_trivially_destructible_v<_ValueType> && is_trivially_destructible_v<_ErrorType>) 913 = default; 914 915 // the expected's destructor handles this 916 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {} 917 918 _ValueType __val_; 919 _ErrorType __unex_; 920 }; 921 922 // use named union because [[no_unique_address]] cannot be applied to an unnamed union, 923 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and 924 // it's not clear that it's implementable, given that the function is allowed to clobber 925 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107. 926 template <class _ValueType, class _ErrorType> 927 requires(is_trivially_move_constructible_v<_ValueType> && is_trivially_move_constructible_v<_ErrorType>) 928 union __union_t<_ValueType, _ErrorType> { 929 _LIBCPP_HIDE_FROM_ABI constexpr __union_t() : __empty_() {} 930 931 template <class _Func, class... _Args> 932 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 933 std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args) 934 : __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 935 936 template <class _Func, class... _Args> 937 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 938 std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) 939 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 940 941 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 942 requires(is_trivially_destructible_v<_ValueType> && is_trivially_destructible_v<_ErrorType>) 943 = default; 944 945 // the expected's destructor handles this 946 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 947 requires(!is_trivially_destructible_v<_ValueType> || !is_trivially_destructible_v<_ErrorType>) 948 {} 949 950 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_; 951 _LIBCPP_NO_UNIQUE_ADDRESS _ValueType __val_; 952 _LIBCPP_NO_UNIQUE_ADDRESS _ErrorType __unex_; 953 }; 954 955 _LIBCPP_NO_UNIQUE_ADDRESS __union_t<_Tp, _Err> __union_; 956 bool __has_val_; 957 }; 958 959 template <class _Tp, class _Err> 960 requires is_void_v<_Tp> 961 class expected<_Tp, _Err> { 962 static_assert(__valid_std_unexpected<_Err>::value, 963 "[expected.void.general] A program that instantiates expected<T, E> with a E that is not a " 964 "valid argument for unexpected<E> is ill-formed"); 965 966 template <class, class> 967 friend class expected; 968 969 template <class _Up, class _OtherErr, class _OtherErrQual> 970 using __can_convert = 971 _And< is_void<_Up>, 972 is_constructible<_Err, _OtherErrQual>, 973 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>, 974 _Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>, 975 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>, 976 _Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>; 977 978 public: 979 using value_type = _Tp; 980 using error_type = _Err; 981 using unexpected_type = unexpected<_Err>; 982 983 template <class _Up> 984 using rebind = expected<_Up, error_type>; 985 986 // [expected.void.ctor], constructors 987 _LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __has_val_(true) {} 988 989 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete; 990 991 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) 992 requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>) 993 = default; 994 995 _LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) 996 noexcept(is_nothrow_copy_constructible_v<_Err>) // strengthened 997 requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>) 998 : __has_val_(__rhs.__has_val_) { 999 if (!__rhs.__has_val_) { 1000 std::construct_at(std::addressof(__union_.__unex_), __rhs.__union_.__unex_); 1001 } 1002 } 1003 1004 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&) 1005 requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>) 1006 = default; 1007 1008 _LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __rhs) 1009 noexcept(is_nothrow_move_constructible_v<_Err>) 1010 requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>) 1011 : __has_val_(__rhs.__has_val_) { 1012 if (!__rhs.__has_val_) { 1013 std::construct_at(std::addressof(__union_.__unex_), std::move(__rhs.__union_.__unex_)); 1014 } 1015 } 1016 1017 template <class _Up, class _OtherErr> 1018 requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value 1019 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) 1020 expected(const expected<_Up, _OtherErr>& __rhs) 1021 noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened 1022 : __has_val_(__rhs.__has_val_) { 1023 if (!__rhs.__has_val_) { 1024 std::construct_at(std::addressof(__union_.__unex_), __rhs.__union_.__unex_); 1025 } 1026 } 1027 1028 template <class _Up, class _OtherErr> 1029 requires __can_convert<_Up, _OtherErr, _OtherErr>::value 1030 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>) 1031 expected(expected<_Up, _OtherErr>&& __rhs) 1032 noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened 1033 : __has_val_(__rhs.__has_val_) { 1034 if (!__rhs.__has_val_) { 1035 std::construct_at(std::addressof(__union_.__unex_), std::move(__rhs.__union_.__unex_)); 1036 } 1037 } 1038 1039 template <class _OtherErr> 1040 requires is_constructible_v<_Err, const _OtherErr&> 1041 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) 1042 expected(const unexpected<_OtherErr>& __unex) 1043 noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened 1044 : __has_val_(false) { 1045 std::construct_at(std::addressof(__union_.__unex_), __unex.error()); 1046 } 1047 1048 template <class _OtherErr> 1049 requires is_constructible_v<_Err, _OtherErr> 1050 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>) 1051 expected(unexpected<_OtherErr>&& __unex) 1052 noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened 1053 : __has_val_(false) { 1054 std::construct_at(std::addressof(__union_.__unex_), std::move(__unex.error())); 1055 } 1056 1057 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __has_val_(true) {} 1058 1059 template <class... _Args> 1060 requires is_constructible_v<_Err, _Args...> 1061 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) 1062 noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened 1063 : __has_val_(false) { 1064 std::construct_at(std::addressof(__union_.__unex_), std::forward<_Args>(__args)...); 1065 } 1066 1067 template <class _Up, class... _Args> 1068 requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... > 1069 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) 1070 noexcept(is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened 1071 : __has_val_(false) { 1072 std::construct_at(std::addressof(__union_.__unex_), __il, std::forward<_Args>(__args)...); 1073 } 1074 1075 private: 1076 template <class _Func> 1077 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected(__expected_construct_in_place_from_invoke_tag, _Func&& __f) 1078 : __has_val_(true) { 1079 std::invoke(std::forward<_Func>(__f)); 1080 } 1081 1082 template <class _Func, class... _Args> 1083 _LIBCPP_HIDE_FROM_ABI constexpr explicit expected( 1084 __expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args) 1085 : __union_(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...), __has_val_(false) {} 1086 1087 public: 1088 // [expected.void.dtor], destructor 1089 1090 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() 1091 requires is_trivially_destructible_v<_Err> 1092 = default; 1093 1094 _LIBCPP_HIDE_FROM_ABI constexpr ~expected() 1095 requires(!is_trivially_destructible_v<_Err>) 1096 { 1097 if (!__has_val_) { 1098 std::destroy_at(std::addressof(__union_.__unex_)); 1099 } 1100 } 1101 1102 // [expected.void.assign], assignment 1103 1104 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete; 1105 1106 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) 1107 noexcept(is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened 1108 requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>) 1109 { 1110 if (__has_val_) { 1111 if (!__rhs.__has_val_) { 1112 std::construct_at(std::addressof(__union_.__unex_), __rhs.__union_.__unex_); 1113 __has_val_ = false; 1114 } 1115 } else { 1116 if (__rhs.__has_val_) { 1117 std::destroy_at(std::addressof(__union_.__unex_)); 1118 __has_val_ = true; 1119 } else { 1120 __union_.__unex_ = __rhs.__union_.__unex_; 1121 } 1122 } 1123 return *this; 1124 } 1125 1126 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&&) = delete; 1127 1128 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&& __rhs) 1129 noexcept(is_nothrow_move_assignable_v<_Err> && 1130 is_nothrow_move_constructible_v<_Err>) 1131 requires(is_move_assignable_v<_Err> && 1132 is_move_constructible_v<_Err>) 1133 { 1134 if (__has_val_) { 1135 if (!__rhs.__has_val_) { 1136 std::construct_at(std::addressof(__union_.__unex_), std::move(__rhs.__union_.__unex_)); 1137 __has_val_ = false; 1138 } 1139 } else { 1140 if (__rhs.__has_val_) { 1141 std::destroy_at(std::addressof(__union_.__unex_)); 1142 __has_val_ = true; 1143 } else { 1144 __union_.__unex_ = std::move(__rhs.__union_.__unex_); 1145 } 1146 } 1147 return *this; 1148 } 1149 1150 template <class _OtherErr> 1151 requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>) 1152 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) { 1153 if (__has_val_) { 1154 std::construct_at(std::addressof(__union_.__unex_), __un.error()); 1155 __has_val_ = false; 1156 } else { 1157 __union_.__unex_ = __un.error(); 1158 } 1159 return *this; 1160 } 1161 1162 template <class _OtherErr> 1163 requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>) 1164 _LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) { 1165 if (__has_val_) { 1166 std::construct_at(std::addressof(__union_.__unex_), std::move(__un.error())); 1167 __has_val_ = false; 1168 } else { 1169 __union_.__unex_ = std::move(__un.error()); 1170 } 1171 return *this; 1172 } 1173 1174 _LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept { 1175 if (!__has_val_) { 1176 std::destroy_at(std::addressof(__union_.__unex_)); 1177 __has_val_ = true; 1178 } 1179 } 1180 1181 // [expected.void.swap], swap 1182 _LIBCPP_HIDE_FROM_ABI constexpr void swap(expected& __rhs) 1183 noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>) 1184 requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>) 1185 { 1186 auto __swap_val_unex_impl = [&](expected& __with_val, expected& __with_err) { 1187 std::construct_at(std::addressof(__with_val.__union_.__unex_), std::move(__with_err.__union_.__unex_)); 1188 std::destroy_at(std::addressof(__with_err.__union_.__unex_)); 1189 __with_val.__has_val_ = false; 1190 __with_err.__has_val_ = true; 1191 }; 1192 1193 if (__has_val_) { 1194 if (!__rhs.__has_val_) { 1195 __swap_val_unex_impl(*this, __rhs); 1196 } 1197 } else { 1198 if (__rhs.__has_val_) { 1199 __swap_val_unex_impl(__rhs, *this); 1200 } else { 1201 using std::swap; 1202 swap(__union_.__unex_, __rhs.__union_.__unex_); 1203 } 1204 } 1205 } 1206 1207 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) 1208 noexcept(noexcept(__x.swap(__y))) 1209 requires requires { __x.swap(__y); } 1210 { 1211 __x.swap(__y); 1212 } 1213 1214 // [expected.void.obs], observers 1215 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return __has_val_; } 1216 1217 _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __has_val_; } 1218 1219 _LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept { 1220 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__has_val_, "expected::operator* requires the expected to contain a value"); 1221 } 1222 1223 _LIBCPP_HIDE_FROM_ABI constexpr void value() const& { 1224 if (!__has_val_) { 1225 std::__throw_bad_expected_access<_Err>(__union_.__unex_); 1226 } 1227 } 1228 1229 _LIBCPP_HIDE_FROM_ABI constexpr void value() && { 1230 if (!__has_val_) { 1231 std::__throw_bad_expected_access<_Err>(std::move(__union_.__unex_)); 1232 } 1233 } 1234 1235 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept { 1236 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 1237 return __union_.__unex_; 1238 } 1239 1240 _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept { 1241 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 1242 return __union_.__unex_; 1243 } 1244 1245 _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept { 1246 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 1247 return std::move(__union_.__unex_); 1248 } 1249 1250 _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept { 1251 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!__has_val_, "expected::error requires the expected to contain an error"); 1252 return std::move(__union_.__unex_); 1253 } 1254 1255 template <class _Up = _Err> 1256 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& { 1257 static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible"); 1258 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type"); 1259 if (has_value()) { 1260 return std::forward<_Up>(__error); 1261 } 1262 return error(); 1263 } 1264 1265 template <class _Up = _Err> 1266 _LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && { 1267 static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible"); 1268 static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type"); 1269 if (has_value()) { 1270 return std::forward<_Up>(__error); 1271 } 1272 return std::move(error()); 1273 } 1274 1275 // [expected.void.monadic], monadic 1276 template <class _Func> 1277 requires is_constructible_v<_Err, _Err&> 1278 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & { 1279 using _Up = remove_cvref_t<invoke_result_t<_Func>>; 1280 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected"); 1281 static_assert( 1282 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected"); 1283 if (has_value()) { 1284 return std::invoke(std::forward<_Func>(__f)); 1285 } 1286 return _Up(unexpect, error()); 1287 } 1288 1289 template <class _Func> 1290 requires is_constructible_v<_Err, const _Err&> 1291 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& { 1292 using _Up = remove_cvref_t<invoke_result_t<_Func>>; 1293 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected"); 1294 static_assert( 1295 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected"); 1296 if (has_value()) { 1297 return std::invoke(std::forward<_Func>(__f)); 1298 } 1299 return _Up(unexpect, error()); 1300 } 1301 1302 template <class _Func> 1303 requires is_constructible_v<_Err, _Err&&> 1304 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && { 1305 using _Up = remove_cvref_t<invoke_result_t<_Func>>; 1306 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected"); 1307 static_assert( 1308 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected"); 1309 if (has_value()) { 1310 return std::invoke(std::forward<_Func>(__f)); 1311 } 1312 return _Up(unexpect, std::move(error())); 1313 } 1314 1315 template <class _Func> 1316 requires is_constructible_v<_Err, const _Err&&> 1317 _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& { 1318 using _Up = remove_cvref_t<invoke_result_t<_Func>>; 1319 static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected"); 1320 static_assert( 1321 is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected"); 1322 if (has_value()) { 1323 return std::invoke(std::forward<_Func>(__f)); 1324 } 1325 return _Up(unexpect, std::move(error())); 1326 } 1327 1328 template <class _Func> 1329 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & { 1330 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>; 1331 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected"); 1332 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 1333 "The result of f(error()) must have the same value_type as this expected"); 1334 if (has_value()) { 1335 return _Gp(); 1336 } 1337 return std::invoke(std::forward<_Func>(__f), error()); 1338 } 1339 1340 template <class _Func> 1341 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& { 1342 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>; 1343 static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected"); 1344 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 1345 "The result of f(error()) must have the same value_type as this expected"); 1346 if (has_value()) { 1347 return _Gp(); 1348 } 1349 return std::invoke(std::forward<_Func>(__f), error()); 1350 } 1351 1352 template <class _Func> 1353 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && { 1354 using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>; 1355 static_assert(__is_std_expected<_Gp>::value, 1356 "The result of f(std::move(error())) must be a specialization of std::expected"); 1357 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 1358 "The result of f(std::move(error())) must have the same value_type as this expected"); 1359 if (has_value()) { 1360 return _Gp(); 1361 } 1362 return std::invoke(std::forward<_Func>(__f), std::move(error())); 1363 } 1364 1365 template <class _Func> 1366 _LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& { 1367 using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>; 1368 static_assert(__is_std_expected<_Gp>::value, 1369 "The result of f(std::move(error())) must be a specialization of std::expected"); 1370 static_assert(is_same_v<typename _Gp::value_type, _Tp>, 1371 "The result of f(std::move(error())) must have the same value_type as this expected"); 1372 if (has_value()) { 1373 return _Gp(); 1374 } 1375 return std::invoke(std::forward<_Func>(__f), std::move(error())); 1376 } 1377 1378 template <class _Func> 1379 requires is_constructible_v<_Err, _Err&> 1380 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & { 1381 using _Up = remove_cv_t<invoke_result_t<_Func>>; 1382 if (!has_value()) { 1383 return expected<_Up, _Err>(unexpect, error()); 1384 } 1385 if constexpr (!is_void_v<_Up>) { 1386 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); 1387 } else { 1388 std::invoke(std::forward<_Func>(__f)); 1389 return expected<_Up, _Err>(); 1390 } 1391 } 1392 1393 template <class _Func> 1394 requires is_constructible_v<_Err, const _Err&> 1395 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& { 1396 using _Up = remove_cv_t<invoke_result_t<_Func>>; 1397 if (!has_value()) { 1398 return expected<_Up, _Err>(unexpect, error()); 1399 } 1400 if constexpr (!is_void_v<_Up>) { 1401 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); 1402 } else { 1403 std::invoke(std::forward<_Func>(__f)); 1404 return expected<_Up, _Err>(); 1405 } 1406 } 1407 1408 template <class _Func> 1409 requires is_constructible_v<_Err, _Err&&> 1410 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && { 1411 using _Up = remove_cv_t<invoke_result_t<_Func>>; 1412 if (!has_value()) { 1413 return expected<_Up, _Err>(unexpect, std::move(error())); 1414 } 1415 if constexpr (!is_void_v<_Up>) { 1416 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); 1417 } else { 1418 std::invoke(std::forward<_Func>(__f)); 1419 return expected<_Up, _Err>(); 1420 } 1421 } 1422 1423 template <class _Func> 1424 requires is_constructible_v<_Err, const _Err&&> 1425 _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& { 1426 using _Up = remove_cv_t<invoke_result_t<_Func>>; 1427 if (!has_value()) { 1428 return expected<_Up, _Err>(unexpect, std::move(error())); 1429 } 1430 if constexpr (!is_void_v<_Up>) { 1431 return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f)); 1432 } else { 1433 std::invoke(std::forward<_Func>(__f)); 1434 return expected<_Up, _Err>(); 1435 } 1436 } 1437 1438 template <class _Func> 1439 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & { 1440 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>; 1441 static_assert(__valid_std_unexpected<_Gp>::value, 1442 "The result of f(error()) must be a valid template argument for unexpected"); 1443 if (has_value()) { 1444 return expected<_Tp, _Gp>(); 1445 } 1446 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); 1447 } 1448 1449 template <class _Func> 1450 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& { 1451 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>; 1452 static_assert(__valid_std_unexpected<_Gp>::value, 1453 "The result of f(error()) must be a valid template argument for unexpected"); 1454 if (has_value()) { 1455 return expected<_Tp, _Gp>(); 1456 } 1457 return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error()); 1458 } 1459 1460 template <class _Func> 1461 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && { 1462 using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>; 1463 static_assert(__valid_std_unexpected<_Gp>::value, 1464 "The result of f(std::move(error())) must be a valid template argument for unexpected"); 1465 if (has_value()) { 1466 return expected<_Tp, _Gp>(); 1467 } 1468 return expected<_Tp, _Gp>( 1469 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); 1470 } 1471 1472 template <class _Func> 1473 _LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& { 1474 using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>; 1475 static_assert(__valid_std_unexpected<_Gp>::value, 1476 "The result of f(std::move(error())) must be a valid template argument for unexpected"); 1477 if (has_value()) { 1478 return expected<_Tp, _Gp>(); 1479 } 1480 return expected<_Tp, _Gp>( 1481 __expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error())); 1482 } 1483 1484 // [expected.void.eq], equality operators 1485 template <class _T2, class _E2> 1486 requires is_void_v<_T2> 1487 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) { 1488 if (__x.__has_val_ != __y.__has_val_) { 1489 return false; 1490 } else { 1491 return __x.__has_val_ || static_cast<bool>(__x.__union_.__unex_ == __y.__union_.__unex_); 1492 } 1493 } 1494 1495 template <class _E2> 1496 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) { 1497 return !__x.__has_val_ && static_cast<bool>(__x.__union_.__unex_ == __y.error()); 1498 } 1499 1500 private: 1501 struct __empty_t {}; 1502 1503 template <class _ErrorType> 1504 union __union_t { 1505 _LIBCPP_HIDE_FROM_ABI constexpr __union_t() : __empty_() {} 1506 1507 template <class _Func, class... _Args> 1508 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 1509 __expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) 1510 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 1511 1512 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 1513 requires(is_trivially_destructible_v<_ErrorType>) 1514 = default; 1515 1516 // the expected's destructor handles this 1517 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {} 1518 1519 __empty_t __empty_; 1520 _ErrorType __unex_; 1521 }; 1522 1523 // use named union because [[no_unique_address]] cannot be applied to an unnamed union, 1524 // also guaranteed elision into a potentially-overlapping subobject is unsettled (and 1525 // it's not clear that it's implementable, given that the function is allowed to clobber 1526 // the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107. 1527 template <class _ErrorType> 1528 requires is_trivially_move_constructible_v<_ErrorType> 1529 union __union_t<_ErrorType> { 1530 _LIBCPP_HIDE_FROM_ABI constexpr __union_t() : __empty_() {} 1531 1532 template <class _Func, class... _Args> 1533 _LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t( 1534 __expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args) 1535 : __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} 1536 1537 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 1538 requires(is_trivially_destructible_v<_ErrorType>) 1539 = default; 1540 1541 // the expected's destructor handles this 1542 _LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() 1543 requires(!is_trivially_destructible_v<_ErrorType>) 1544 {} 1545 1546 _LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_; 1547 _LIBCPP_NO_UNIQUE_ADDRESS _ErrorType __unex_; 1548 }; 1549 1550 _LIBCPP_NO_UNIQUE_ADDRESS __union_t<_Err> __union_; 1551 bool __has_val_; 1552 }; 1553 1554 _LIBCPP_END_NAMESPACE_STD 1555 1556 #endif // _LIBCPP_STD_VER >= 23 1557 1558 _LIBCPP_POP_MACROS 1559 1560 #endif // _LIBCPP___EXPECTED_EXPECTED_H 1561