expected.h 37 KB

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