expected 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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/stl_construct.h> // construct_at
  30. #include <bits/utility.h> // in_place_t
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /**
  35. * @defgroup expected_values Expected values
  36. * @addtogroup utilities
  37. * @since C++23
  38. * @{
  39. */
  40. #define __cpp_lib_expected 202202L
  41. /// Discriminated union that holds an expected value or an error value.
  42. /**
  43. * @since C++23
  44. */
  45. template<typename _Tp, typename _Er>
  46. class expected;
  47. /// Wrapper type used to pass an error value to a `std::expected`.
  48. /**
  49. * @since C++23
  50. */
  51. template<typename _Er>
  52. class unexpected;
  53. /// Exception thrown by std::expected when the value() is not present.
  54. /**
  55. * @since C++23
  56. */
  57. template<typename _Er>
  58. class bad_expected_access;
  59. template<>
  60. class bad_expected_access<void> : public exception
  61. {
  62. protected:
  63. bad_expected_access() noexcept { }
  64. bad_expected_access(const bad_expected_access&) = default;
  65. bad_expected_access(bad_expected_access&&) = default;
  66. bad_expected_access& operator=(const bad_expected_access&) = default;
  67. bad_expected_access& operator=(bad_expected_access&&) = default;
  68. ~bad_expected_access() = default;
  69. public:
  70. [[nodiscard]]
  71. const char*
  72. what() const noexcept override
  73. { return "bad access to std::expected without expected value"; }
  74. };
  75. template<typename _Er>
  76. class bad_expected_access : public bad_expected_access<void> {
  77. public:
  78. explicit
  79. bad_expected_access(_Er __e) : _M_unex(std::move(__e)) { }
  80. // XXX const char* what() const noexcept override;
  81. [[nodiscard]]
  82. _Er&
  83. error() & noexcept
  84. { return _M_unex; }
  85. [[nodiscard]]
  86. const _Er&
  87. error() const & noexcept
  88. { return _M_unex; }
  89. [[nodiscard]]
  90. _Er&&
  91. error() && noexcept
  92. { return std::move(_M_unex); }
  93. [[nodiscard]]
  94. const _Er&&
  95. error() const && noexcept
  96. { return std::move(_M_unex); }
  97. private:
  98. _Er _M_unex;
  99. };
  100. /// Tag type for constructing unexpected values in a std::expected
  101. /**
  102. * @since C++23
  103. */
  104. struct unexpect_t
  105. {
  106. explicit unexpect_t() = default;
  107. };
  108. /// Tag for constructing unexpected values in a std::expected
  109. /**
  110. * @since C++23
  111. */
  112. inline constexpr unexpect_t unexpect{};
  113. /// @cond undoc
  114. namespace __expected
  115. {
  116. template<typename _Tp>
  117. constexpr bool __is_expected = false;
  118. template<typename _Tp, typename _Er>
  119. constexpr bool __is_expected<expected<_Tp, _Er>> = true;
  120. template<typename _Tp>
  121. constexpr bool __is_unexpected = false;
  122. template<typename _Tp>
  123. constexpr bool __is_unexpected<unexpected<_Tp>> = true;
  124. template<typename _Er>
  125. concept __can_be_unexpected
  126. = is_object_v<_Er> && (!is_array_v<_Er>)
  127. && (!__expected::__is_unexpected<_Er>)
  128. && (!is_const_v<_Er>) && (!is_volatile_v<_Er>);
  129. }
  130. /// @endcond
  131. template<typename _Er>
  132. class unexpected
  133. {
  134. static_assert( __expected::__can_be_unexpected<_Er> );
  135. public:
  136. constexpr unexpected(const unexpected&) = default;
  137. constexpr unexpected(unexpected&&) = default;
  138. template<typename _Err = _Er>
  139. requires (!is_same_v<remove_cvref_t<_Err>, unexpected>)
  140. && (!is_same_v<remove_cvref_t<_Err>, in_place_t>)
  141. && is_constructible_v<_Er, _Err>
  142. constexpr explicit
  143. unexpected(_Err&& __e)
  144. noexcept(is_nothrow_constructible_v<_Er, _Err>)
  145. : _M_unex(std::forward<_Err>(__e))
  146. { }
  147. template<typename... _Args>
  148. requires is_constructible_v<_Er, _Args...>
  149. constexpr explicit
  150. unexpected(in_place_t, _Args&&... __args)
  151. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  152. : _M_unex(std::forward<_Args>(__args)...)
  153. { }
  154. template<typename _Up, typename... _Args>
  155. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  156. constexpr explicit
  157. unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  158. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  159. _Args...>)
  160. : _M_unex(__il, std::forward<_Args>(__args)...)
  161. { }
  162. constexpr unexpected& operator=(const unexpected&) = default;
  163. constexpr unexpected& operator=(unexpected&&) = default;
  164. [[nodiscard]]
  165. constexpr const _Er&
  166. error() const & noexcept { return _M_unex; }
  167. [[nodiscard]]
  168. constexpr _Er&
  169. error() & noexcept { return _M_unex; }
  170. [[nodiscard]]
  171. constexpr const _Er&&
  172. error() const && noexcept { return std::move(_M_unex); }
  173. [[nodiscard]]
  174. constexpr _Er&&
  175. error() && noexcept { return std::move(_M_unex); }
  176. constexpr void
  177. swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Er>)
  178. {
  179. static_assert( is_swappable_v<_Er> );
  180. using std::swap;
  181. swap(_M_unex, __other._M_unex);
  182. }
  183. template<typename _Err>
  184. [[nodiscard]]
  185. friend constexpr bool
  186. operator==(const unexpected& __x, const unexpected<_Err>& __y)
  187. { return __x._M_unex == __y.error(); }
  188. friend constexpr void
  189. swap(unexpected& __x, unexpected& __y)
  190. noexcept(noexcept(__x.swap(__y)))
  191. requires requires {__x.swap(__y);}
  192. { __x.swap(__y); }
  193. private:
  194. _Er _M_unex;
  195. };
  196. template<typename _Er> unexpected(_Er) -> unexpected<_Er>;
  197. /// @cond undoc
  198. namespace __expected
  199. {
  200. template<typename _Tp>
  201. struct _Guard
  202. {
  203. static_assert( is_nothrow_move_constructible_v<_Tp> );
  204. constexpr explicit
  205. _Guard(_Tp& __x)
  206. : _M_guarded(__builtin_addressof(__x)), _M_tmp(std::move(__x)) // nothrow
  207. { std::destroy_at(_M_guarded); }
  208. constexpr
  209. ~_Guard()
  210. {
  211. if (_M_guarded) [[unlikely]]
  212. std::construct_at(_M_guarded, std::move(_M_tmp));
  213. }
  214. _Guard(const _Guard&) = delete;
  215. _Guard& operator=(const _Guard&) = delete;
  216. constexpr _Tp&&
  217. release() noexcept
  218. {
  219. _M_guarded = nullptr;
  220. return std::move(_M_tmp);
  221. }
  222. private:
  223. _Tp* _M_guarded;
  224. _Tp _M_tmp;
  225. };
  226. // reinit-expected helper from [expected.object.assign]
  227. template<typename _Tp, typename _Up, typename _Vp>
  228. constexpr void
  229. __reinit(_Tp* __newval, _Up* __oldval, _Vp&& __arg)
  230. noexcept(is_nothrow_constructible_v<_Tp, _Vp>)
  231. {
  232. if constexpr (is_nothrow_constructible_v<_Tp, _Vp>)
  233. {
  234. std::destroy_at(__oldval);
  235. std::construct_at(__newval, std::forward<_Vp>(__arg));
  236. }
  237. else if constexpr (is_nothrow_move_constructible_v<_Tp>)
  238. {
  239. _Tp __tmp(std::forward<_Vp>(__arg)); // might throw
  240. std::destroy_at(__oldval);
  241. std::construct_at(__newval, std::move(__tmp));
  242. }
  243. else
  244. {
  245. _Guard<_Up> __guard(*__oldval);
  246. std::construct_at(__newval, std::forward<_Vp>(__arg)); // might throw
  247. __guard.release();
  248. }
  249. }
  250. }
  251. /// @endcond
  252. template<typename _Tp, typename _Er>
  253. class expected
  254. {
  255. static_assert( ! is_reference_v<_Tp> );
  256. static_assert( ! is_function_v<_Tp> );
  257. static_assert( ! is_same_v<remove_cv_t<_Tp>, in_place_t> );
  258. static_assert( ! is_same_v<remove_cv_t<_Tp>, unexpect_t> );
  259. static_assert( ! __expected::__is_unexpected<remove_cv_t<_Tp>> );
  260. static_assert( __expected::__can_be_unexpected<_Er> );
  261. template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
  262. static constexpr bool __cons_from_expected
  263. = __or_v<is_constructible<_Tp, expected<_Up, _Err>&>,
  264. is_constructible<_Tp, expected<_Up, _Err>>,
  265. is_constructible<_Tp, const expected<_Up, _Err>&>,
  266. is_constructible<_Tp, const expected<_Up, _Err>>,
  267. is_convertible<expected<_Up, _Err>&, _Tp>,
  268. is_convertible<expected<_Up, _Err>, _Tp>,
  269. is_convertible<const expected<_Up, _Err>&, _Tp>,
  270. is_convertible<const expected<_Up, _Err>, _Tp>,
  271. is_constructible<_Unex, expected<_Up, _Err>&>,
  272. is_constructible<_Unex, expected<_Up, _Err>>,
  273. is_constructible<_Unex, const expected<_Up, _Err>&>,
  274. is_constructible<_Unex, const expected<_Up, _Err>>
  275. >;
  276. template<typename _Up, typename _Err>
  277. constexpr static bool __explicit_conv
  278. = __or_v<__not_<is_convertible<_Up, _Tp>>,
  279. __not_<is_convertible<_Err, _Er>>
  280. >;
  281. public:
  282. using value_type = _Tp;
  283. using error_type = _Er;
  284. using unexpected_type = unexpected<_Er>;
  285. template<typename _Up>
  286. using rebind = expected<_Up, error_type>;
  287. constexpr
  288. expected()
  289. noexcept(is_nothrow_default_constructible_v<_Tp>)
  290. requires is_default_constructible_v<_Tp>
  291. : _M_val(), _M_has_value(true)
  292. { }
  293. expected(const expected&) = default;
  294. constexpr
  295. expected(const expected& __x)
  296. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  297. is_nothrow_copy_constructible<_Er>>)
  298. requires is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Er>
  299. && (!is_trivially_copy_constructible_v<_Tp>
  300. || !is_trivially_copy_constructible_v<_Er>)
  301. : _M_invalid(), _M_has_value(__x._M_has_value)
  302. {
  303. if (_M_has_value)
  304. std::construct_at(__builtin_addressof(_M_val), __x._M_val);
  305. else
  306. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  307. }
  308. expected(expected&&) = default;
  309. constexpr
  310. expected(expected&& __x)
  311. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  312. is_nothrow_move_constructible<_Er>>)
  313. requires is_move_constructible_v<_Tp> && is_move_constructible_v<_Er>
  314. && (!is_trivially_move_constructible_v<_Tp>
  315. || !is_trivially_move_constructible_v<_Er>)
  316. : _M_invalid(), _M_has_value(__x._M_has_value)
  317. {
  318. if (_M_has_value)
  319. std::construct_at(__builtin_addressof(_M_val),
  320. std::move(__x)._M_val);
  321. else
  322. std::construct_at(__builtin_addressof(_M_unex),
  323. std::move(__x)._M_unex);
  324. }
  325. template<typename _Up, typename _Gr>
  326. requires is_constructible_v<_Tp, const _Up&>
  327. && is_constructible_v<_Er, const _Gr&>
  328. && (!__cons_from_expected<_Up, _Gr>)
  329. constexpr explicit(__explicit_conv<const _Up&, const _Gr&>)
  330. expected(const expected<_Up, _Gr>& __x)
  331. noexcept(__and_v<is_nothrow_constructible<_Tp, const _Up&>,
  332. is_nothrow_constructible<_Er, const _Gr&>>)
  333. : _M_invalid(), _M_has_value(__x._M_has_value)
  334. {
  335. if (_M_has_value)
  336. std::construct_at(__builtin_addressof(_M_val), __x._M_val);
  337. else
  338. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  339. }
  340. template<typename _Up, typename _Gr>
  341. requires is_constructible_v<_Tp, _Up>
  342. && is_constructible_v<_Er, _Gr>
  343. && (!__cons_from_expected<_Up, _Gr>)
  344. constexpr explicit(__explicit_conv<_Up, _Gr>)
  345. expected(expected<_Up, _Gr>&& __x)
  346. noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
  347. is_nothrow_constructible<_Er, _Gr>>)
  348. : _M_invalid(), _M_has_value(__x._M_has_value)
  349. {
  350. if (_M_has_value)
  351. std::construct_at(__builtin_addressof(_M_val),
  352. std::move(__x)._M_val);
  353. else
  354. std::construct_at(__builtin_addressof(_M_unex),
  355. std::move(__x)._M_unex);
  356. }
  357. template<typename _Up = _Tp>
  358. requires (!is_same_v<remove_cvref_t<_Up>, expected>)
  359. && (!is_same_v<remove_cvref_t<_Up>, in_place_t>)
  360. && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
  361. && is_constructible_v<_Tp, _Up>
  362. constexpr explicit(!is_convertible_v<_Up, _Tp>)
  363. expected(_Up&& __v)
  364. noexcept(is_nothrow_constructible_v<_Tp, _Up>)
  365. : _M_val(std::forward<_Up>(__v)), _M_has_value(true)
  366. { }
  367. template<typename _Gr = _Er>
  368. requires is_constructible_v<_Er, const _Gr&>
  369. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  370. expected(const unexpected<_Gr>& __u)
  371. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  372. : _M_unex(__u.error()), _M_has_value(false)
  373. { }
  374. template<typename _Gr = _Er>
  375. requires is_constructible_v<_Er, _Gr>
  376. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  377. expected(unexpected<_Gr>&& __u)
  378. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  379. : _M_unex(std::move(__u).error()), _M_has_value(false)
  380. { }
  381. template<typename... _Args>
  382. requires is_constructible_v<_Tp, _Args...>
  383. constexpr explicit
  384. expected(in_place_t, _Args&&... __args)
  385. noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
  386. : _M_val(std::forward<_Args>(__args)...), _M_has_value(true)
  387. { }
  388. template<typename _Up, typename... _Args>
  389. requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
  390. constexpr explicit
  391. expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
  392. noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  393. _Args...>)
  394. : _M_val(__il, std::forward<_Args>(__args)...), _M_has_value(true)
  395. { }
  396. template<typename... _Args>
  397. requires is_constructible_v<_Er, _Args...>
  398. constexpr explicit
  399. expected(unexpect_t, _Args&&... __args)
  400. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  401. : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
  402. { }
  403. template<typename _Up, typename... _Args>
  404. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  405. constexpr explicit
  406. expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
  407. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  408. _Args...>)
  409. : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
  410. { }
  411. constexpr ~expected() = default;
  412. constexpr ~expected()
  413. requires (!is_trivially_destructible_v<_Tp>)
  414. || (!is_trivially_destructible_v<_Er>)
  415. {
  416. if (_M_has_value)
  417. std::destroy_at(__builtin_addressof(_M_val));
  418. else
  419. std::destroy_at(__builtin_addressof(_M_unex));
  420. }
  421. // assignment
  422. expected& operator=(const expected&) = delete;
  423. constexpr expected&
  424. operator=(const expected& __x)
  425. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  426. is_nothrow_copy_constructible<_Er>,
  427. is_nothrow_copy_assignable<_Tp>,
  428. is_nothrow_copy_assignable<_Er>>)
  429. requires is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp>
  430. && is_copy_assignable_v<_Er> && is_copy_constructible_v<_Er>
  431. && (is_nothrow_move_constructible_v<_Tp>
  432. || is_nothrow_move_constructible_v<_Er>)
  433. {
  434. if (__x._M_has_value)
  435. this->_M_assign_val(__x._M_val);
  436. else
  437. this->_M_assign_unex(__x._M_unex);
  438. return *this;
  439. }
  440. constexpr expected&
  441. operator=(expected&& __x)
  442. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  443. is_nothrow_move_constructible<_Er>,
  444. is_nothrow_move_assignable<_Tp>,
  445. is_nothrow_move_assignable<_Er>>)
  446. requires is_move_assignable_v<_Tp> && is_move_constructible_v<_Tp>
  447. && is_move_assignable_v<_Er> && is_move_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. _M_assign_val(std::move(__x._M_val));
  453. else
  454. _M_assign_unex(std::move(__x._M_unex));
  455. return *this;
  456. }
  457. template<typename _Up = _Tp>
  458. requires (!is_same_v<expected, remove_cvref_t<_Up>>)
  459. && (!__expected::__is_unexpected<remove_cvref_t<_Up>>)
  460. && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>
  461. && (is_nothrow_constructible_v<_Tp, _Up>
  462. || is_nothrow_move_constructible_v<_Tp>
  463. || is_nothrow_move_constructible_v<_Er>)
  464. constexpr expected&
  465. operator=(_Up&& __v)
  466. {
  467. _M_assign_val(std::forward<_Up>(__v));
  468. return *this;
  469. }
  470. template<typename _Gr>
  471. requires is_constructible_v<_Er, const _Gr&>
  472. && is_assignable_v<_Er&, const _Gr&>
  473. && (is_nothrow_constructible_v<_Er, const _Gr&>
  474. || is_nothrow_move_constructible_v<_Tp>
  475. || is_nothrow_move_constructible_v<_Er>)
  476. constexpr expected&
  477. operator=(const unexpected<_Gr>& __e)
  478. {
  479. _M_assign_unex(__e.error());
  480. return *this;
  481. }
  482. template<typename _Gr>
  483. requires is_constructible_v<_Er, _Gr>
  484. && is_assignable_v<_Er&, _Gr>
  485. && (is_nothrow_constructible_v<_Er, _Gr>
  486. || is_nothrow_move_constructible_v<_Tp>
  487. || is_nothrow_move_constructible_v<_Er>)
  488. constexpr expected&
  489. operator=(unexpected<_Gr>&& __e)
  490. {
  491. _M_assign_unex(std::move(__e).error());
  492. return *this;
  493. }
  494. // modifiers
  495. template<typename... _Args>
  496. requires is_nothrow_constructible_v<_Tp, _Args...>
  497. constexpr _Tp&
  498. emplace(_Args&&... __args) noexcept
  499. {
  500. if (_M_has_value)
  501. std::destroy_at(__builtin_addressof(_M_val));
  502. else
  503. {
  504. std::destroy_at(__builtin_addressof(_M_unex));
  505. _M_has_value = true;
  506. }
  507. std::construct_at(__builtin_addressof(_M_val),
  508. std::forward<_Args>(__args)...);
  509. return _M_val;
  510. }
  511. template<typename _Up, typename... _Args>
  512. requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
  513. _Args...>
  514. constexpr _Tp&
  515. emplace(initializer_list<_Up> __il, _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. __il, std::forward<_Args>(__args)...);
  526. return _M_val;
  527. }
  528. // swap
  529. constexpr void
  530. swap(expected& __x)
  531. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  532. is_nothrow_move_constructible<_Er>,
  533. is_nothrow_swappable<_Tp&>,
  534. is_nothrow_swappable<_Er&>>)
  535. requires is_swappable_v<_Tp> && is_swappable_v<_Er>
  536. && is_move_constructible_v<_Tp>
  537. && is_move_constructible_v<_Er>
  538. && (is_nothrow_move_constructible_v<_Tp>
  539. || is_nothrow_move_constructible_v<_Er>)
  540. {
  541. if (_M_has_value)
  542. {
  543. if (__x._M_has_value)
  544. {
  545. using std::swap;
  546. swap(_M_val, __x._M_val);
  547. }
  548. else
  549. this->_M_swap_val_unex(__x);
  550. }
  551. else
  552. {
  553. if (__x._M_has_value)
  554. __x._M_swap_val_unex(*this);
  555. else
  556. {
  557. using std::swap;
  558. swap(_M_unex, __x._M_unex);
  559. }
  560. }
  561. }
  562. // observers
  563. [[nodiscard]]
  564. constexpr const _Tp*
  565. operator->() const noexcept
  566. {
  567. __glibcxx_assert(_M_has_value);
  568. return __builtin_addressof(_M_val);
  569. }
  570. [[nodiscard]]
  571. constexpr _Tp*
  572. operator->() noexcept
  573. {
  574. __glibcxx_assert(_M_has_value);
  575. return __builtin_addressof(_M_val);
  576. }
  577. [[nodiscard]]
  578. constexpr const _Tp&
  579. operator*() const & noexcept
  580. {
  581. __glibcxx_assert(_M_has_value);
  582. return _M_val;
  583. }
  584. [[nodiscard]]
  585. constexpr _Tp&
  586. operator*() & noexcept
  587. {
  588. __glibcxx_assert(_M_has_value);
  589. return _M_val;
  590. }
  591. [[nodiscard]]
  592. constexpr const _Tp&&
  593. operator*() const && noexcept
  594. {
  595. __glibcxx_assert(_M_has_value);
  596. return std::move(_M_val);
  597. }
  598. [[nodiscard]]
  599. constexpr _Tp&&
  600. operator*() && noexcept
  601. {
  602. __glibcxx_assert(_M_has_value);
  603. return std::move(_M_val);
  604. }
  605. [[nodiscard]]
  606. constexpr explicit
  607. operator bool() const noexcept { return _M_has_value; }
  608. [[nodiscard]]
  609. constexpr bool has_value() const noexcept { return _M_has_value; }
  610. constexpr const _Tp&
  611. value() const &
  612. {
  613. if (_M_has_value) [[likely]]
  614. return _M_val;
  615. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
  616. }
  617. constexpr _Tp&
  618. value() &
  619. {
  620. if (_M_has_value) [[likely]]
  621. return _M_val;
  622. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
  623. }
  624. constexpr const _Tp&&
  625. value() const &&
  626. {
  627. if (_M_has_value) [[likely]]
  628. return std::move(_M_val);
  629. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
  630. std::move(_M_unex)));
  631. }
  632. constexpr _Tp&&
  633. value() &&
  634. {
  635. if (_M_has_value) [[likely]]
  636. return std::move(_M_val);
  637. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(
  638. std::move(_M_unex)));
  639. }
  640. constexpr const _Er&
  641. error() const & noexcept
  642. {
  643. __glibcxx_assert(!_M_has_value);
  644. return _M_unex;
  645. }
  646. constexpr _Er&
  647. error() & noexcept
  648. {
  649. __glibcxx_assert(!_M_has_value);
  650. return _M_unex;
  651. }
  652. constexpr const _Er&&
  653. error() const && noexcept
  654. {
  655. __glibcxx_assert(!_M_has_value);
  656. return std::move(_M_unex);
  657. }
  658. constexpr _Er&&
  659. error() && noexcept
  660. {
  661. __glibcxx_assert(!_M_has_value);
  662. return std::move(_M_unex);
  663. }
  664. template<typename _Up>
  665. constexpr _Tp
  666. value_or(_Up&& __v) const &
  667. noexcept(__and_v<is_nothrow_copy_constructible<_Tp>,
  668. is_nothrow_convertible<_Up, _Tp>>)
  669. {
  670. static_assert( is_copy_constructible_v<_Tp> );
  671. static_assert( is_convertible_v<_Up, _Tp> );
  672. if (_M_has_value)
  673. return _M_val;
  674. return static_cast<_Tp>(std::forward<_Up>(__v));
  675. }
  676. template<typename _Up>
  677. constexpr _Tp
  678. value_or(_Up&& __v) &&
  679. noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
  680. is_nothrow_convertible<_Up, _Tp>>)
  681. {
  682. static_assert( is_move_constructible_v<_Tp> );
  683. static_assert( is_convertible_v<_Up, _Tp> );
  684. if (_M_has_value)
  685. return std::move(_M_val);
  686. return static_cast<_Tp>(std::forward<_Up>(__v));
  687. }
  688. // equality operators
  689. template<typename _Up, typename _Er2>
  690. requires (!is_void_v<_Up>)
  691. friend constexpr bool
  692. operator==(const expected& __x, const expected<_Up, _Er2>& __y)
  693. noexcept(noexcept(bool(*__x == *__y))
  694. && noexcept(bool(__x.error() == __y.error())))
  695. {
  696. if (__x.has_value())
  697. return __y.has_value() && bool(*__x == *__y);
  698. else
  699. return !__y.has_value() && bool(__x.error() == __y.error());
  700. }
  701. template<typename _Up>
  702. friend constexpr bool
  703. operator==(const expected& __x, const _Up& __v)
  704. noexcept(noexcept(bool(*__x == __v)))
  705. { return __x.has_value() && bool(*__x == __v); }
  706. template<typename _Er2>
  707. friend constexpr bool
  708. operator==(const expected& __x, const unexpected<_Er2>& __e)
  709. noexcept(noexcept(bool(__x.error() == __e.error())))
  710. { return !__x.has_value() && bool(__x.error() == __e.error()); }
  711. friend constexpr void
  712. swap(expected& __x, expected& __y)
  713. noexcept(noexcept(__x.swap(__y)))
  714. requires requires {__x.swap(__y);}
  715. { __x.swap(__y); }
  716. private:
  717. template<typename, typename> friend class expected;
  718. template<typename _Vp>
  719. constexpr void
  720. _M_assign_val(_Vp&& __v)
  721. {
  722. if (_M_has_value)
  723. _M_val = std::forward<_Vp>(__v);
  724. else
  725. {
  726. __expected::__reinit(__builtin_addressof(_M_val),
  727. __builtin_addressof(_M_unex),
  728. std::forward<_Vp>(__v));
  729. _M_has_value = true;
  730. }
  731. }
  732. template<typename _Vp>
  733. constexpr void
  734. _M_assign_unex(_Vp&& __v)
  735. {
  736. if (_M_has_value)
  737. {
  738. __expected::__reinit(__builtin_addressof(_M_unex),
  739. __builtin_addressof(_M_val),
  740. std::forward<_Vp>(__v));
  741. _M_has_value = false;
  742. }
  743. else
  744. _M_unex = std::forward<_Vp>(__v);
  745. }
  746. // Swap two expected objects when only one has a value.
  747. // Precondition: this->_M_has_value && !__rhs._M_has_value
  748. constexpr void
  749. _M_swap_val_unex(expected& __rhs)
  750. noexcept(__and_v<is_nothrow_move_constructible<_Er>,
  751. is_nothrow_move_constructible<_Tp>>)
  752. {
  753. if constexpr (is_nothrow_move_constructible_v<_Er>)
  754. {
  755. __expected::_Guard<_Er> __guard(__rhs._M_unex);
  756. std::construct_at(__builtin_addressof(__rhs._M_val),
  757. std::move(_M_val)); // might throw
  758. __rhs._M_has_value = true;
  759. std::destroy_at(__builtin_addressof(_M_val));
  760. std::construct_at(__builtin_addressof(_M_unex),
  761. __guard.release());
  762. _M_has_value = false;
  763. }
  764. else
  765. {
  766. __expected::_Guard<_Tp> __guard(__rhs._M_val);
  767. std::construct_at(__builtin_addressof(_M_unex),
  768. std::move(__rhs._M_unex)); // might throw
  769. _M_has_value = false;
  770. std::destroy_at(__builtin_addressof(__rhs._M_unex));
  771. std::construct_at(__builtin_addressof(__rhs._M_val),
  772. __guard.release());
  773. __rhs._M_has_value = true;
  774. }
  775. }
  776. union {
  777. struct { } _M_invalid;
  778. _Tp _M_val;
  779. _Er _M_unex;
  780. };
  781. bool _M_has_value;
  782. };
  783. // Partial specialization for std::expected<cv void, E>
  784. template<typename _Tp, typename _Er> requires is_void_v<_Tp>
  785. class expected<_Tp, _Er>
  786. {
  787. static_assert( __expected::__can_be_unexpected<_Er> );
  788. template<typename _Up, typename _Err, typename _Unex = unexpected<_Er>>
  789. static constexpr bool __cons_from_expected
  790. = __or_v<is_constructible<_Unex, expected<_Up, _Err>&>,
  791. is_constructible<_Unex, expected<_Up, _Err>>,
  792. is_constructible<_Unex, const expected<_Up, _Err>&>,
  793. is_constructible<_Unex, const expected<_Up, _Err>>
  794. >;
  795. public:
  796. using value_type = _Tp;
  797. using error_type = _Er;
  798. using unexpected_type = unexpected<_Er>;
  799. template<typename _Up>
  800. using rebind = expected<_Up, error_type>;
  801. constexpr
  802. expected() noexcept
  803. : _M_void(), _M_has_value(true)
  804. { }
  805. expected(const expected&) = default;
  806. constexpr
  807. expected(const expected& __x)
  808. noexcept(is_nothrow_copy_constructible_v<_Er>)
  809. requires is_copy_constructible_v<_Er>
  810. && (!is_trivially_copy_constructible_v<_Er>)
  811. : _M_void(), _M_has_value(__x._M_has_value)
  812. {
  813. if (!_M_has_value)
  814. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  815. }
  816. expected(expected&&) = default;
  817. constexpr
  818. expected(expected&& __x)
  819. noexcept(is_nothrow_move_constructible_v<_Er>)
  820. requires is_move_constructible_v<_Er>
  821. && (!is_trivially_move_constructible_v<_Er>)
  822. : _M_void(), _M_has_value(__x._M_has_value)
  823. {
  824. if (!_M_has_value)
  825. std::construct_at(__builtin_addressof(_M_unex),
  826. std::move(__x)._M_unex);
  827. }
  828. template<typename _Up, typename _Gr>
  829. requires is_void_v<_Up>
  830. && is_constructible_v<_Er, const _Gr&>
  831. && (!__cons_from_expected<_Up, _Gr>)
  832. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  833. expected(const expected<_Up, _Gr>& __x)
  834. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  835. : _M_void(), _M_has_value(__x._M_has_value)
  836. {
  837. if (!_M_has_value)
  838. std::construct_at(__builtin_addressof(_M_unex), __x._M_unex);
  839. }
  840. template<typename _Up, typename _Gr>
  841. requires is_void_v<_Up>
  842. && is_constructible_v<_Er, _Gr>
  843. && (!__cons_from_expected<_Up, _Gr>)
  844. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  845. expected(expected<_Up, _Gr>&& __x)
  846. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  847. : _M_void(), _M_has_value(__x._M_has_value)
  848. {
  849. if (!_M_has_value)
  850. std::construct_at(__builtin_addressof(_M_unex),
  851. std::move(__x)._M_unex);
  852. }
  853. template<typename _Gr = _Er>
  854. requires is_constructible_v<_Er, const _Gr&>
  855. constexpr explicit(!is_convertible_v<const _Gr&, _Er>)
  856. expected(const unexpected<_Gr>& __u)
  857. noexcept(is_nothrow_constructible_v<_Er, const _Gr&>)
  858. : _M_unex(__u.error()), _M_has_value(false)
  859. { }
  860. template<typename _Gr = _Er>
  861. requires is_constructible_v<_Er, _Gr>
  862. constexpr explicit(!is_convertible_v<_Gr, _Er>)
  863. expected(unexpected<_Gr>&& __u)
  864. noexcept(is_nothrow_constructible_v<_Er, _Gr>)
  865. : _M_unex(std::move(__u).error()), _M_has_value(false)
  866. { }
  867. template<typename... _Args>
  868. constexpr explicit
  869. expected(in_place_t) noexcept
  870. : expected()
  871. { }
  872. template<typename... _Args>
  873. requires is_constructible_v<_Er, _Args...>
  874. constexpr explicit
  875. expected(unexpect_t, _Args&&... __args)
  876. noexcept(is_nothrow_constructible_v<_Er, _Args...>)
  877. : _M_unex(std::forward<_Args>(__args)...), _M_has_value(false)
  878. { }
  879. template<typename _Up, typename... _Args>
  880. requires is_constructible_v<_Er, initializer_list<_Up>&, _Args...>
  881. constexpr explicit
  882. expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args)
  883. noexcept(is_nothrow_constructible_v<_Er, initializer_list<_Up>&,
  884. _Args...>)
  885. : _M_unex(__il, std::forward<_Args>(__args)...), _M_has_value(false)
  886. { }
  887. constexpr ~expected() = default;
  888. constexpr ~expected() requires (!is_trivially_destructible_v<_Er>)
  889. {
  890. if (!_M_has_value)
  891. std::destroy_at(__builtin_addressof(_M_unex));
  892. }
  893. // assignment
  894. expected& operator=(const expected&) = delete;
  895. constexpr expected&
  896. operator=(const expected& __x)
  897. noexcept(__and_v<is_nothrow_copy_constructible<_Er>,
  898. is_nothrow_copy_assignable<_Er>>)
  899. requires is_copy_constructible_v<_Er>
  900. && is_copy_assignable_v<_Er>
  901. {
  902. if (__x._M_has_value)
  903. emplace();
  904. else
  905. _M_assign_unex(__x._M_unex);
  906. return *this;
  907. }
  908. constexpr expected&
  909. operator=(expected&& __x)
  910. noexcept(__and_v<is_nothrow_move_constructible<_Er>,
  911. is_nothrow_move_assignable<_Er>>)
  912. requires is_move_constructible_v<_Er>
  913. && is_move_assignable_v<_Er>
  914. {
  915. if (__x._M_has_value)
  916. emplace();
  917. else
  918. _M_assign_unex(std::move(__x._M_unex));
  919. return *this;
  920. }
  921. template<typename _Gr>
  922. requires is_constructible_v<_Er, const _Gr&>
  923. && is_assignable_v<_Er&, const _Gr&>
  924. constexpr expected&
  925. operator=(const unexpected<_Gr>& __e)
  926. {
  927. _M_assign_unex(__e.error());
  928. return *this;
  929. }
  930. template<typename _Gr>
  931. requires is_constructible_v<_Er, _Gr>
  932. && is_assignable_v<_Er&, _Gr>
  933. constexpr expected&
  934. operator=(unexpected<_Gr>&& __e)
  935. {
  936. _M_assign_unex(std::move(__e.error()));
  937. return *this;
  938. }
  939. // modifiers
  940. constexpr void
  941. emplace() noexcept
  942. {
  943. if (!_M_has_value)
  944. {
  945. std::destroy_at(__builtin_addressof(_M_unex));
  946. _M_has_value = true;
  947. }
  948. }
  949. // swap
  950. constexpr void
  951. swap(expected& __x)
  952. noexcept(__and_v<is_nothrow_swappable<_Er&>,
  953. is_nothrow_move_constructible<_Er>>)
  954. requires is_swappable_v<_Er> && is_move_constructible_v<_Er>
  955. {
  956. if (_M_has_value)
  957. {
  958. if (!__x._M_has_value)
  959. {
  960. std::construct_at(__builtin_addressof(_M_unex),
  961. std::move(__x._M_unex)); // might throw
  962. std::destroy_at(__builtin_addressof(__x._M_unex));
  963. _M_has_value = false;
  964. __x._M_has_value = true;
  965. }
  966. }
  967. else
  968. {
  969. if (__x._M_has_value)
  970. {
  971. std::construct_at(__builtin_addressof(__x._M_unex),
  972. std::move(_M_unex)); // might throw
  973. std::destroy_at(__builtin_addressof(_M_unex));
  974. _M_has_value = true;
  975. __x._M_has_value = false;
  976. }
  977. else
  978. {
  979. using std::swap;
  980. swap(_M_unex, __x._M_unex);
  981. }
  982. }
  983. }
  984. // observers
  985. [[nodiscard]]
  986. constexpr explicit
  987. operator bool() const noexcept { return _M_has_value; }
  988. [[nodiscard]]
  989. constexpr bool has_value() const noexcept { return _M_has_value; }
  990. constexpr void
  991. operator*() const noexcept { __glibcxx_assert(_M_has_value); }
  992. constexpr void
  993. value() const&
  994. {
  995. if (_M_has_value) [[likely]]
  996. return;
  997. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(_M_unex));
  998. }
  999. constexpr void
  1000. value() &&
  1001. {
  1002. if (_M_has_value) [[likely]]
  1003. return;
  1004. _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(std::move(_M_unex)));
  1005. }
  1006. constexpr const _Er&
  1007. error() const & noexcept
  1008. {
  1009. __glibcxx_assert(!_M_has_value);
  1010. return _M_unex;
  1011. }
  1012. constexpr _Er&
  1013. error() & noexcept
  1014. {
  1015. __glibcxx_assert(!_M_has_value);
  1016. return _M_unex;
  1017. }
  1018. constexpr const _Er&&
  1019. error() const && noexcept
  1020. {
  1021. __glibcxx_assert(!_M_has_value);
  1022. return std::move(_M_unex);
  1023. }
  1024. constexpr _Er&&
  1025. error() && noexcept
  1026. {
  1027. __glibcxx_assert(!_M_has_value);
  1028. return std::move(_M_unex);
  1029. }
  1030. // equality operators
  1031. template<typename _Up, typename _Er2>
  1032. requires is_void_v<_Up>
  1033. friend constexpr bool
  1034. operator==(const expected& __x, const expected<_Up, _Er2>& __y)
  1035. noexcept(noexcept(bool(__x.error() == __y.error())))
  1036. {
  1037. if (__x.has_value())
  1038. return __y.has_value();
  1039. else
  1040. return !__y.has_value() && bool(__x.error() == __y.error());
  1041. }
  1042. template<typename _Er2>
  1043. friend constexpr bool
  1044. operator==(const expected& __x, const unexpected<_Er2>& __e)
  1045. noexcept(noexcept(bool(__x.error() == __e.error())))
  1046. { return !__x.has_value() && bool(__x.error() == __e.error()); }
  1047. friend constexpr void
  1048. swap(expected& __x, expected& __y)
  1049. noexcept(noexcept(__x.swap(__y)))
  1050. requires requires { __x.swap(__y); }
  1051. { __x.swap(__y); }
  1052. private:
  1053. template<typename, typename> friend class expected;
  1054. template<typename _Vp>
  1055. constexpr void
  1056. _M_assign_unex(_Vp&& __v)
  1057. {
  1058. if (_M_has_value)
  1059. {
  1060. std::construct_at(__builtin_addressof(_M_unex),
  1061. std::forward<_Vp>(__v));
  1062. _M_has_value = false;
  1063. }
  1064. else
  1065. _M_unex = std::forward<_Vp>(__v);
  1066. }
  1067. union {
  1068. struct { } _M_void;
  1069. _Er _M_unex;
  1070. };
  1071. bool _M_has_value;
  1072. };
  1073. /// @}
  1074. _GLIBCXX_END_NAMESPACE_VERSION
  1075. } // namespace std
  1076. #endif // C++23
  1077. #endif // _GLIBCXX_EXPECTED