expected.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // This is based on the proposed implementation of std::expected (P0323)
  4. // https://github.com/TartanLlama/expected/blob/master/include/tl/expected.hpp
  5. #pragma once
  6. #include <type_traits>
  7. #include <utility>
  8. namespace Common {
  9. template <typename T, typename E>
  10. class Expected;
  11. template <typename E>
  12. class Unexpected {
  13. public:
  14. Unexpected() = delete;
  15. constexpr explicit Unexpected(const E& e) : m_val{e} {}
  16. constexpr explicit Unexpected(E&& e) : m_val{std::move(e)} {}
  17. constexpr E& value() & {
  18. return m_val;
  19. }
  20. constexpr const E& value() const& {
  21. return m_val;
  22. }
  23. constexpr E&& value() && {
  24. return std::move(m_val);
  25. }
  26. constexpr const E&& value() const&& {
  27. return std::move(m_val);
  28. }
  29. private:
  30. E m_val;
  31. };
  32. template <typename E>
  33. constexpr auto operator<=>(const Unexpected<E>& lhs, const Unexpected<E>& rhs) {
  34. return lhs.value() <=> rhs.value();
  35. }
  36. struct unexpect_t {
  37. constexpr explicit unexpect_t() = default;
  38. };
  39. namespace detail {
  40. struct no_init_t {
  41. constexpr explicit no_init_t() = default;
  42. };
  43. /**
  44. * This specialization is for when T is not trivially destructible,
  45. * so the destructor must be called on destruction of `expected'
  46. * Additionally, this requires E to be trivially destructible
  47. */
  48. template <typename T, typename E, bool = std::is_trivially_destructible_v<T>>
  49. requires std::is_trivially_destructible_v<E>
  50. struct expected_storage_base {
  51. constexpr expected_storage_base() : m_val{T{}}, m_has_val{true} {}
  52. constexpr expected_storage_base(no_init_t) : m_has_val{false} {}
  53. template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
  54. constexpr expected_storage_base(std::in_place_t, Args&&... args)
  55. : m_val{std::forward<Args>(args)...}, m_has_val{true} {}
  56. template <typename U, typename... Args,
  57. std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
  58. nullptr>
  59. constexpr expected_storage_base(std::in_place_t, std::initializer_list<U> il, Args&&... args)
  60. : m_val{il, std::forward<Args>(args)...}, m_has_val{true} {}
  61. template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
  62. constexpr explicit expected_storage_base(unexpect_t, Args&&... args)
  63. : m_unexpect{std::forward<Args>(args)...}, m_has_val{false} {}
  64. template <typename U, typename... Args,
  65. std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
  66. nullptr>
  67. constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il,
  68. Args&&... args)
  69. : m_unexpect{il, std::forward<Args>(args)...}, m_has_val{false} {}
  70. ~expected_storage_base() {
  71. if (m_has_val) {
  72. m_val.~T();
  73. }
  74. }
  75. union {
  76. T m_val;
  77. Unexpected<E> m_unexpect;
  78. };
  79. bool m_has_val;
  80. };
  81. /**
  82. * This specialization is for when T is trivially destructible,
  83. * so the destructor of `expected` can be trivial
  84. * Additionally, this requires E to be trivially destructible
  85. */
  86. template <typename T, typename E>
  87. requires std::is_trivially_destructible_v<E>
  88. struct expected_storage_base<T, E, true> {
  89. constexpr expected_storage_base() : m_val{T{}}, m_has_val{true} {}
  90. constexpr expected_storage_base(no_init_t) : m_has_val{false} {}
  91. template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
  92. constexpr expected_storage_base(std::in_place_t, Args&&... args)
  93. : m_val{std::forward<Args>(args)...}, m_has_val{true} {}
  94. template <typename U, typename... Args,
  95. std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
  96. nullptr>
  97. constexpr expected_storage_base(std::in_place_t, std::initializer_list<U> il, Args&&... args)
  98. : m_val{il, std::forward<Args>(args)...}, m_has_val{true} {}
  99. template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
  100. constexpr explicit expected_storage_base(unexpect_t, Args&&... args)
  101. : m_unexpect{std::forward<Args>(args)...}, m_has_val{false} {}
  102. template <typename U, typename... Args,
  103. std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
  104. nullptr>
  105. constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il,
  106. Args&&... args)
  107. : m_unexpect{il, std::forward<Args>(args)...}, m_has_val{false} {}
  108. ~expected_storage_base() = default;
  109. union {
  110. T m_val;
  111. Unexpected<E> m_unexpect;
  112. };
  113. bool m_has_val;
  114. };
  115. template <typename T, typename E>
  116. struct expected_operations_base : expected_storage_base<T, E> {
  117. using expected_storage_base<T, E>::expected_storage_base;
  118. template <typename... Args>
  119. void construct(Args&&... args) noexcept {
  120. new (std::addressof(this->m_val)) T{std::forward<Args>(args)...};
  121. this->m_has_val = true;
  122. }
  123. template <typename Rhs>
  124. void construct_with(Rhs&& rhs) noexcept {
  125. new (std::addressof(this->m_val)) T{std::forward<Rhs>(rhs).get()};
  126. this->m_has_val = true;
  127. }
  128. template <typename... Args>
  129. void construct_error(Args&&... args) noexcept {
  130. new (std::addressof(this->m_unexpect)) Unexpected<E>{std::forward<Args>(args)...};
  131. this->m_has_val = false;
  132. }
  133. void assign(const expected_operations_base& rhs) noexcept {
  134. if (!this->m_has_val && rhs.m_has_val) {
  135. geterr().~Unexpected<E>();
  136. construct(rhs.get());
  137. } else {
  138. assign_common(rhs);
  139. }
  140. }
  141. void assign(expected_operations_base&& rhs) noexcept {
  142. if (!this->m_has_val && rhs.m_has_val) {
  143. geterr().~Unexpected<E>();
  144. construct(std::move(rhs).get());
  145. } else {
  146. assign_common(rhs);
  147. }
  148. }
  149. template <typename Rhs>
  150. void assign_common(Rhs&& rhs) {
  151. if (this->m_has_val) {
  152. if (rhs.m_has_val) {
  153. get() = std::forward<Rhs>(rhs).get();
  154. } else {
  155. destroy_val();
  156. construct_error(std::forward<Rhs>(rhs).geterr());
  157. }
  158. } else {
  159. if (!rhs.m_has_val) {
  160. geterr() = std::forward<Rhs>(rhs).geterr();
  161. }
  162. }
  163. }
  164. bool has_value() const {
  165. return this->m_has_val;
  166. }
  167. constexpr T& get() & {
  168. return this->m_val;
  169. }
  170. constexpr const T& get() const& {
  171. return this->m_val;
  172. }
  173. constexpr T&& get() && {
  174. return std::move(this->m_val);
  175. }
  176. constexpr const T&& get() const&& {
  177. return std::move(this->m_val);
  178. }
  179. constexpr Unexpected<E>& geterr() & {
  180. return this->m_unexpect;
  181. }
  182. constexpr const Unexpected<E>& geterr() const& {
  183. return this->m_unexpect;
  184. }
  185. constexpr Unexpected<E>&& geterr() && {
  186. return std::move(this->m_unexpect);
  187. }
  188. constexpr const Unexpected<E>&& geterr() const&& {
  189. return std::move(this->m_unexpect);
  190. }
  191. constexpr void destroy_val() {
  192. get().~T();
  193. }
  194. };
  195. /**
  196. * This manages conditionally having a trivial copy constructor
  197. * This specialization is for when T is trivially copy constructible
  198. * Additionally, this requires E to be trivially copy constructible
  199. */
  200. template <typename T, typename E, bool = std::is_trivially_copy_constructible_v<T>>
  201. requires std::is_trivially_copy_constructible_v<E>
  202. struct expected_copy_base : expected_operations_base<T, E> {
  203. using expected_operations_base<T, E>::expected_operations_base;
  204. };
  205. /**
  206. * This specialization is for when T is not trivially copy constructible
  207. * Additionally, this requires E to be trivially copy constructible
  208. */
  209. template <typename T, typename E>
  210. requires std::is_trivially_copy_constructible_v<E>
  211. struct expected_copy_base<T, E, false> : expected_operations_base<T, E> {
  212. using expected_operations_base<T, E>::expected_operations_base;
  213. expected_copy_base() = default;
  214. expected_copy_base(const expected_copy_base& rhs)
  215. : expected_operations_base<T, E>{no_init_t{}} {
  216. if (rhs.has_value()) {
  217. this->construct_with(rhs);
  218. } else {
  219. this->construct_error(rhs.geterr());
  220. }
  221. }
  222. expected_copy_base(expected_copy_base&&) = default;
  223. expected_copy_base& operator=(const expected_copy_base&) = default;
  224. expected_copy_base& operator=(expected_copy_base&&) = default;
  225. };
  226. /**
  227. * This manages conditionally having a trivial move constructor
  228. * This specialization is for when T is trivially move constructible
  229. * Additionally, this requires E to be trivially move constructible
  230. */
  231. template <typename T, typename E, bool = std::is_trivially_move_constructible_v<T>>
  232. requires std::is_trivially_move_constructible_v<E>
  233. struct expected_move_base : expected_copy_base<T, E> {
  234. using expected_copy_base<T, E>::expected_copy_base;
  235. };
  236. /**
  237. * This specialization is for when T is not trivially move constructible
  238. * Additionally, this requires E to be trivially move constructible
  239. */
  240. template <typename T, typename E>
  241. requires std::is_trivially_move_constructible_v<E>
  242. struct expected_move_base<T, E, false> : expected_copy_base<T, E> {
  243. using expected_copy_base<T, E>::expected_copy_base;
  244. expected_move_base() = default;
  245. expected_move_base(const expected_move_base&) = default;
  246. expected_move_base(expected_move_base&& rhs) noexcept(std::is_nothrow_move_constructible_v<T>)
  247. : expected_copy_base<T, E>{no_init_t{}} {
  248. if (rhs.has_value()) {
  249. this->construct_with(std::move(rhs));
  250. } else {
  251. this->construct_error(std::move(rhs.geterr()));
  252. }
  253. }
  254. expected_move_base& operator=(const expected_move_base&) = default;
  255. expected_move_base& operator=(expected_move_base&&) = default;
  256. };
  257. /**
  258. * This manages conditionally having a trivial copy assignment operator
  259. * This specialization is for when T is trivially copy assignable
  260. * Additionally, this requires E to be trivially copy assignable
  261. */
  262. template <typename T, typename E,
  263. bool = std::conjunction_v<std::is_trivially_copy_assignable<T>,
  264. std::is_trivially_copy_constructible<T>,
  265. std::is_trivially_destructible<T>>>
  266. requires std::conjunction_v<std::is_trivially_copy_assignable<E>,
  267. std::is_trivially_copy_constructible<E>,
  268. std::is_trivially_destructible<E>>
  269. struct expected_copy_assign_base : expected_move_base<T, E> {
  270. using expected_move_base<T, E>::expected_move_base;
  271. };
  272. /**
  273. * This specialization is for when T is not trivially copy assignable
  274. * Additionally, this requires E to be trivially copy assignable
  275. */
  276. template <typename T, typename E>
  277. requires std::conjunction_v<std::is_trivially_copy_assignable<E>,
  278. std::is_trivially_copy_constructible<E>,
  279. std::is_trivially_destructible<E>>
  280. struct expected_copy_assign_base<T, E, false> : expected_move_base<T, E> {
  281. using expected_move_base<T, E>::expected_move_base;
  282. expected_copy_assign_base() = default;
  283. expected_copy_assign_base(const expected_copy_assign_base&) = default;
  284. expected_copy_assign_base(expected_copy_assign_base&&) = default;
  285. expected_copy_assign_base& operator=(const expected_copy_assign_base& rhs) {
  286. this->assign(rhs);
  287. return *this;
  288. }
  289. expected_copy_assign_base& operator=(expected_copy_assign_base&&) = default;
  290. };
  291. /**
  292. * This manages conditionally having a trivial move assignment operator
  293. * This specialization is for when T is trivially move assignable
  294. * Additionally, this requires E to be trivially move assignable
  295. */
  296. template <typename T, typename E,
  297. bool = std::conjunction_v<std::is_trivially_move_assignable<T>,
  298. std::is_trivially_move_constructible<T>,
  299. std::is_trivially_destructible<T>>>
  300. requires std::conjunction_v<std::is_trivially_move_assignable<E>,
  301. std::is_trivially_move_constructible<E>,
  302. std::is_trivially_destructible<E>>
  303. struct expected_move_assign_base : expected_copy_assign_base<T, E> {
  304. using expected_copy_assign_base<T, E>::expected_copy_assign_base;
  305. };
  306. /**
  307. * This specialization is for when T is not trivially move assignable
  308. * Additionally, this requires E to be trivially move assignable
  309. */
  310. template <typename T, typename E>
  311. requires std::conjunction_v<std::is_trivially_move_assignable<E>,
  312. std::is_trivially_move_constructible<E>,
  313. std::is_trivially_destructible<E>>
  314. struct expected_move_assign_base<T, E, false> : expected_copy_assign_base<T, E> {
  315. using expected_copy_assign_base<T, E>::expected_copy_assign_base;
  316. expected_move_assign_base() = default;
  317. expected_move_assign_base(const expected_move_assign_base&) = default;
  318. expected_move_assign_base(expected_move_assign_base&&) = default;
  319. expected_move_assign_base& operator=(const expected_move_assign_base&) = default;
  320. expected_move_assign_base& operator=(expected_move_assign_base&& rhs) noexcept(
  321. std::conjunction_v<std::is_nothrow_move_constructible<T>,
  322. std::is_nothrow_move_assignable<T>>) {
  323. this->assign(std::move(rhs));
  324. return *this;
  325. }
  326. };
  327. /**
  328. * expected_delete_ctor_base will conditionally delete copy and move constructors
  329. * depending on whether T is copy/move constructible
  330. * Additionally, this requires E to be copy/move constructible
  331. */
  332. template <typename T, typename E, bool EnableCopy = std::is_copy_constructible_v<T>,
  333. bool EnableMove = std::is_move_constructible_v<T>>
  334. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
  335. struct expected_delete_ctor_base {
  336. expected_delete_ctor_base() = default;
  337. expected_delete_ctor_base(const expected_delete_ctor_base&) = default;
  338. expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = default;
  339. expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default;
  340. expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default;
  341. };
  342. template <typename T, typename E>
  343. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
  344. struct expected_delete_ctor_base<T, E, true, false> {
  345. expected_delete_ctor_base() = default;
  346. expected_delete_ctor_base(const expected_delete_ctor_base&) = default;
  347. expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = delete;
  348. expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default;
  349. expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default;
  350. };
  351. template <typename T, typename E>
  352. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
  353. struct expected_delete_ctor_base<T, E, false, true> {
  354. expected_delete_ctor_base() = default;
  355. expected_delete_ctor_base(const expected_delete_ctor_base&) = delete;
  356. expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = default;
  357. expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default;
  358. expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default;
  359. };
  360. template <typename T, typename E>
  361. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
  362. struct expected_delete_ctor_base<T, E, false, false> {
  363. expected_delete_ctor_base() = default;
  364. expected_delete_ctor_base(const expected_delete_ctor_base&) = delete;
  365. expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = delete;
  366. expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default;
  367. expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default;
  368. };
  369. /**
  370. * expected_delete_assign_base will conditionally delete copy and move assignment operators
  371. * depending on whether T is copy/move constructible + assignable
  372. * Additionally, this requires E to be copy/move constructible + assignable
  373. */
  374. template <
  375. typename T, typename E,
  376. bool EnableCopy = std::conjunction_v<std::is_copy_constructible<T>, std::is_copy_assignable<T>>,
  377. bool EnableMove = std::conjunction_v<std::is_move_constructible<T>, std::is_move_assignable<T>>>
  378. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
  379. std::is_copy_assignable<E>, std::is_move_assignable<E>>
  380. struct expected_delete_assign_base {
  381. expected_delete_assign_base() = default;
  382. expected_delete_assign_base(const expected_delete_assign_base&) = default;
  383. expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default;
  384. expected_delete_assign_base& operator=(const expected_delete_assign_base&) = default;
  385. expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = default;
  386. };
  387. template <typename T, typename E>
  388. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
  389. std::is_copy_assignable<E>, std::is_move_assignable<E>>
  390. struct expected_delete_assign_base<T, E, true, false> {
  391. expected_delete_assign_base() = default;
  392. expected_delete_assign_base(const expected_delete_assign_base&) = default;
  393. expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default;
  394. expected_delete_assign_base& operator=(const expected_delete_assign_base&) = default;
  395. expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = delete;
  396. };
  397. template <typename T, typename E>
  398. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
  399. std::is_copy_assignable<E>, std::is_move_assignable<E>>
  400. struct expected_delete_assign_base<T, E, false, true> {
  401. expected_delete_assign_base() = default;
  402. expected_delete_assign_base(const expected_delete_assign_base&) = default;
  403. expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default;
  404. expected_delete_assign_base& operator=(const expected_delete_assign_base&) = delete;
  405. expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = default;
  406. };
  407. template <typename T, typename E>
  408. requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
  409. std::is_copy_assignable<E>, std::is_move_assignable<E>>
  410. struct expected_delete_assign_base<T, E, false, false> {
  411. expected_delete_assign_base() = default;
  412. expected_delete_assign_base(const expected_delete_assign_base&) = default;
  413. expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default;
  414. expected_delete_assign_base& operator=(const expected_delete_assign_base&) = delete;
  415. expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = delete;
  416. };
  417. /**
  418. * This is needed to be able to construct the expected_default_ctor_base which follows,
  419. * while still conditionally deleting the default constructor.
  420. */
  421. struct default_constructor_tag {
  422. constexpr explicit default_constructor_tag() = default;
  423. };
  424. /**
  425. * expected_default_ctor_base will ensure that expected
  426. * has a deleted default constructor if T is not default constructible
  427. * This specialization is for when T is default constructible
  428. */
  429. template <typename T, typename E, bool Enable = std::is_default_constructible_v<T>>
  430. struct expected_default_ctor_base {
  431. constexpr expected_default_ctor_base() noexcept = default;
  432. constexpr expected_default_ctor_base(expected_default_ctor_base const&) noexcept = default;
  433. constexpr expected_default_ctor_base(expected_default_ctor_base&&) noexcept = default;
  434. expected_default_ctor_base& operator=(expected_default_ctor_base const&) noexcept = default;
  435. expected_default_ctor_base& operator=(expected_default_ctor_base&&) noexcept = default;
  436. constexpr explicit expected_default_ctor_base(default_constructor_tag) {}
  437. };
  438. template <typename T, typename E>
  439. struct expected_default_ctor_base<T, E, false> {
  440. constexpr expected_default_ctor_base() noexcept = delete;
  441. constexpr expected_default_ctor_base(expected_default_ctor_base const&) noexcept = default;
  442. constexpr expected_default_ctor_base(expected_default_ctor_base&&) noexcept = default;
  443. expected_default_ctor_base& operator=(expected_default_ctor_base const&) noexcept = default;
  444. expected_default_ctor_base& operator=(expected_default_ctor_base&&) noexcept = default;
  445. constexpr explicit expected_default_ctor_base(default_constructor_tag) {}
  446. };
  447. template <typename T, typename E, typename U>
  448. using expected_enable_forward_value =
  449. std::enable_if_t<std::is_constructible_v<T, U&&> &&
  450. !std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> &&
  451. !std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
  452. !std::is_same_v<Unexpected<E>, std::remove_cvref_t<U>>>;
  453. template <typename T, typename E, typename U, typename G, typename UR, typename GR>
  454. using expected_enable_from_other = std::enable_if_t<
  455. std::is_constructible_v<T, UR> && std::is_constructible_v<E, GR> &&
  456. !std::is_constructible_v<T, Expected<U, G>&> && !std::is_constructible_v<T, Expected<U, G>&&> &&
  457. !std::is_constructible_v<T, const Expected<U, G>&> &&
  458. !std::is_constructible_v<T, const Expected<U, G>&&> &&
  459. !std::is_convertible_v<Expected<U, G>&, T> && !std::is_convertible_v<Expected<U, G>&&, T> &&
  460. !std::is_convertible_v<const Expected<U, G>&, T> &&
  461. !std::is_convertible_v<const Expected<U, G>&&, T>>;
  462. } // namespace detail
  463. template <typename T, typename E>
  464. class Expected : private detail::expected_move_assign_base<T, E>,
  465. private detail::expected_delete_ctor_base<T, E>,
  466. private detail::expected_delete_assign_base<T, E>,
  467. private detail::expected_default_ctor_base<T, E> {
  468. public:
  469. using value_type = T;
  470. using error_type = E;
  471. using unexpected_type = Unexpected<E>;
  472. constexpr Expected() = default;
  473. constexpr Expected(const Expected&) = default;
  474. constexpr Expected(Expected&&) = default;
  475. Expected& operator=(const Expected&) = default;
  476. Expected& operator=(Expected&&) = default;
  477. template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
  478. constexpr Expected(std::in_place_t, Args&&... args)
  479. : impl_base{std::in_place, std::forward<Args>(args)...},
  480. ctor_base{detail::default_constructor_tag{}} {}
  481. template <typename U, typename... Args,
  482. std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
  483. nullptr>
  484. constexpr Expected(std::in_place_t, std::initializer_list<U> il, Args&&... args)
  485. : impl_base{std::in_place, il, std::forward<Args>(args)...},
  486. ctor_base{detail::default_constructor_tag{}} {}
  487. template <typename G = E, std::enable_if_t<std::is_constructible_v<E, const G&>>* = nullptr,
  488. std::enable_if_t<!std::is_convertible_v<const G&, E>>* = nullptr>
  489. constexpr explicit Expected(const Unexpected<G>& e)
  490. : impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {}
  491. template <typename G = E, std::enable_if_t<std::is_constructible_v<E, const G&>>* = nullptr,
  492. std::enable_if_t<std::is_convertible_v<const G&, E>>* = nullptr>
  493. constexpr Expected(Unexpected<G> const& e)
  494. : impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {}
  495. template <typename G = E, std::enable_if_t<std::is_constructible_v<E, G&&>>* = nullptr,
  496. std::enable_if_t<!std::is_convertible_v<G&&, E>>* = nullptr>
  497. constexpr explicit Expected(Unexpected<G>&& e) noexcept(std::is_nothrow_constructible_v<E, G&&>)
  498. : impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{
  499. detail::default_constructor_tag{}} {}
  500. template <typename G = E, std::enable_if_t<std::is_constructible_v<E, G&&>>* = nullptr,
  501. std::enable_if_t<std::is_convertible_v<G&&, E>>* = nullptr>
  502. constexpr Expected(Unexpected<G>&& e) noexcept(std::is_nothrow_constructible_v<E, G&&>)
  503. : impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{
  504. detail::default_constructor_tag{}} {}
  505. template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
  506. constexpr explicit Expected(unexpect_t, Args&&... args)
  507. : impl_base{unexpect_t{}, std::forward<Args>(args)...},
  508. ctor_base{detail::default_constructor_tag{}} {}
  509. template <typename U, typename... Args,
  510. std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
  511. nullptr>
  512. constexpr explicit Expected(unexpect_t, std::initializer_list<U> il, Args&&... args)
  513. : impl_base{unexpect_t{}, il, std::forward<Args>(args)...},
  514. ctor_base{detail::default_constructor_tag{}} {}
  515. template <typename U, typename G,
  516. std::enable_if_t<!(std::is_convertible_v<U const&, T> &&
  517. std::is_convertible_v<G const&, E>)>* = nullptr,
  518. detail::expected_enable_from_other<T, E, U, G, const U&, const G&>* = nullptr>
  519. constexpr explicit Expected(const Expected<U, G>& rhs)
  520. : ctor_base{detail::default_constructor_tag{}} {
  521. if (rhs.has_value()) {
  522. this->construct(*rhs);
  523. } else {
  524. this->construct_error(rhs.error());
  525. }
  526. }
  527. template <typename U, typename G,
  528. std::enable_if_t<(std::is_convertible_v<U const&, T> &&
  529. std::is_convertible_v<G const&, E>)>* = nullptr,
  530. detail::expected_enable_from_other<T, E, U, G, const U&, const G&>* = nullptr>
  531. constexpr Expected(const Expected<U, G>& rhs) : ctor_base{detail::default_constructor_tag{}} {
  532. if (rhs.has_value()) {
  533. this->construct(*rhs);
  534. } else {
  535. this->construct_error(rhs.error());
  536. }
  537. }
  538. template <typename U, typename G,
  539. std::enable_if_t<!(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* =
  540. nullptr,
  541. detail::expected_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
  542. constexpr explicit Expected(Expected<U, G>&& rhs)
  543. : ctor_base{detail::default_constructor_tag{}} {
  544. if (rhs.has_value()) {
  545. this->construct(std::move(*rhs));
  546. } else {
  547. this->construct_error(std::move(rhs.error()));
  548. }
  549. }
  550. template <typename U, typename G,
  551. std::enable_if_t<(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* =
  552. nullptr,
  553. detail::expected_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
  554. constexpr Expected(Expected<U, G>&& rhs) : ctor_base{detail::default_constructor_tag{}} {
  555. if (rhs.has_value()) {
  556. this->construct(std::move(*rhs));
  557. } else {
  558. this->construct_error(std::move(rhs.error()));
  559. }
  560. }
  561. template <typename U = T, std::enable_if_t<!std::is_convertible_v<U&&, T>>* = nullptr,
  562. detail::expected_enable_forward_value<T, E, U>* = nullptr>
  563. constexpr explicit Expected(U&& v) : Expected{std::in_place, std::forward<U>(v)} {}
  564. template <typename U = T, std::enable_if_t<std::is_convertible_v<U&&, T>>* = nullptr,
  565. detail::expected_enable_forward_value<T, E, U>* = nullptr>
  566. constexpr Expected(U&& v) : Expected{std::in_place, std::forward<U>(v)} {}
  567. template <typename U = T, typename G = T,
  568. std::enable_if_t<std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
  569. std::enable_if_t<(
  570. !std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
  571. !std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::remove_cvref_t<U>>> &&
  572. std::is_constructible_v<T, U> && std::is_assignable_v<G&, U> &&
  573. std::is_nothrow_move_constructible_v<E>)>* = nullptr>
  574. Expected& operator=(U&& v) {
  575. if (has_value()) {
  576. val() = std::forward<U>(v);
  577. } else {
  578. err().~Unexpected<E>();
  579. new (valptr()) T{std::forward<U>(v)};
  580. this->m_has_val = true;
  581. }
  582. return *this;
  583. }
  584. template <typename U = T, typename G = T,
  585. std::enable_if_t<!std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
  586. std::enable_if_t<(
  587. !std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
  588. !std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::remove_cvref_t<U>>> &&
  589. std::is_constructible_v<T, U> && std::is_assignable_v<G&, U> &&
  590. std::is_nothrow_move_constructible_v<E>)>* = nullptr>
  591. Expected& operator=(U&& v) {
  592. if (has_value()) {
  593. val() = std::forward<U>(v);
  594. } else {
  595. auto tmp = std::move(err());
  596. err().~Unexpected<E>();
  597. new (valptr()) T{std::forward<U>(v)};
  598. this->m_has_val = true;
  599. }
  600. return *this;
  601. }
  602. template <typename G = E, std::enable_if_t<std::is_nothrow_copy_constructible_v<G> &&
  603. std::is_assignable_v<G&, G>>* = nullptr>
  604. Expected& operator=(const Unexpected<G>& rhs) {
  605. if (!has_value()) {
  606. err() = rhs;
  607. } else {
  608. this->destroy_val();
  609. new (errptr()) Unexpected<E>{rhs};
  610. this->m_has_val = false;
  611. }
  612. return *this;
  613. }
  614. template <typename G = E, std::enable_if_t<std::is_nothrow_move_constructible_v<G> &&
  615. std::is_move_assignable_v<G>>* = nullptr>
  616. Expected& operator=(Unexpected<G>&& rhs) noexcept {
  617. if (!has_value()) {
  618. err() = std::move(rhs);
  619. } else {
  620. this->destroy_val();
  621. new (errptr()) Unexpected<E>{std::move(rhs)};
  622. this->m_has_val = false;
  623. }
  624. return *this;
  625. }
  626. template <typename... Args,
  627. std::enable_if_t<std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
  628. void emplace(Args&&... args) {
  629. if (has_value()) {
  630. val() = T{std::forward<Args>(args)...};
  631. } else {
  632. err().~Unexpected<E>();
  633. new (valptr()) T{std::forward<Args>(args)...};
  634. this->m_has_val = true;
  635. }
  636. }
  637. template <typename... Args,
  638. std::enable_if_t<!std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
  639. void emplace(Args&&... args) {
  640. if (has_value()) {
  641. val() = T{std::forward<Args>(args)...};
  642. } else {
  643. auto tmp = std::move(err());
  644. err().~Unexpected<E>();
  645. new (valptr()) T{std::forward<Args>(args)...};
  646. this->m_has_val = true;
  647. }
  648. }
  649. template <typename U, typename... Args,
  650. std::enable_if_t<std::is_nothrow_constructible_v<T, std::initializer_list<U>&,
  651. Args&&...>>* = nullptr>
  652. void emplace(std::initializer_list<U> il, Args&&... args) {
  653. if (has_value()) {
  654. T t{il, std::forward<Args>(args)...};
  655. val() = std::move(t);
  656. } else {
  657. err().~Unexpected<E>();
  658. new (valptr()) T{il, std::forward<Args>(args)...};
  659. this->m_has_val = true;
  660. }
  661. }
  662. template <typename U, typename... Args,
  663. std::enable_if_t<!std::is_nothrow_constructible_v<T, std::initializer_list<U>&,
  664. Args&&...>>* = nullptr>
  665. void emplace(std::initializer_list<U> il, Args&&... args) {
  666. if (has_value()) {
  667. T t{il, std::forward<Args>(args)...};
  668. val() = std::move(t);
  669. } else {
  670. auto tmp = std::move(err());
  671. err().~Unexpected<E>();
  672. new (valptr()) T{il, std::forward<Args>(args)...};
  673. this->m_has_val = true;
  674. }
  675. }
  676. constexpr T* operator->() {
  677. return valptr();
  678. }
  679. constexpr const T* operator->() const {
  680. return valptr();
  681. }
  682. template <typename U = T>
  683. constexpr U& operator*() & {
  684. return val();
  685. }
  686. template <typename U = T>
  687. constexpr const U& operator*() const& {
  688. return val();
  689. }
  690. template <typename U = T>
  691. constexpr U&& operator*() && {
  692. return std::move(val());
  693. }
  694. template <typename U = T>
  695. constexpr const U&& operator*() const&& {
  696. return std::move(val());
  697. }
  698. constexpr bool has_value() const noexcept {
  699. return this->m_has_val;
  700. }
  701. constexpr explicit operator bool() const noexcept {
  702. return this->m_has_val;
  703. }
  704. template <typename U = T>
  705. constexpr U& value() & {
  706. return val();
  707. }
  708. template <typename U = T>
  709. constexpr const U& value() const& {
  710. return val();
  711. }
  712. template <typename U = T>
  713. constexpr U&& value() && {
  714. return std::move(val());
  715. }
  716. template <typename U = T>
  717. constexpr const U&& value() const&& {
  718. return std::move(val());
  719. }
  720. constexpr E& error() & {
  721. return err().value();
  722. }
  723. constexpr const E& error() const& {
  724. return err().value();
  725. }
  726. constexpr E&& error() && {
  727. return std::move(err().value());
  728. }
  729. constexpr const E&& error() const&& {
  730. return std::move(err().value());
  731. }
  732. template <typename U>
  733. constexpr T value_or(U&& v) const& {
  734. static_assert(std::is_copy_constructible_v<T> && std::is_convertible_v<U&&, T>,
  735. "T must be copy-constructible and convertible from U&&");
  736. return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
  737. }
  738. template <typename U>
  739. constexpr T value_or(U&& v) && {
  740. static_assert(std::is_move_constructible_v<T> && std::is_convertible_v<U&&, T>,
  741. "T must be move-constructible and convertible from U&&");
  742. return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
  743. }
  744. private:
  745. static_assert(!std::is_reference_v<T>, "T must not be a reference");
  746. static_assert(!std::is_same_v<T, std::remove_cv_t<std::in_place_t>>,
  747. "T must not be std::in_place_t");
  748. static_assert(!std::is_same_v<T, std::remove_cv_t<unexpect_t>>, "T must not be unexpect_t");
  749. static_assert(!std::is_same_v<T, std::remove_cv_t<Unexpected<E>>>,
  750. "T must not be Unexpected<E>");
  751. static_assert(!std::is_reference_v<E>, "E must not be a reference");
  752. T* valptr() {
  753. return std::addressof(this->m_val);
  754. }
  755. const T* valptr() const {
  756. return std::addressof(this->m_val);
  757. }
  758. Unexpected<E>* errptr() {
  759. return std::addressof(this->m_unexpect);
  760. }
  761. const Unexpected<E>* errptr() const {
  762. return std::addressof(this->m_unexpect);
  763. }
  764. template <typename U = T>
  765. constexpr U& val() {
  766. return this->m_val;
  767. }
  768. template <typename U = T>
  769. constexpr const U& val() const {
  770. return this->m_val;
  771. }
  772. constexpr Unexpected<E>& err() {
  773. return this->m_unexpect;
  774. }
  775. constexpr const Unexpected<E>& err() const {
  776. return this->m_unexpect;
  777. }
  778. using impl_base = detail::expected_move_assign_base<T, E>;
  779. using ctor_base = detail::expected_default_ctor_base<T, E>;
  780. };
  781. template <typename T, typename E, typename U, typename F>
  782. constexpr bool operator==(const Expected<T, E>& lhs, const Expected<U, F>& rhs) {
  783. return (lhs.has_value() != rhs.has_value())
  784. ? false
  785. : (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs);
  786. }
  787. template <typename T, typename E, typename U, typename F>
  788. constexpr bool operator!=(const Expected<T, E>& lhs, const Expected<U, F>& rhs) {
  789. return !operator==(lhs, rhs);
  790. }
  791. template <typename T, typename E, typename U>
  792. constexpr bool operator==(const Expected<T, E>& x, const U& v) {
  793. return x.has_value() ? *x == v : false;
  794. }
  795. template <typename T, typename E, typename U>
  796. constexpr bool operator==(const U& v, const Expected<T, E>& x) {
  797. return x.has_value() ? *x == v : false;
  798. }
  799. template <typename T, typename E, typename U>
  800. constexpr bool operator!=(const Expected<T, E>& x, const U& v) {
  801. return !operator==(x, v);
  802. }
  803. template <typename T, typename E, typename U>
  804. constexpr bool operator!=(const U& v, const Expected<T, E>& x) {
  805. return !operator==(v, x);
  806. }
  807. template <typename T, typename E>
  808. constexpr bool operator==(const Expected<T, E>& x, const Unexpected<E>& e) {
  809. return x.has_value() ? false : x.error() == e.value();
  810. }
  811. template <typename T, typename E>
  812. constexpr bool operator==(const Unexpected<E>& e, const Expected<T, E>& x) {
  813. return x.has_value() ? false : x.error() == e.value();
  814. }
  815. template <typename T, typename E>
  816. constexpr bool operator!=(const Expected<T, E>& x, const Unexpected<E>& e) {
  817. return !operator==(x, e);
  818. }
  819. template <typename T, typename E>
  820. constexpr bool operator!=(const Unexpected<E>& e, const Expected<T, E>& x) {
  821. return !operator==(e, x);
  822. }
  823. } // namespace Common