expected.h 79 KB

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