expected.h 64 KB

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