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