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