expected.h 80 KB

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