expected 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. // <expected> -*- C++ -*-
  2. // Copyright The GNU Toolchain Authors.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/expected
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPECTED
  24. #define _GLIBCXX_EXPECTED
  25. #pragma GCC system_header
  26. #if __cplusplus > 202002L && __cpp_concepts >= 202002L
  27. #include <initializer_list>
  28. #include <bits/exception.h> // exception
  29. #include <bits/invoke.h> // __invoke
  30. #include <bits/stl_construct.h> // construct_at
  31. #include <bits/utility.h> // in_place_t
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @defgroup expected_values Expected values
  37. * @addtogroup utilities
  38. * @since C++23
  39. * @{
  40. */
  41. #define __cpp_lib_expected 202211L
  42. /// Discriminated union that holds an expected value or an error value.
  43. /**
  44. * @since C++23
  45. */
  46. template<typename _Tp, typename _Er>
  47. class expected;
  48. /// Wrapper type used to pass an error value to a `std::expected`.
  49. /**
  50. * @since C++23
  51. */
  52. template<typename _Er>
  53. class unexpected;
  54. /// Exception thrown by std::expected when the value() is not present.
  55. /**
  56. * @since C++23
  57. */
  58. template<typename _Er>
  59. class bad_expected_access;
  60. template<>
  61. class bad_expected_access<void> : public exception
  62. {
  63. protected:
  64. bad_expected_access() noexcept { }
  65. bad_expected_access(const bad_expected_access&) = default;
  66. bad_expected_access(bad_expected_access&&) = default;
  67. bad_expected_access& operator=(const bad_expected_access&) = default;
  68. bad_expected_access& operator=(bad_expected_access&&) = default;
  69. ~bad_expected_access() = default;
  70. public:
  71. [[nodiscard]]
  72. const char*
  73. what() const noexcept override
  74. { return "bad access to std::expected without expected value"; }
  75. };
  76. template<typename _Er>
  77. class bad_expected_access : public bad_expected_access<void> {
  78. public:
  79. explicit
  80. bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
  81. // XXX const char* what() const noexcept override;
  82. [[nodiscard]]
  83. _Er&
  84. error() & noexcept
  85. { return _M_unex; }
  86. [[nodiscard]]
  87. const _Er&
  88. error() const & noexcept
  89. { return _M_unex; }
  90. [[nodiscard]]
  91. _Er&&
  92. error() && noexcept
  93. { return std::move(_M_unex); }
  94. [[nodiscard]]
  95. const _Er&&
  96. error() const && noexcept
  97. { return std::move(_M_unex); }
  98. private:
  99. _Er _M_unex;
  100. };
  101. /// Tag type for constructing unexpected values in a std::expected
  102. /**
  103. * @since C++23
  104. */
  105. struct unexpect_t
  106. {
  107. explicit unexpect_t() = default;
  108. };
  109. /// Tag for constructing unexpected values in a std::expected
  110. /**
  111. * @since C++23
  112. */
  113. inline constexpr unexpect_t unexpect{};
  114. /// @cond undocumented
  115. namespace __expected
  116. {
  117. template<typename _Tp>
  118. constexpr bool __is_expected = false;
  119. template<typename _Tp, typename _Er>
  120. constexpr bool __is_expected<expected<_Tp, _Er>> = true;
  121. template<typename _Tp>
  122. constexpr bool __is_unexpected = false;
  123. template<typename _Tp>
  124. constexpr bool __is_unexpected<unexpected<_Tp>> = true;
  125. template<typename _Fn, typename _Tp>
  126. using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
  127. template<typename _Fn, typename _Tp>
  128. using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
  129. template<typename _Fn>
  130. using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
  131. template<typename _Fn>
  132. using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
  133. template<typename _Er>
  134. concept __can_be_unexpected
  135. = is_object_v<_Er> && (!is_array_v<_Er>)
  136. && (!__expected::__is_unexpected<_Er>)
  137. && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
  138. // Tag types for in-place construction from an invocation result.
  139. struct __in_place_inv { };
  140. struct __unexpect_inv { };
  141. }
  142. /// @endcond
  143. template<typename _Er>
  144. class unexpected
  145. {
  146. static_assert( __expected::__can_be_unexpected<_Er> );
  147. public:
  148. constexpr unexpected(const unexpected&) = default;
  149. constexpr unexpected(unexpected&&) = default;
  150. template<typename _Err = _Er>
  151. requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
  152. && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
  153. && is_constructible_v<_Er, _Err>
  154. constexpr explicit
  155. unexpected(_Err&& __e)
  156. noexcept(is_nothrow_constructible_v<_Er, _Err>)
  157. : _M_unex(std::forward<_Err>(__e))
  158. { }
  159. template<typename... _Args>
  160. requires is_constructible_v<_Er, _Args...>
  161. constexpr explicit
  162. unexpected(in_place_t, _Args&&... __args)
  163. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  164. : _M_unex(std::forward<_Args>(__args)...)
  165. { }
  166. template<typename _Up, typename... _Args>
  167. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  168. constexpr explicit
  169. unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  170. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  171. _Args...>)
  172. : _M_unex(__il, std::forward<_Args>(__args)...)
  173. { }
  174. constexpr unexpected& operator=(const unexpected&) = default;
  175. constexpr unexpected& operator=(unexpected&&) = default;
  176. [[nodiscard]]
  177. constexpr const _Er&
  178. error() const & noexcept { return _M_unex; }
  179. [[nodiscard]]
  180. constexpr _Er&
  181. error() & noexcept { return _M_unex; }
  182. [[nodiscard]]
  183. constexpr const _Er&&
  184. error() const && noexcept { return std::move(_M_unex); }
  185. [[nodiscard]]
  186. constexpr _Er&&
  187. error() && noexcept { return std::move(_M_unex); }
  188. constexpr void
  189. swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
  190. requires is_swappable_v<_Er>
  191. {
  192. using std::swap;
  193. swap(_M_unex, __other._M_unex);
  194. }
  195. template<typename _Err>
  196. [[nodiscard]]
  197. friend constexpr bool
  198. operator==(const unexpected& __x, const unexpected<_Err>& __y)
  199. { return __x._M_unex == __y.error(); }
  200. friend constexpr void
  201. swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
  202. requires is_swappable_v<_Er>
  203. { __x.swap(__y); }
  204. private:
  205. _Er _M_unex;
  206. };
  207. template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
  208. /// @cond undocumented
  209. namespace __expected
  210. {
  211. template<typename _Tp>
  212. struct _Guard
  213. {
  214. static_assert( is_nothrow_move_constructible_v<_Tp> );
  215. constexpr explicit
  216. _Guard(_Tp& __x)
  217. : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
  218. { std::destroy_at(_M_guarded); }
  219. constexpr
  220. ~_Guard()
  221. {
  222. if (_M_guarded) [[unlikely]]
  223. std::construct_at(_M_guarded, std::move(_M_tmp));
  224. }
  225. _Guard(const _Guard&) = delete;
  226. _Guard& operator=(const _Guard&) = delete;
  227. constexpr _Tp&&
  228. release() noexcept
  229. {
  230. _M_guarded = nullptr;
  231. return std::move(_M_tmp);
  232. }
  233. private:
  234. _Tp* _M_guarded;
  235. _Tp _M_tmp;
  236. };
  237. // reinit-expected helper from [expected.object.assign]
  238. template<typename _Tp, typename _Up, typename _Vp>
  239. constexpr void
  240. __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
  241. noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
  242. {
  243. if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
  244. {
  245. std::destroy_at(__oldval);
  246. std::construct_at(__newval, std::forward<_Vp>(__arg));
  247. }
  248. else if constexpr (is_nothrow_move_constructible_v<_Tp>)
  249. {
  250. _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
  251. std::destroy_at(__oldval);
  252. std::construct_at(__newval, std::move(__tmp));
  253. }
  254. else
  255. {
  256. _Guard<_Up> __guard(*__oldval);
  257. std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
  258. __guard.release();
  259. }
  260. }
  261. }
  262. /// @endcond
  263. template<typename _Tp, typename _Er>
  264. class expected
  265. {
  266. static_assert( ! is_reference_v<_Tp> );
  267. static_assert( ! is_function_v<_Tp> );
  268. static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
  269. static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
  270. static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
  271. static_assert( __expected::__can_be_unexpected<_Er> );
  272. template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
  273. static constexpr bool __cons_from_expected
  274. = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
  275. is_constructible<_Tp, expected<_Up, _Err>>,
  276. is_constructible<_Tp, const expected<_Up, _Err>&>,
  277. is_constructible<_Tp, const expected<_Up, _Err>>,
  278. is_convertible<expected<_Up, _Err>&, _Tp>,
  279. is_convertible<expected<_Up, _Err>, _Tp>,
  280. is_convertible<const expected<_Up, _Err>&, _Tp>,
  281. is_convertible<const expected<_Up, _Err>, _Tp>,
  282. is_constructible<_Unex, expected<_Up, _Err>&>,
  283. is_constructible<_Unex, expected<_Up, _Err>>,
  284. is_constructible<_Unex, const expected<_Up, _Err>&>,
  285. is_constructible<_Unex, const expected<_Up, _Err>>
  286. >;
  287. template<typename _Up, typename _Err>
  288. constexpr static bool __explicit_conv
  289. = __or_v<__not_<is_convertible<_Up, _Tp>>,
  290. __not_<is_convertible<_Err, _Er>>
  291. >;
  292. template<typename _Up>
  293. static constexpr bool __same_val
  294. = is_same_v<typename _Up::value_type, _Tp>;
  295. template<typename _Up>
  296. static constexpr bool __same_err
  297. = is_same_v<typename _Up::error_type, _Er>;
  298. public:
  299. using value_type = _Tp;
  300. using error_type = _Er;
  301. using unexpected_type = unexpected<_Er>;
  302. template<typename _Up>
  303. using rebind = expected<_Up, error_type>;
  304. constexpr
  305. expected()
  306. noexcept(is_nothrow_default_constructible_v<_Tp>)
  307. requires is_default_constructible_v<_Tp>
  308. : _M_val(), _M_has_value(true)
  309. { }
  310. expected(const expected&) = default;
  311. constexpr
  312. expected(const expected& __x)
  313. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  314. is_nothrow_copy_constructible<_Er>>)
  315. requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
  316. && (!is_trivially_copy_constructible_v<_Tp>
  317. || !is_trivially_copy_constructible_v<_Er>)
  318. : _M_has_value(__x._M_has_value)
  319. {
  320. if (_M_has_value)
  321. std::construct_at(__builtin_addressof(_M_val), __x._M_val);
  322. else
  323. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  324. }
  325. expected(expected&&) = default;
  326. constexpr
  327. expected(expected&& __x)
  328. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  329. is_nothrow_move_constructible<_Er>>)
  330. requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
  331. && (!is_trivially_move_constructible_v<_Tp>
  332. || !is_trivially_move_constructible_v<_Er>)
  333. : _M_has_value(__x._M_has_value)
  334. {
  335. if (_M_has_value)
  336. std::construct_at(__builtin_addressof(_M_val),
  337. std::move(__x)._M_val);
  338. else
  339. std::construct_at(__builtin_addressof(_M_unex),
  340. std::move(__x)._M_unex);
  341. }
  342. template<typename _Up, typename _Gr>
  343. requires is_constructible_v<_Tp, const _Up&>
  344. && is_constructible_v<_Er, const _Gr&>
  345. && (!__cons_from_expected<_Up, _Gr>)
  346. constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
  347. expected(const expected<_Up, _Gr>& __x)
  348. noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
  349. is_nothrow_constructible<_Er, const _Gr&>>)
  350. : _M_has_value(__x._M_has_value)
  351. {
  352. if (_M_has_value)
  353. std::construct_at(__builtin_addressof(_M_val), __x._M_val);
  354. else
  355. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  356. }
  357. template<typename _Up, typename _Gr>
  358. requires is_constructible_v<_Tp, _Up>
  359. && is_constructible_v<_Er, _Gr>
  360. && (!__cons_from_expected<_Up, _Gr>)
  361. constexpr explicit(__explicit_conv<_Up, _Gr>)
  362. expected(expected<_Up, _Gr>&& __x)
  363. noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
  364. is_nothrow_constructible<_Er, _Gr>>)
  365. : _M_has_value(__x._M_has_value)
  366. {
  367. if (_M_has_value)
  368. std::construct_at(__builtin_addressof(_M_val),
  369. std::move(__x)._M_val);
  370. else
  371. std::construct_at(__builtin_addressof(_M_unex),
  372. std::move(__x)._M_unex);
  373. }
  374. template<typename _Up = _Tp>
  375. requires (!is_same_v<remove_cvref_t<_Up>, expected>)
  376. && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
  377. && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
  378. && is_constructible_v<_Tp, _Up>
  379. constexpr explicit(!is_convertible_v<_Up, _Tp>)
  380. expected(_Up&& __v)
  381. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  382. : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
  383. { }
  384. template<typename _Gr = _Er>
  385. requires is_constructible_v<_Er, const _Gr&>
  386. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  387. expected(const unexpected<_Gr>& __u)
  388. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  389. : _M_unex(__u.error()), _M_has_value(false)
  390. { }
  391. template<typename _Gr = _Er>
  392. requires is_constructible_v<_Er, _Gr>
  393. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  394. expected(unexpected<_Gr>&& __u)
  395. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  396. : _M_unex(std::move(__u).error()), _M_has_value(false)
  397. { }
  398. template<typename... _Args>
  399. requires is_constructible_v<_Tp, _Args...>
  400. constexpr explicit
  401. expected(in_place_t, _Args&&... __args)
  402. noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
  403. : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
  404. { }
  405. template<typename _Up, typename... _Args>
  406. requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
  407. constexpr explicit
  408. expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  409. noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  410. _Args...>)
  411. : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
  412. { }
  413. template<typename... _Args>
  414. requires is_constructible_v<_Er, _Args...>
  415. constexpr explicit
  416. expected(unexpect_t, _Args&&... __args)
  417. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  418. : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
  419. { }
  420. template<typename _Up, typename... _Args>
  421. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  422. constexpr explicit
  423. expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
  424. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  425. _Args...>)
  426. : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
  427. { }
  428. constexpr ~expected() = default;
  429. constexpr ~expected()
  430. requires (!is_trivially_destructible_v<_Tp>)
  431. || (!is_trivially_destructible_v<_Er>)
  432. {
  433. if (_M_has_value)
  434. std::destroy_at(__builtin_addressof(_M_val));
  435. else
  436. std::destroy_at(__builtin_addressof(_M_unex));
  437. }
  438. // assignment
  439. expected& operator=(const expected&) = delete;
  440. constexpr expected&
  441. operator=(const expected& __x)
  442. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  443. is_nothrow_copy_constructible<_Er>,
  444. is_nothrow_copy_assignable<_Tp>,
  445. is_nothrow_copy_assignable<_Er>>)
  446. requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
  447. && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
  448. && (is_nothrow_move_constructible_v<_Tp>
  449. || is_nothrow_move_constructible_v<_Er>)
  450. {
  451. if (__x._M_has_value)
  452. this->_M_assign_val(__x._M_val);
  453. else
  454. this->_M_assign_unex(__x._M_unex);
  455. return *this;
  456. }
  457. constexpr expected&
  458. operator=(expected&& __x)
  459. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  460. is_nothrow_move_constructible<_Er>,
  461. is_nothrow_move_assignable<_Tp>,
  462. is_nothrow_move_assignable<_Er>>)
  463. requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
  464. && is_move_assignable_v<_Er> && is_move_constructible_v<_Er>
  465. && (is_nothrow_move_constructible_v<_Tp>
  466. || is_nothrow_move_constructible_v<_Er>)
  467. {
  468. if (__x._M_has_value)
  469. _M_assign_val(std::move(__x._M_val));
  470. else
  471. _M_assign_unex(std::move(__x._M_unex));
  472. return *this;
  473. }
  474. template<typename _Up = _Tp>
  475. requires (!is_same_v<expected, remove_cvref_t<_Up>>)
  476. && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
  477. && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
  478. && (is_nothrow_constructible_v<_Tp, _Up>
  479. || is_nothrow_move_constructible_v<_Tp>
  480. || is_nothrow_move_constructible_v<_Er>)
  481. constexpr expected&
  482. operator=(_Up&& __v)
  483. {
  484. _M_assign_val(std::forward<_Up>(__v));
  485. return *this;
  486. }
  487. template<typename _Gr>
  488. requires is_constructible_v<_Er, const _Gr&>
  489. && is_assignable_v<_Er&, const _Gr&>
  490. && (is_nothrow_constructible_v<_Er, const _Gr&>
  491. || is_nothrow_move_constructible_v<_Tp>
  492. || is_nothrow_move_constructible_v<_Er>)
  493. constexpr expected&
  494. operator=(const unexpected<_Gr>& __e)
  495. {
  496. _M_assign_unex(__e.error());
  497. return *this;
  498. }
  499. template<typename _Gr>
  500. requires is_constructible_v<_Er, _Gr>
  501. && is_assignable_v<_Er&, _Gr>
  502. && (is_nothrow_constructible_v<_Er, _Gr>
  503. || is_nothrow_move_constructible_v<_Tp>
  504. || is_nothrow_move_constructible_v<_Er>)
  505. constexpr expected&
  506. operator=(unexpected<_Gr>&& __e)
  507. {
  508. _M_assign_unex(std::move(__e).error());
  509. return *this;
  510. }
  511. // modifiers
  512. template<typename... _Args>
  513. requires is_nothrow_constructible_v<_Tp, _Args...>
  514. constexpr _Tp&
  515. emplace(_Args&&... __args) noexcept
  516. {
  517. if (_M_has_value)
  518. std::destroy_at(__builtin_addressof(_M_val));
  519. else
  520. {
  521. std::destroy_at(__builtin_addressof(_M_unex));
  522. _M_has_value = true;
  523. }
  524. std::construct_at(__builtin_addressof(_M_val),
  525. std::forward<_Args>(__args)...);
  526. return _M_val;
  527. }
  528. template<typename _Up, typename... _Args>
  529. requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  530. _Args...>
  531. constexpr _Tp&
  532. emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept
  533. {
  534. if (_M_has_value)
  535. std::destroy_at(__builtin_addressof(_M_val));
  536. else
  537. {
  538. std::destroy_at(__builtin_addressof(_M_unex));
  539. _M_has_value = true;
  540. }
  541. std::construct_at(__builtin_addressof(_M_val),
  542. __il, std::forward<_Args>(__args)...);
  543. return _M_val;
  544. }
  545. // swap
  546. constexpr void
  547. swap(expected& __x)
  548. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  549. is_nothrow_move_constructible<_Er>,
  550. is_nothrow_swappable<_Tp&>,
  551. is_nothrow_swappable<_Er&>>)
  552. requires is_swappable_v<_Tp> && is_swappable_v<_Er>
  553. && is_move_constructible_v<_Tp>
  554. && is_move_constructible_v<_Er>
  555. && (is_nothrow_move_constructible_v<_Tp>
  556. || is_nothrow_move_constructible_v<_Er>)
  557. {
  558. if (_M_has_value)
  559. {
  560. if (__x._M_has_value)
  561. {
  562. using std::swap;
  563. swap(_M_val, __x._M_val);
  564. }
  565. else
  566. this->_M_swap_val_unex(__x);
  567. }
  568. else
  569. {
  570. if (__x._M_has_value)
  571. __x._M_swap_val_unex(*this);
  572. else
  573. {
  574. using std::swap;
  575. swap(_M_unex, __x._M_unex);
  576. }
  577. }
  578. }
  579. // observers
  580. [[nodiscard]]
  581. constexpr const _Tp*
  582. operator->() const noexcept
  583. {
  584. __glibcxx_assert(_M_has_value);
  585. return __builtin_addressof(_M_val);
  586. }
  587. [[nodiscard]]
  588. constexpr _Tp*
  589. operator->() noexcept
  590. {
  591. __glibcxx_assert(_M_has_value);
  592. return __builtin_addressof(_M_val);
  593. }
  594. [[nodiscard]]
  595. constexpr const _Tp&
  596. operator*() const & noexcept
  597. {
  598. __glibcxx_assert(_M_has_value);
  599. return _M_val;
  600. }
  601. [[nodiscard]]
  602. constexpr _Tp&
  603. operator*() & noexcept
  604. {
  605. __glibcxx_assert(_M_has_value);
  606. return _M_val;
  607. }
  608. [[nodiscard]]
  609. constexpr const _Tp&&
  610. operator*() const && noexcept
  611. {
  612. __glibcxx_assert(_M_has_value);
  613. return std::move(_M_val);
  614. }
  615. [[nodiscard]]
  616. constexpr _Tp&&
  617. operator*() && noexcept
  618. {
  619. __glibcxx_assert(_M_has_value);
  620. return std::move(_M_val);
  621. }
  622. [[nodiscard]]
  623. constexpr explicit
  624. operator bool() const noexcept { return _M_has_value; }
  625. [[nodiscard]]
  626. constexpr bool has_value() const noexcept { return _M_has_value; }
  627. constexpr const _Tp&
  628. value() const &
  629. {
  630. if (_M_has_value) [[likely]]
  631. return _M_val;
  632. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
  633. }
  634. constexpr _Tp&
  635. value() &
  636. {
  637. if (_M_has_value) [[likely]]
  638. return _M_val;
  639. const auto& __unex = _M_unex;
  640. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
  641. }
  642. constexpr const _Tp&&
  643. value() const &&
  644. {
  645. if (_M_has_value) [[likely]]
  646. return std::move(_M_val);
  647. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
  648. }
  649. constexpr _Tp&&
  650. value() &&
  651. {
  652. if (_M_has_value) [[likely]]
  653. return std::move(_M_val);
  654. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
  655. }
  656. constexpr const _Er&
  657. error() const & noexcept
  658. {
  659. __glibcxx_assert(!_M_has_value);
  660. return _M_unex;
  661. }
  662. constexpr _Er&
  663. error() & noexcept
  664. {
  665. __glibcxx_assert(!_M_has_value);
  666. return _M_unex;
  667. }
  668. constexpr const _Er&&
  669. error() const && noexcept
  670. {
  671. __glibcxx_assert(!_M_has_value);
  672. return std::move(_M_unex);
  673. }
  674. constexpr _Er&&
  675. error() && noexcept
  676. {
  677. __glibcxx_assert(!_M_has_value);
  678. return std::move(_M_unex);
  679. }
  680. template<typename _Up>
  681. constexpr _Tp
  682. value_or(_Up&& __v) const &
  683. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  684. is_nothrow_convertible<_Up, _Tp>>)
  685. {
  686. static_assert( is_copy_constructible_v<_Tp> );
  687. static_assert( is_convertible_v<_Up, _Tp> );
  688. if (_M_has_value)
  689. return _M_val;
  690. return static_cast<_Tp>(std::forward<_Up>(__v));
  691. }
  692. template<typename _Up>
  693. constexpr _Tp
  694. value_or(_Up&& __v) &&
  695. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  696. is_nothrow_convertible<_Up, _Tp>>)
  697. {
  698. static_assert( is_move_constructible_v<_Tp> );
  699. static_assert( is_convertible_v<_Up, _Tp> );
  700. if (_M_has_value)
  701. return std::move(_M_val);
  702. return static_cast<_Tp>(std::forward<_Up>(__v));
  703. }
  704. template<typename _Gr = _Er>
  705. constexpr _Er
  706. error_or(_Gr&& __e) const&
  707. {
  708. static_assert( is_copy_constructible_v<_Er> );
  709. static_assert( is_convertible_v<_Gr, _Er> );
  710. if (_M_has_value)
  711. return std::forward<_Gr>(__e);
  712. return _M_unex;
  713. }
  714. template<typename _Gr = _Er>
  715. constexpr _Er
  716. error_or(_Gr&& __e) &&
  717. {
  718. static_assert( is_move_constructible_v<_Er> );
  719. static_assert( is_convertible_v<_Gr, _Er> );
  720. if (_M_has_value)
  721. return std::forward<_Gr>(__e);
  722. return std::move(_M_unex);
  723. }
  724. // monadic operations
  725. template<typename _Fn> requires is_constructible_v<_Er, _Er&>
  726. constexpr auto
  727. and_then(_Fn&& __f) &
  728. {
  729. using _Up = __expected::__result<_Fn, _Tp&>;
  730. static_assert(__expected::__is_expected<_Up>,
  731. "the function passed to std::expected<T, E>::and_then "
  732. "must return a std::expected");
  733. static_assert(is_same_v<typename _Up::error_type, _Er>,
  734. "the function passed to std::expected<T, E>::and_then "
  735. "must return a std::expected with the same error_type");
  736. if (has_value())
  737. return std::__invoke(std::forward<_Fn>(__f), _M_val);
  738. else
  739. return _Up(unexpect, _M_unex);
  740. }
  741. template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
  742. constexpr auto
  743. and_then(_Fn&& __f) const &
  744. {
  745. using _Up = __expected::__result<_Fn, const _Tp&>;
  746. static_assert(__expected::__is_expected<_Up>,
  747. "the function passed to std::expected<T, E>::and_then "
  748. "must return a std::expected");
  749. static_assert(is_same_v<typename _Up::error_type, _Er>,
  750. "the function passed to std::expected<T, E>::and_then "
  751. "must return a std::expected with the same error_type");
  752. if (has_value())
  753. return std::__invoke(std::forward<_Fn>(__f), _M_val);
  754. else
  755. return _Up(unexpect, _M_unex);
  756. }
  757. template<typename _Fn> requires is_constructible_v<_Er, _Er>
  758. constexpr auto
  759. and_then(_Fn&& __f) &&
  760. {
  761. using _Up = __expected::__result<_Fn, _Tp&&>;
  762. static_assert(__expected::__is_expected<_Up>,
  763. "the function passed to std::expected<T, E>::and_then "
  764. "must return a std::expected");
  765. static_assert(is_same_v<typename _Up::error_type, _Er>,
  766. "the function passed to std::expected<T, E>::and_then "
  767. "must return a std::expected with the same error_type");
  768. if (has_value())
  769. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
  770. else
  771. return _Up(unexpect, std::move(_M_unex));
  772. }
  773. template<typename _Fn> requires is_constructible_v<_Er, const _Er>
  774. constexpr auto
  775. and_then(_Fn&& __f) const &&
  776. {
  777. using _Up = __expected::__result<_Fn, const _Tp&&>;
  778. static_assert(__expected::__is_expected<_Up>,
  779. "the function passed to std::expected<T, E>::and_then "
  780. "must return a std::expected");
  781. static_assert(is_same_v<typename _Up::error_type, _Er>,
  782. "the function passed to std::expected<T, E>::and_then "
  783. "must return a std::expected with the same error_type");
  784. if (has_value())
  785. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val));
  786. else
  787. return _Up(unexpect, std::move(_M_unex));
  788. }
  789. template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
  790. constexpr auto
  791. or_else(_Fn&& __f) &
  792. {
  793. using _Gr = __expected::__result<_Fn, _Er&>;
  794. static_assert(__expected::__is_expected<_Gr>,
  795. "the function passed to std::expected<T, E>::or_else "
  796. "must return a std::expected");
  797. static_assert(is_same_v<typename _Gr::value_type, _Tp>,
  798. "the function passed to std::expected<T, E>::or_else "
  799. "must return a std::expected with the same value_type");
  800. if (has_value())
  801. return _Gr(in_place, _M_val);
  802. else
  803. return std::__invoke(std::forward<_Fn>(__f), _M_unex);
  804. }
  805. template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
  806. constexpr auto
  807. or_else(_Fn&& __f) const &
  808. {
  809. using _Gr = __expected::__result<_Fn, const _Er&>;
  810. static_assert(__expected::__is_expected<_Gr>,
  811. "the function passed to std::expected<T, E>::or_else "
  812. "must return a std::expected");
  813. static_assert(is_same_v<typename _Gr::value_type, _Tp>,
  814. "the function passed to std::expected<T, E>::or_else "
  815. "must return a std::expected with the same value_type");
  816. if (has_value())
  817. return _Gr(in_place, _M_val);
  818. else
  819. return std::__invoke(std::forward<_Fn>(__f), _M_unex);
  820. }
  821. template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
  822. constexpr auto
  823. or_else(_Fn&& __f) &&
  824. {
  825. using _Gr = __expected::__result<_Fn, _Er&&>;
  826. static_assert(__expected::__is_expected<_Gr>,
  827. "the function passed to std::expected<T, E>::or_else "
  828. "must return a std::expected");
  829. static_assert(is_same_v<typename _Gr::value_type, _Tp>,
  830. "the function passed to std::expected<T, E>::or_else "
  831. "must return a std::expected with the same value_type");
  832. if (has_value())
  833. return _Gr(in_place, std::move(_M_val));
  834. else
  835. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
  836. }
  837. template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
  838. constexpr auto
  839. or_else(_Fn&& __f) const &&
  840. {
  841. using _Gr = __expected::__result<_Fn, const _Er&&>;
  842. static_assert(__expected::__is_expected<_Gr>,
  843. "the function passed to std::expected<T, E>::or_else "
  844. "must return a std::expected");
  845. static_assert(is_same_v<typename _Gr::value_type, _Tp>,
  846. "the function passed to std::expected<T, E>::or_else "
  847. "must return a std::expected with the same value_type");
  848. if (has_value())
  849. return _Gr(in_place, std::move(_M_val));
  850. else
  851. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
  852. }
  853. template<typename _Fn> requires is_constructible_v<_Er, _Er&>
  854. constexpr auto
  855. transform(_Fn&& __f) &
  856. {
  857. using _Up = __expected::__result_xform<_Fn, _Tp&>;
  858. using _Res = expected<_Up, _Er>;
  859. if (has_value())
  860. return _Res(__in_place_inv{}, [&]() {
  861. return std::__invoke(std::forward<_Fn>(__f),
  862. _M_val);
  863. });
  864. else
  865. return _Res(unexpect, _M_unex);
  866. }
  867. template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
  868. constexpr auto
  869. transform(_Fn&& __f) const &
  870. {
  871. using _Up = __expected::__result_xform<_Fn, const _Tp&>;
  872. using _Res = expected<_Up, _Er>;
  873. if (has_value())
  874. return _Res(__in_place_inv{}, [&]() {
  875. return std::__invoke(std::forward<_Fn>(__f),
  876. _M_val);
  877. });
  878. else
  879. return _Res(unexpect, _M_unex);
  880. }
  881. template<typename _Fn> requires is_constructible_v<_Er, _Er>
  882. constexpr auto
  883. transform(_Fn&& __f) &&
  884. {
  885. using _Up = __expected::__result_xform<_Fn, _Tp>;
  886. using _Res = expected<_Up, _Er>;
  887. if (has_value())
  888. return _Res(__in_place_inv{}, [&]() {
  889. return std::__invoke(std::forward<_Fn>(__f),
  890. std::move(_M_val));
  891. });
  892. else
  893. return _Res(unexpect, std::move(_M_unex));
  894. }
  895. template<typename _Fn> requires is_constructible_v<_Er, const _Er>
  896. constexpr auto
  897. transform(_Fn&& __f) const &&
  898. {
  899. using _Up = __expected::__result_xform<_Fn, const _Tp>;
  900. using _Res = expected<_Up, _Er>;
  901. if (has_value())
  902. return _Res(__in_place_inv{}, [&]() {
  903. return std::__invoke(std::forward<_Fn>(__f),
  904. std::move(_M_val));
  905. });
  906. else
  907. return _Res(unexpect, std::move(_M_unex));
  908. }
  909. template<typename _Fn> requires is_constructible_v<_Tp, _Tp&>
  910. constexpr auto
  911. transform_error(_Fn&& __f) &
  912. {
  913. using _Gr = __expected::__result_xform<_Fn, _Er&>;
  914. using _Res = expected<_Tp, _Gr>;
  915. if (has_value())
  916. return _Res(in_place, _M_val);
  917. else
  918. return _Res(__unexpect_inv{}, [&]() {
  919. return std::__invoke(std::forward<_Fn>(__f),
  920. _M_unex);
  921. });
  922. }
  923. template<typename _Fn> requires is_constructible_v<_Tp, const _Tp&>
  924. constexpr auto
  925. transform_error(_Fn&& __f) const &
  926. {
  927. using _Gr = __expected::__result_xform<_Fn, const _Er&>;
  928. using _Res = expected<_Tp, _Gr>;
  929. if (has_value())
  930. return _Res(in_place, _M_val);
  931. else
  932. return _Res(__unexpect_inv{}, [&]() {
  933. return std::__invoke(std::forward<_Fn>(__f),
  934. _M_unex);
  935. });
  936. }
  937. template<typename _Fn> requires is_constructible_v<_Tp, _Tp>
  938. constexpr auto
  939. transform_error(_Fn&& __f) &&
  940. {
  941. using _Gr = __expected::__result_xform<_Fn, _Er&&>;
  942. using _Res = expected<_Tp, _Gr>;
  943. if (has_value())
  944. return _Res(in_place, std::move(_M_val));
  945. else
  946. return _Res(__unexpect_inv{}, [&]() {
  947. return std::__invoke(std::forward<_Fn>(__f),
  948. std::move(_M_unex));
  949. });
  950. }
  951. template<typename _Fn> requires is_constructible_v<_Tp, const _Tp>
  952. constexpr auto
  953. transform_error(_Fn&& __f) const &&
  954. {
  955. using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
  956. using _Res = expected<_Tp, _Gr>;
  957. if (has_value())
  958. return _Res(in_place, std::move(_M_val));
  959. else
  960. return _Res(__unexpect_inv{}, [&]() {
  961. return std::__invoke(std::forward<_Fn>(__f),
  962. std::move(_M_unex));
  963. });
  964. }
  965. // equality operators
  966. template<typename _Up, typename _Er2>
  967. requires (!is_void_v<_Up>)
  968. friend constexpr bool
  969. operator==(const expected& __x, const expected<_Up, _Er2>& __y)
  970. // FIXME: noexcept(noexcept(bool(*__x == *__y))
  971. // && noexcept(bool(__x.error() == __y.error())))
  972. {
  973. if (__x.has_value())
  974. return __y.has_value() && bool(*__x == *__y);
  975. else
  976. return !__y.has_value() && bool(__x.error() == __y.error());
  977. }
  978. template<typename _Up>
  979. friend constexpr bool
  980. operator==(const expected& __x, const _Up& __v)
  981. // FIXME: noexcept(noexcept(bool(*__x == __v)))
  982. { return __x.has_value() && bool(*__x == __v); }
  983. template<typename _Er2>
  984. friend constexpr bool
  985. operator==(const expected& __x, const unexpected<_Er2>& __e)
  986. // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
  987. { return !__x.has_value() && bool(__x.error() == __e.error()); }
  988. friend constexpr void
  989. swap(expected& __x, expected& __y)
  990. noexcept(noexcept(__x.swap(__y)))
  991. requires requires {__x.swap(__y);}
  992. { __x.swap(__y); }
  993. private:
  994. template<typename, typename> friend class expected;
  995. template<typename _Vp>
  996. constexpr void
  997. _M_assign_val(_Vp&& __v)
  998. {
  999. if (_M_has_value)
  1000. _M_val = std::forward<_Vp>(__v);
  1001. else
  1002. {
  1003. __expected::__reinit(__builtin_addressof(_M_val),
  1004. __builtin_addressof(_M_unex),
  1005. std::forward<_Vp>(__v));
  1006. _M_has_value = true;
  1007. }
  1008. }
  1009. template<typename _Vp>
  1010. constexpr void
  1011. _M_assign_unex(_Vp&& __v)
  1012. {
  1013. if (_M_has_value)
  1014. {
  1015. __expected::__reinit(__builtin_addressof(_M_unex),
  1016. __builtin_addressof(_M_val),
  1017. std::forward<_Vp>(__v));
  1018. _M_has_value = false;
  1019. }
  1020. else
  1021. _M_unex = std::forward<_Vp>(__v);
  1022. }
  1023. // Swap two expected objects when only one has a value.
  1024. // Precondition: this->_M_has_value && !__rhs._M_has_value
  1025. constexpr void
  1026. _M_swap_val_unex(expected& __rhs)
  1027. noexcept(__and_v<is_nothrow_move_constructible<_Er>,
  1028. is_nothrow_move_constructible<_Tp>>)
  1029. {
  1030. if constexpr (is_nothrow_move_constructible_v<_Er>)
  1031. {
  1032. __expected::_Guard<_Er> __guard(__rhs._M_unex);
  1033. std::construct_at(__builtin_addressof(__rhs._M_val),
  1034. std::move(_M_val)); // might throw
  1035. __rhs._M_has_value = true;
  1036. std::destroy_at(__builtin_addressof(_M_val));
  1037. std::construct_at(__builtin_addressof(_M_unex),
  1038. __guard.release());
  1039. _M_has_value = false;
  1040. }
  1041. else
  1042. {
  1043. __expected::_Guard<_Tp> __guard(_M_val);
  1044. std::construct_at(__builtin_addressof(_M_unex),
  1045. std::move(__rhs._M_unex)); // might throw
  1046. _M_has_value = false;
  1047. std::destroy_at(__builtin_addressof(__rhs._M_unex));
  1048. std::construct_at(__builtin_addressof(__rhs._M_val),
  1049. __guard.release());
  1050. __rhs._M_has_value = true;
  1051. }
  1052. }
  1053. using __in_place_inv = __expected::__in_place_inv;
  1054. using __unexpect_inv = __expected::__unexpect_inv;
  1055. template<typename _Fn>
  1056. explicit constexpr
  1057. expected(__in_place_inv, _Fn&& __fn)
  1058. : _M_val(std::forward<_Fn>(__fn)()), _M_has_value(true)
  1059. { }
  1060. template<typename _Fn>
  1061. explicit constexpr
  1062. expected(__unexpect_inv, _Fn&& __fn)
  1063. : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
  1064. { }
  1065. union {
  1066. _Tp _M_val;
  1067. _Er _M_unex;
  1068. };
  1069. bool _M_has_value;
  1070. };
  1071. // Partial specialization for std::expected<cv void, E>
  1072. template<typename _Tp, typename _Er> requires is_void_v<_Tp>
  1073. class expected<_Tp, _Er>
  1074. {
  1075. static_assert( __expected::__can_be_unexpected<_Er> );
  1076. template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
  1077. static constexpr bool __cons_from_expected
  1078. = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
  1079. is_constructible<_Unex, expected<_Up, _Err>>,
  1080. is_constructible<_Unex, const expected<_Up, _Err>&>,
  1081. is_constructible<_Unex, const expected<_Up, _Err>>
  1082. >;
  1083. template<typename _Up>
  1084. static constexpr bool __same_val
  1085. = is_same_v<typename _Up::value_type, _Tp>;
  1086. template<typename _Up>
  1087. static constexpr bool __same_err
  1088. = is_same_v<typename _Up::error_type, _Er>;
  1089. public:
  1090. using value_type = _Tp;
  1091. using error_type = _Er;
  1092. using unexpected_type = unexpected<_Er>;
  1093. template<typename _Up>
  1094. using rebind = expected<_Up, error_type>;
  1095. constexpr
  1096. expected() noexcept
  1097. : _M_void(), _M_has_value(true)
  1098. { }
  1099. expected(const expected&) = default;
  1100. constexpr
  1101. expected(const expected& __x)
  1102. noexcept(is_nothrow_copy_constructible_v<_Er>)
  1103. requires is_copy_constructible_v<_Er>
  1104. && (!is_trivially_copy_constructible_v<_Er>)
  1105. : _M_void(), _M_has_value(__x._M_has_value)
  1106. {
  1107. if (!_M_has_value)
  1108. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  1109. }
  1110. expected(expected&&) = default;
  1111. constexpr
  1112. expected(expected&& __x)
  1113. noexcept(is_nothrow_move_constructible_v<_Er>)
  1114. requires is_move_constructible_v<_Er>
  1115. && (!is_trivially_move_constructible_v<_Er>)
  1116. : _M_void(), _M_has_value(__x._M_has_value)
  1117. {
  1118. if (!_M_has_value)
  1119. std::construct_at(__builtin_addressof(_M_unex),
  1120. std::move(__x)._M_unex);
  1121. }
  1122. template<typename _Up, typename _Gr>
  1123. requires is_void_v<_Up>
  1124. && is_constructible_v<_Er, const _Gr&>
  1125. && (!__cons_from_expected<_Up, _Gr>)
  1126. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  1127. expected(const expected<_Up, _Gr>& __x)
  1128. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  1129. : _M_void(), _M_has_value(__x._M_has_value)
  1130. {
  1131. if (!_M_has_value)
  1132. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  1133. }
  1134. template<typename _Up, typename _Gr>
  1135. requires is_void_v<_Up>
  1136. && is_constructible_v<_Er, _Gr>
  1137. && (!__cons_from_expected<_Up, _Gr>)
  1138. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  1139. expected(expected<_Up, _Gr>&& __x)
  1140. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  1141. : _M_void(), _M_has_value(__x._M_has_value)
  1142. {
  1143. if (!_M_has_value)
  1144. std::construct_at(__builtin_addressof(_M_unex),
  1145. std::move(__x)._M_unex);
  1146. }
  1147. template<typename _Gr = _Er>
  1148. requires is_constructible_v<_Er, const _Gr&>
  1149. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  1150. expected(const unexpected<_Gr>& __u)
  1151. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  1152. : _M_unex(__u.error()), _M_has_value(false)
  1153. { }
  1154. template<typename _Gr = _Er>
  1155. requires is_constructible_v<_Er, _Gr>
  1156. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  1157. expected(unexpected<_Gr>&& __u)
  1158. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  1159. : _M_unex(std::move(__u).error()), _M_has_value(false)
  1160. { }
  1161. constexpr explicit
  1162. expected(in_place_t) noexcept
  1163. : expected()
  1164. { }
  1165. template<typename... _Args>
  1166. requires is_constructible_v<_Er, _Args...>
  1167. constexpr explicit
  1168. expected(unexpect_t, _Args&&... __args)
  1169. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  1170. : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
  1171. { }
  1172. template<typename _Up, typename... _Args>
  1173. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  1174. constexpr explicit
  1175. expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
  1176. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  1177. _Args...>)
  1178. : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
  1179. { }
  1180. constexpr ~expected() = default;
  1181. constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
  1182. {
  1183. if (!_M_has_value)
  1184. std::destroy_at(__builtin_addressof(_M_unex));
  1185. }
  1186. // assignment
  1187. expected& operator=(const expected&) = delete;
  1188. constexpr expected&
  1189. operator=(const expected& __x)
  1190. noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
  1191. is_nothrow_copy_assignable<_Er>>)
  1192. requires is_copy_constructible_v<_Er>
  1193. && is_copy_assignable_v<_Er>
  1194. {
  1195. if (__x._M_has_value)
  1196. emplace();
  1197. else
  1198. _M_assign_unex(__x._M_unex);
  1199. return *this;
  1200. }
  1201. constexpr expected&
  1202. operator=(expected&& __x)
  1203. noexcept(__and_v<is_nothrow_move_constructible<_Er>,
  1204. is_nothrow_move_assignable<_Er>>)
  1205. requires is_move_constructible_v<_Er>
  1206. && is_move_assignable_v<_Er>
  1207. {
  1208. if (__x._M_has_value)
  1209. emplace();
  1210. else
  1211. _M_assign_unex(std::move(__x._M_unex));
  1212. return *this;
  1213. }
  1214. template<typename _Gr>
  1215. requires is_constructible_v<_Er, const _Gr&>
  1216. && is_assignable_v<_Er&, const _Gr&>
  1217. constexpr expected&
  1218. operator=(const unexpected<_Gr>& __e)
  1219. {
  1220. _M_assign_unex(__e.error());
  1221. return *this;
  1222. }
  1223. template<typename _Gr>
  1224. requires is_constructible_v<_Er, _Gr>
  1225. && is_assignable_v<_Er&, _Gr>
  1226. constexpr expected&
  1227. operator=(unexpected<_Gr>&& __e)
  1228. {
  1229. _M_assign_unex(std::move(__e.error()));
  1230. return *this;
  1231. }
  1232. // modifiers
  1233. constexpr void
  1234. emplace() noexcept
  1235. {
  1236. if (!_M_has_value)
  1237. {
  1238. std::destroy_at(__builtin_addressof(_M_unex));
  1239. _M_has_value = true;
  1240. }
  1241. }
  1242. // swap
  1243. constexpr void
  1244. swap(expected& __x)
  1245. noexcept(__and_v<is_nothrow_swappable<_Er&>,
  1246. is_nothrow_move_constructible<_Er>>)
  1247. requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
  1248. {
  1249. if (_M_has_value)
  1250. {
  1251. if (!__x._M_has_value)
  1252. {
  1253. std::construct_at(__builtin_addressof(_M_unex),
  1254. std::move(__x._M_unex)); // might throw
  1255. std::destroy_at(__builtin_addressof(__x._M_unex));
  1256. _M_has_value = false;
  1257. __x._M_has_value = true;
  1258. }
  1259. }
  1260. else
  1261. {
  1262. if (__x._M_has_value)
  1263. {
  1264. std::construct_at(__builtin_addressof(__x._M_unex),
  1265. std::move(_M_unex)); // might throw
  1266. std::destroy_at(__builtin_addressof(_M_unex));
  1267. _M_has_value = true;
  1268. __x._M_has_value = false;
  1269. }
  1270. else
  1271. {
  1272. using std::swap;
  1273. swap(_M_unex, __x._M_unex);
  1274. }
  1275. }
  1276. }
  1277. // observers
  1278. [[nodiscard]]
  1279. constexpr explicit
  1280. operator bool() const noexcept { return _M_has_value; }
  1281. [[nodiscard]]
  1282. constexpr bool has_value() const noexcept { return _M_has_value; }
  1283. constexpr void
  1284. operator*() const noexcept { __glibcxx_assert(_M_has_value); }
  1285. constexpr void
  1286. value() const&
  1287. {
  1288. if (_M_has_value) [[likely]]
  1289. return;
  1290. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
  1291. }
  1292. constexpr void
  1293. value() &&
  1294. {
  1295. if (_M_has_value) [[likely]]
  1296. return;
  1297. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
  1298. }
  1299. constexpr const _Er&
  1300. error() const & noexcept
  1301. {
  1302. __glibcxx_assert(!_M_has_value);
  1303. return _M_unex;
  1304. }
  1305. constexpr _Er&
  1306. error() & noexcept
  1307. {
  1308. __glibcxx_assert(!_M_has_value);
  1309. return _M_unex;
  1310. }
  1311. constexpr const _Er&&
  1312. error() const && noexcept
  1313. {
  1314. __glibcxx_assert(!_M_has_value);
  1315. return std::move(_M_unex);
  1316. }
  1317. constexpr _Er&&
  1318. error() && noexcept
  1319. {
  1320. __glibcxx_assert(!_M_has_value);
  1321. return std::move(_M_unex);
  1322. }
  1323. template<typename _Gr = _Er>
  1324. constexpr _Er
  1325. error_or(_Gr&& __e) const&
  1326. {
  1327. static_assert( is_copy_constructible_v<_Er> );
  1328. static_assert( is_convertible_v<_Gr, _Er> );
  1329. if (_M_has_value)
  1330. return std::forward<_Gr>(__e);
  1331. return _M_unex;
  1332. }
  1333. template<typename _Gr = _Er>
  1334. constexpr _Er
  1335. error_or(_Gr&& __e) &&
  1336. {
  1337. static_assert( is_move_constructible_v<_Er> );
  1338. static_assert( is_convertible_v<_Gr, _Er> );
  1339. if (_M_has_value)
  1340. return std::forward<_Gr>(__e);
  1341. return std::move(_M_unex);
  1342. }
  1343. // monadic operations
  1344. template<typename _Fn> requires is_constructible_v<_Er, _Er&>
  1345. constexpr auto
  1346. and_then(_Fn&& __f) &
  1347. {
  1348. using _Up = __expected::__result0<_Fn>;
  1349. static_assert(__expected::__is_expected<_Up>);
  1350. static_assert(is_same_v<typename _Up::error_type, _Er>);
  1351. if (has_value())
  1352. return std::__invoke(std::forward<_Fn>(__f));
  1353. else
  1354. return _Up(unexpect, _M_unex);
  1355. }
  1356. template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
  1357. constexpr auto
  1358. and_then(_Fn&& __f) const &
  1359. {
  1360. using _Up = __expected::__result0<_Fn>;
  1361. static_assert(__expected::__is_expected<_Up>);
  1362. static_assert(is_same_v<typename _Up::error_type, _Er>);
  1363. if (has_value())
  1364. return std::__invoke(std::forward<_Fn>(__f));
  1365. else
  1366. return _Up(unexpect, _M_unex);
  1367. }
  1368. template<typename _Fn> requires is_constructible_v<_Er, _Er>
  1369. constexpr auto
  1370. and_then(_Fn&& __f) &&
  1371. {
  1372. using _Up = __expected::__result0<_Fn>;
  1373. static_assert(__expected::__is_expected<_Up>);
  1374. static_assert(is_same_v<typename _Up::error_type, _Er>);
  1375. if (has_value())
  1376. return std::__invoke(std::forward<_Fn>(__f));
  1377. else
  1378. return _Up(unexpect, std::move(_M_unex));
  1379. }
  1380. template<typename _Fn> requires is_constructible_v<_Er, const _Er>
  1381. constexpr auto
  1382. and_then(_Fn&& __f) const &&
  1383. {
  1384. using _Up = __expected::__result0<_Fn>;
  1385. static_assert(__expected::__is_expected<_Up>);
  1386. static_assert(is_same_v<typename _Up::error_type, _Er>);
  1387. if (has_value())
  1388. return std::__invoke(std::forward<_Fn>(__f));
  1389. else
  1390. return _Up(unexpect, std::move(_M_unex));
  1391. }
  1392. template<typename _Fn>
  1393. constexpr auto
  1394. or_else(_Fn&& __f) &
  1395. {
  1396. using _Gr = __expected::__result<_Fn, _Er&>;
  1397. static_assert(__expected::__is_expected<_Gr>);
  1398. static_assert(is_same_v<typename _Gr::value_type, _Tp>);
  1399. if (has_value())
  1400. return _Gr();
  1401. else
  1402. return std::__invoke(std::forward<_Fn>(__f), _M_unex);
  1403. }
  1404. template<typename _Fn>
  1405. constexpr auto
  1406. or_else(_Fn&& __f) const &
  1407. {
  1408. using _Gr = __expected::__result<_Fn, const _Er&>;
  1409. static_assert(__expected::__is_expected<_Gr>);
  1410. static_assert(is_same_v<typename _Gr::value_type, _Tp>);
  1411. if (has_value())
  1412. return _Gr();
  1413. else
  1414. return std::__invoke(std::forward<_Fn>(__f), _M_unex);
  1415. }
  1416. template<typename _Fn>
  1417. constexpr auto
  1418. or_else(_Fn&& __f) &&
  1419. {
  1420. using _Gr = __expected::__result<_Fn, _Er&&>;
  1421. static_assert(__expected::__is_expected<_Gr>);
  1422. static_assert(is_same_v<typename _Gr::value_type, _Tp>);
  1423. if (has_value())
  1424. return _Gr();
  1425. else
  1426. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
  1427. }
  1428. template<typename _Fn>
  1429. constexpr auto
  1430. or_else(_Fn&& __f) const &&
  1431. {
  1432. using _Gr = __expected::__result<_Fn, const _Er&&>;
  1433. static_assert(__expected::__is_expected<_Gr>);
  1434. static_assert(is_same_v<typename _Gr::value_type, _Tp>);
  1435. if (has_value())
  1436. return _Gr();
  1437. else
  1438. return std::__invoke(std::forward<_Fn>(__f), std::move(_M_unex));
  1439. }
  1440. template<typename _Fn> requires is_constructible_v<_Er, _Er&>
  1441. constexpr auto
  1442. transform(_Fn&& __f) &
  1443. {
  1444. using _Up = __expected::__result0_xform<_Fn>;
  1445. using _Res = expected<_Up, _Er>;
  1446. if (has_value())
  1447. return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
  1448. else
  1449. return _Res(unexpect, _M_unex);
  1450. }
  1451. template<typename _Fn> requires is_constructible_v<_Er, const _Er&>
  1452. constexpr auto
  1453. transform(_Fn&& __f) const &
  1454. {
  1455. using _Up = __expected::__result0_xform<_Fn>;
  1456. using _Res = expected<_Up, _Er>;
  1457. if (has_value())
  1458. return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
  1459. else
  1460. return _Res(unexpect, _M_unex);
  1461. }
  1462. template<typename _Fn> requires is_constructible_v<_Er, _Er>
  1463. constexpr auto
  1464. transform(_Fn&& __f) &&
  1465. {
  1466. using _Up = __expected::__result0_xform<_Fn>;
  1467. using _Res = expected<_Up, _Er>;
  1468. if (has_value())
  1469. return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
  1470. else
  1471. return _Res(unexpect, std::move(_M_unex));
  1472. }
  1473. template<typename _Fn> requires is_constructible_v<_Er, const _Er>
  1474. constexpr auto
  1475. transform(_Fn&& __f) const &&
  1476. {
  1477. using _Up = __expected::__result0_xform<_Fn>;
  1478. using _Res = expected<_Up, _Er>;
  1479. if (has_value())
  1480. return _Res(__in_place_inv{}, std::forward<_Fn>(__f));
  1481. else
  1482. return _Res(unexpect, std::move(_M_unex));
  1483. }
  1484. template<typename _Fn>
  1485. constexpr auto
  1486. transform_error(_Fn&& __f) &
  1487. {
  1488. using _Gr = __expected::__result_xform<_Fn, _Er&>;
  1489. using _Res = expected<_Tp, _Gr>;
  1490. if (has_value())
  1491. return _Res();
  1492. else
  1493. return _Res(__unexpect_inv{}, [&]() {
  1494. return std::__invoke(std::forward<_Fn>(__f),
  1495. _M_unex);
  1496. });
  1497. }
  1498. template<typename _Fn>
  1499. constexpr auto
  1500. transform_error(_Fn&& __f) const &
  1501. {
  1502. using _Gr = __expected::__result_xform<_Fn, const _Er&>;
  1503. using _Res = expected<_Tp, _Gr>;
  1504. if (has_value())
  1505. return _Res();
  1506. else
  1507. return _Res(__unexpect_inv{}, [&]() {
  1508. return std::__invoke(std::forward<_Fn>(__f),
  1509. _M_unex);
  1510. });
  1511. }
  1512. template<typename _Fn>
  1513. constexpr auto
  1514. transform_error(_Fn&& __f) &&
  1515. {
  1516. using _Gr = __expected::__result_xform<_Fn, _Er&&>;
  1517. using _Res = expected<_Tp, _Gr>;
  1518. if (has_value())
  1519. return _Res();
  1520. else
  1521. return _Res(__unexpect_inv{}, [&]() {
  1522. return std::__invoke(std::forward<_Fn>(__f),
  1523. std::move(_M_unex));
  1524. });
  1525. }
  1526. template<typename _Fn>
  1527. constexpr auto
  1528. transform_error(_Fn&& __f) const &&
  1529. {
  1530. using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
  1531. using _Res = expected<_Tp, _Gr>;
  1532. if (has_value())
  1533. return _Res();
  1534. else
  1535. return _Res(__unexpect_inv{}, [&]() {
  1536. return std::__invoke(std::forward<_Fn>(__f),
  1537. std::move(_M_unex));
  1538. });
  1539. }
  1540. // equality operators
  1541. template<typename _Up, typename _Er2>
  1542. requires is_void_v<_Up>
  1543. friend constexpr bool
  1544. operator==(const expected& __x, const expected<_Up, _Er2>& __y)
  1545. // FIXME: noexcept(noexcept(bool(__x.error() == __y.error())))
  1546. {
  1547. if (__x.has_value())
  1548. return __y.has_value();
  1549. else
  1550. return !__y.has_value() && bool(__x.error() == __y.error());
  1551. }
  1552. template<typename _Er2>
  1553. friend constexpr bool
  1554. operator==(const expected& __x, const unexpected<_Er2>& __e)
  1555. // FIXME: noexcept(noexcept(bool(__x.error() == __e.error())))
  1556. { return !__x.has_value() && bool(__x.error() == __e.error()); }
  1557. friend constexpr void
  1558. swap(expected& __x, expected& __y)
  1559. noexcept(noexcept(__x.swap(__y)))
  1560. requires requires { __x.swap(__y); }
  1561. { __x.swap(__y); }
  1562. private:
  1563. template<typename, typename> friend class expected;
  1564. template<typename _Vp>
  1565. constexpr void
  1566. _M_assign_unex(_Vp&& __v)
  1567. {
  1568. if (_M_has_value)
  1569. {
  1570. std::construct_at(__builtin_addressof(_M_unex),
  1571. std::forward<_Vp>(__v));
  1572. _M_has_value = false;
  1573. }
  1574. else
  1575. _M_unex = std::forward<_Vp>(__v);
  1576. }
  1577. using __in_place_inv = __expected::__in_place_inv;
  1578. using __unexpect_inv = __expected::__unexpect_inv;
  1579. template<typename _Fn>
  1580. explicit constexpr
  1581. expected(__in_place_inv, _Fn&& __fn)
  1582. : _M_void(), _M_has_value(true)
  1583. { std::forward<_Fn>(__fn)(); }
  1584. template<typename _Fn>
  1585. explicit constexpr
  1586. expected(__unexpect_inv, _Fn&& __fn)
  1587. : _M_unex(std::forward<_Fn>(__fn)()), _M_has_value(false)
  1588. { }
  1589. union {
  1590. struct { } _M_void;
  1591. _Er _M_unex;
  1592. };
  1593. bool _M_has_value;
  1594. };
  1595. /// @}
  1596. _GLIBCXX_END_NAMESPACE_VERSION
  1597. } // namespace std
  1598. #endif // C++23
  1599. #endif // _GLIBCXX_EXPECTED