value_storage.hpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /* Essentially an internal optional implementation :)
  2. (C) 2017-2023 Niall Douglas <http://www.nedproductions.biz/> (24 commits)
  3. File Created: June 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_VALUE_STORAGE_HPP
  26. #define BOOST_OUTCOME_VALUE_STORAGE_HPP
  27. #include "../config.hpp"
  28. #include <cassert>
  29. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  30. namespace detail
  31. {
  32. // Helpers for move assigning to empty storage
  33. template <class T, bool isCopyOrMoveConstructible = std::is_copy_constructible<T>::value || std::is_move_constructible<T>::value,
  34. bool isDefaultConstructibleAndCopyOrMoveAssignable =
  35. std::is_default_constructible<T>::value && (std::is_copy_assignable<T>::value || std::is_move_assignable<T>::value)>
  36. struct move_assign_to_empty;
  37. // Prefer to use move or copy construction
  38. template <class T> struct move_assign_to_empty<T, true, false>
  39. {
  40. move_assign_to_empty(T *dest, T *o) noexcept(std::is_nothrow_move_constructible<T>::value) { new(dest) T(static_cast<T &&>(*o)); }
  41. };
  42. template <class T> struct move_assign_to_empty<T, true, true>
  43. {
  44. move_assign_to_empty(T *dest, T *o) noexcept(std::is_nothrow_move_constructible<T>::value) { new(dest) T(static_cast<T &&>(*o)); }
  45. };
  46. // But fall back on default construction and move assign if necessary
  47. template <class T> struct move_assign_to_empty<T, false, true>
  48. {
  49. move_assign_to_empty(T *dest, T *o) noexcept(std::is_nothrow_default_constructible<T>::value &&std::is_nothrow_move_assignable<T>::value)
  50. {
  51. new(dest) T;
  52. *dest = static_cast<T &&>(*o);
  53. }
  54. };
  55. // Void does nothing
  56. template <> struct move_assign_to_empty<void, false, false>
  57. {
  58. move_assign_to_empty(void *, void *) noexcept
  59. { /* nothing to assign */
  60. }
  61. };
  62. template <> struct move_assign_to_empty<const void, false, false>
  63. {
  64. move_assign_to_empty(const void *, const void *) noexcept
  65. { /* nothing to assign */
  66. }
  67. };
  68. // Helpers for copy assigning to empty storage
  69. template <class T, bool isCopyConstructible = std::is_copy_constructible<T>::value,
  70. bool isDefaultConstructibleAndCopyAssignable = std::is_default_constructible<T>::value &&std::is_copy_assignable<T>::value>
  71. struct copy_assign_to_empty;
  72. // Prefer to use copy construction
  73. template <class T> struct copy_assign_to_empty<T, true, false>
  74. {
  75. copy_assign_to_empty(T *dest, const T *o) noexcept(std::is_nothrow_copy_constructible<T>::value) { new(dest) T(*o); }
  76. };
  77. template <class T> struct copy_assign_to_empty<T, true, true>
  78. {
  79. copy_assign_to_empty(T *dest, const T *o) noexcept(std::is_nothrow_copy_constructible<T>::value) { new(dest) T(*o); }
  80. };
  81. // But fall back on default construction and copy assign if necessary
  82. template <class T> struct copy_assign_to_empty<T, false, true>
  83. {
  84. copy_assign_to_empty(T *dest, const T *o) noexcept(std::is_nothrow_default_constructible<T>::value &&std::is_nothrow_copy_assignable<T>::value)
  85. {
  86. new(dest) T;
  87. *dest = *o;
  88. }
  89. };
  90. // Void does nothing
  91. template <> struct copy_assign_to_empty<void, false, false>
  92. {
  93. copy_assign_to_empty(void *, void *) noexcept
  94. { /* nothing to assign */
  95. }
  96. };
  97. template <> struct copy_assign_to_empty<const void, false, false>
  98. {
  99. copy_assign_to_empty(const void *, const void *) noexcept
  100. { /* nothing to assign */
  101. }
  102. };
  103. template <class T, bool nothrow> struct strong_swap_impl
  104. {
  105. constexpr strong_swap_impl(bool &allgood, T &a, T &b)
  106. {
  107. allgood = true;
  108. using std::swap;
  109. swap(a, b);
  110. }
  111. };
  112. template <class T, bool nothrow> struct strong_placement_impl
  113. {
  114. template <class F> constexpr strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
  115. {
  116. allgood = true;
  117. new(a) T(static_cast<T &&>(*b));
  118. b->~T();
  119. f();
  120. }
  121. };
  122. #ifndef BOOST_NO_EXCEPTIONS
  123. template <class T> struct strong_swap_impl<T, false>
  124. {
  125. strong_swap_impl(bool &allgood, T &a, T &b)
  126. {
  127. allgood = true;
  128. T v(static_cast<T &&>(a));
  129. try
  130. {
  131. a = static_cast<T &&>(b);
  132. }
  133. catch(...)
  134. {
  135. // Try to put back a
  136. try
  137. {
  138. a = static_cast<T &&>(v);
  139. // fall through as all good
  140. }
  141. catch(...)
  142. {
  143. // failed to completely restore
  144. allgood = false;
  145. // throw away second exception
  146. }
  147. throw; // rethrow original exception
  148. }
  149. // b has been moved to a, try to move v to b
  150. try
  151. {
  152. b = static_cast<T &&>(v);
  153. }
  154. catch(...)
  155. {
  156. // Try to restore a to b, and v to a
  157. try
  158. {
  159. b = static_cast<T &&>(a);
  160. a = static_cast<T &&>(v);
  161. // fall through as all good
  162. }
  163. catch(...)
  164. {
  165. // failed to completely restore
  166. allgood = false;
  167. // throw away second exception
  168. }
  169. throw; // rethrow original exception
  170. }
  171. }
  172. };
  173. template <class T> struct strong_placement_impl<T, false>
  174. {
  175. template <class F> strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
  176. {
  177. new(a) T(static_cast<T &&>(*b));
  178. try
  179. {
  180. b->~T();
  181. f();
  182. }
  183. catch(...)
  184. {
  185. // Try to put back a, but only if we are still good
  186. if(allgood)
  187. {
  188. try
  189. {
  190. new(b) T(static_cast<T &&>(*a));
  191. // fall through as all good
  192. }
  193. catch(...)
  194. {
  195. // failed to completely restore
  196. allgood = false;
  197. // throw away second exception
  198. }
  199. throw; // rethrow original exception
  200. }
  201. }
  202. }
  203. };
  204. #endif
  205. } // namespace detail
  206. /*!
  207. */
  208. BOOST_OUTCOME_TEMPLATE(class T)
  209. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
  210. constexpr inline void strong_swap(bool &allgood, T &a, T &b) noexcept(detail::is_nothrow_swappable<T>::value)
  211. {
  212. detail::strong_swap_impl<T, detail::is_nothrow_swappable<T>::value>(allgood, a, b);
  213. }
  214. /*!
  215. */
  216. BOOST_OUTCOME_TEMPLATE(class T, class F)
  217. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
  218. constexpr inline void strong_placement(bool &allgood, T *a, T *b, F &&f) noexcept(std::is_nothrow_move_constructible<T>::value)
  219. {
  220. detail::strong_placement_impl<T, std::is_nothrow_move_constructible<T>::value>(allgood, a, b, static_cast<F &&>(f));
  221. }
  222. namespace detail
  223. {
  224. template <class T>
  225. constexpr
  226. #ifdef _MSC_VER
  227. __declspec(noreturn)
  228. #elif defined(__GNUC__) || defined(__clang__)
  229. __attribute__((noreturn))
  230. #endif
  231. void make_ub(T && /*unused*/)
  232. {
  233. assert(false); // NOLINT
  234. #if defined(__GNUC__) || defined(__clang__)
  235. __builtin_unreachable();
  236. #elif defined(_MSC_VER)
  237. __assume(0);
  238. #endif
  239. }
  240. /* Outcome v1 used a C bitfield whose values were tracked by compiler optimisers nicely,
  241. but that produces ICEs when used in constexpr.
  242. Outcome v2.0-v2.1 used a 32 bit integer and manually set and cleared bits. Unfortunately
  243. only GCC's optimiser tracks bit values during constant folding, and only per byte, and
  244. even then unreliably. https://wg21.link/P1886 "Error speed benchmarking" showed just how
  245. poorly clang and MSVC fails to optimise outcome-using code, if you manually set bits.
  246. Outcome v2.2 therefore uses an enum with fixed values, and constexpr manipulation functions
  247. to change the value to one of the enum's values. This is stupid to look at in source code,
  248. but it make clang's optimiser do the right thing, so it's worth it.
  249. */
  250. #define BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS 0
  251. enum class status : uint16_t
  252. {
  253. // WARNING: These bits are not tracked by abi-dumper, but changing them will break ABI!
  254. none = 0,
  255. have_value = (1U << 0U),
  256. have_error = (1U << 1U),
  257. have_exception = (2U << 1U),
  258. have_error_exception = (3U << 1U),
  259. // failed to complete a strong swap
  260. have_lost_consistency = (1U << 3U),
  261. have_value_lost_consistency = (1U << 0U) | (1U << 3U),
  262. have_error_lost_consistency = (1U << 1U) | (1U << 3U),
  263. have_exception_lost_consistency = (2U << 1U) | (1U << 3U),
  264. have_error_exception_lost_consistency = (3U << 1U) | (1U << 3U),
  265. // can errno be set from this error?
  266. have_error_is_errno = (1U << 4U),
  267. have_error_error_is_errno = (1U << 1U) | (1U << 4U),
  268. have_error_exception_error_is_errno = (3U << 1U) | (1U << 4U),
  269. have_error_lost_consistency_error_is_errno = (1U << 1U) | (1U << 3U) | (1U << 4U),
  270. have_error_exception_lost_consistency_error_is_errno = (3U << 1U) | (1U << 3U) | (1U << 4U),
  271. // value has been moved from
  272. have_moved_from = (1U << 5U)
  273. };
  274. struct status_bitfield_type
  275. {
  276. status status_value{status::none};
  277. uint16_t spare_storage_value{0}; // hooks::spare_storage()
  278. constexpr status_bitfield_type() = default;
  279. constexpr status_bitfield_type(status v) noexcept
  280. : status_value(v)
  281. {
  282. } // NOLINT
  283. constexpr status_bitfield_type(status v, uint16_t s) noexcept
  284. : status_value(v)
  285. , spare_storage_value(s)
  286. {
  287. }
  288. constexpr status_bitfield_type(const status_bitfield_type &) = default;
  289. constexpr status_bitfield_type(status_bitfield_type &&) = default;
  290. constexpr status_bitfield_type &operator=(const status_bitfield_type &) = default;
  291. constexpr status_bitfield_type &operator=(status_bitfield_type &&) = default;
  292. //~status_bitfield_type() = default; // Do NOT uncomment this, it breaks older clangs!
  293. constexpr bool have_value() const noexcept
  294. {
  295. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  296. return (status_value == status::have_value) //
  297. || (status_value == status::have_value_lost_consistency) //
  298. ;
  299. #else
  300. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_value)) != 0;
  301. #endif
  302. }
  303. constexpr bool have_error() const noexcept
  304. {
  305. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  306. return (status_value == status::have_error) //
  307. || (status_value == status::have_error_exception) //
  308. || (status_value == status::have_error_lost_consistency) //
  309. || (status_value == status::have_error_exception_lost_consistency) //
  310. || (status_value == status::have_error_error_is_errno) //
  311. || (status_value == status::have_error_exception_error_is_errno) //
  312. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  313. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  314. ;
  315. #else
  316. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error)) != 0;
  317. #endif
  318. }
  319. constexpr bool have_exception() const noexcept
  320. {
  321. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  322. return (status_value == status::have_exception) //
  323. || (status_value == status::have_error_exception) //
  324. || (status_value == status::have_exception_lost_consistency) //
  325. || (status_value == status::have_error_exception_lost_consistency) //
  326. || (status_value == status::have_error_exception_error_is_errno) //
  327. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  328. ;
  329. #else
  330. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_exception)) != 0;
  331. #endif
  332. }
  333. constexpr bool have_lost_consistency() const noexcept
  334. {
  335. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  336. return (status_value == status::have_value_lost_consistency) //
  337. || (status_value == status::have_error_lost_consistency) //
  338. || (status_value == status::have_exception_lost_consistency) //
  339. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  340. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  341. ;
  342. #else
  343. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_lost_consistency)) != 0;
  344. #endif
  345. }
  346. constexpr bool have_error_is_errno() const noexcept
  347. {
  348. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  349. return (status_value == status::have_error_error_is_errno) //
  350. || (status_value == status::have_error_exception_error_is_errno) //
  351. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  352. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  353. ;
  354. #else
  355. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error_is_errno)) != 0;
  356. #endif
  357. }
  358. constexpr bool have_moved_from() const noexcept
  359. {
  360. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  361. #error Fixme
  362. #else
  363. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_moved_from)) != 0;
  364. #endif
  365. }
  366. constexpr status_bitfield_type &set_have_value(bool v) noexcept
  367. {
  368. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  369. switch(status_value)
  370. {
  371. case status::none:
  372. if(v)
  373. {
  374. status_value = status::have_value;
  375. }
  376. break;
  377. case status::have_value:
  378. if(!v)
  379. {
  380. status_value = status::none;
  381. }
  382. break;
  383. case status::have_error:
  384. if(v)
  385. {
  386. make_ub(*this);
  387. }
  388. break;
  389. case status::have_exception:
  390. if(v)
  391. {
  392. make_ub(*this);
  393. }
  394. break;
  395. case status::have_error_exception:
  396. if(v)
  397. {
  398. make_ub(*this);
  399. }
  400. break;
  401. case status::have_value_lost_consistency:
  402. if(!v)
  403. {
  404. status_value = status::none;
  405. }
  406. break;
  407. case status::have_error_lost_consistency:
  408. if(v)
  409. {
  410. make_ub(*this);
  411. }
  412. break;
  413. case status::have_exception_lost_consistency:
  414. if(v)
  415. {
  416. make_ub(*this);
  417. }
  418. break;
  419. case status::have_error_exception_lost_consistency:
  420. if(v)
  421. {
  422. make_ub(*this);
  423. }
  424. break;
  425. case status::have_error_error_is_errno:
  426. if(v)
  427. {
  428. make_ub(*this);
  429. }
  430. break;
  431. case status::have_error_exception_error_is_errno:
  432. if(v)
  433. {
  434. make_ub(*this);
  435. }
  436. break;
  437. case status::have_error_lost_consistency_error_is_errno:
  438. if(v)
  439. {
  440. make_ub(*this);
  441. }
  442. break;
  443. case status::have_error_exception_lost_consistency_error_is_errno:
  444. if(v)
  445. {
  446. make_ub(*this);
  447. }
  448. break;
  449. }
  450. #else
  451. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_value)) :
  452. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_value)));
  453. #endif
  454. return *this;
  455. }
  456. constexpr status_bitfield_type &set_have_error(bool v) noexcept
  457. {
  458. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  459. switch(status_value)
  460. {
  461. case status::none:
  462. if(v)
  463. {
  464. status_value = status::have_error;
  465. }
  466. break;
  467. case status::have_value:
  468. if(v)
  469. {
  470. make_ub(*this);
  471. }
  472. break;
  473. case status::have_error:
  474. if(!v)
  475. {
  476. status_value = status::none;
  477. }
  478. break;
  479. case status::have_exception:
  480. if(v)
  481. {
  482. status_value = status::have_error_exception;
  483. }
  484. break;
  485. case status::have_error_exception:
  486. if(!v)
  487. {
  488. status_value = status::have_exception;
  489. }
  490. break;
  491. case status::have_value_lost_consistency:
  492. if(v)
  493. {
  494. make_ub(*this);
  495. }
  496. break;
  497. case status::have_error_lost_consistency:
  498. if(!v)
  499. {
  500. status_value = status::none;
  501. }
  502. break;
  503. case status::have_exception_lost_consistency:
  504. if(v)
  505. {
  506. status_value = status::have_error_exception_lost_consistency;
  507. }
  508. break;
  509. case status::have_error_exception_lost_consistency:
  510. if(!v)
  511. {
  512. status_value = status::have_exception_lost_consistency;
  513. }
  514. break;
  515. case status::have_error_error_is_errno:
  516. if(!v)
  517. {
  518. status_value = status::none;
  519. }
  520. break;
  521. case status::have_error_exception_error_is_errno:
  522. if(!v)
  523. {
  524. status_value = status::have_exception;
  525. }
  526. break;
  527. case status::have_error_lost_consistency_error_is_errno:
  528. if(!v)
  529. {
  530. status_value = status::none;
  531. }
  532. break;
  533. case status::have_error_exception_lost_consistency_error_is_errno:
  534. if(!v)
  535. {
  536. status_value = status::have_exception_lost_consistency;
  537. }
  538. break;
  539. }
  540. #else
  541. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error)) :
  542. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error)));
  543. #endif
  544. return *this;
  545. }
  546. constexpr status_bitfield_type &set_have_exception(bool v) noexcept
  547. {
  548. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  549. switch(status_value)
  550. {
  551. case status::none:
  552. if(v)
  553. {
  554. status_value = status::have_exception;
  555. }
  556. break;
  557. case status::have_value:
  558. if(v)
  559. {
  560. make_ub(*this);
  561. }
  562. break;
  563. case status::have_error:
  564. if(v)
  565. {
  566. status_value = status::have_error_exception;
  567. }
  568. break;
  569. case status::have_exception:
  570. if(!v)
  571. {
  572. status_value = status::none;
  573. }
  574. break;
  575. case status::have_error_exception:
  576. if(!v)
  577. {
  578. status_value = status::have_error;
  579. }
  580. break;
  581. case status::have_value_lost_consistency:
  582. if(v)
  583. {
  584. make_ub(*this);
  585. }
  586. break;
  587. case status::have_error_lost_consistency:
  588. if(v)
  589. {
  590. status_value = status::have_error_exception_lost_consistency;
  591. }
  592. break;
  593. case status::have_exception_lost_consistency:
  594. if(!v)
  595. {
  596. status_value = status::none;
  597. }
  598. break;
  599. case status::have_error_exception_lost_consistency:
  600. if(!v)
  601. {
  602. status_value = status::have_error_lost_consistency;
  603. }
  604. break;
  605. case status::have_error_error_is_errno:
  606. if(v)
  607. {
  608. status_value = status::have_error_exception_error_is_errno;
  609. }
  610. break;
  611. case status::have_error_exception_error_is_errno:
  612. if(!v)
  613. {
  614. status_value = status::have_error_error_is_errno;
  615. }
  616. break;
  617. case status::have_error_lost_consistency_error_is_errno:
  618. if(v)
  619. {
  620. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  621. }
  622. break;
  623. case status::have_error_exception_lost_consistency_error_is_errno:
  624. if(!v)
  625. {
  626. status_value = status::have_error_lost_consistency_error_is_errno;
  627. }
  628. break;
  629. }
  630. #else
  631. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_exception)) :
  632. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_exception)));
  633. #endif
  634. return *this;
  635. }
  636. constexpr status_bitfield_type &set_have_error_is_errno(bool v) noexcept
  637. {
  638. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  639. switch(status_value)
  640. {
  641. case status::none:
  642. make_ub(*this);
  643. break;
  644. case status::have_value:
  645. make_ub(*this);
  646. break;
  647. case status::have_error:
  648. if(v)
  649. {
  650. status_value = status::have_error_error_is_errno;
  651. }
  652. break;
  653. case status::have_exception:
  654. make_ub(*this);
  655. break;
  656. case status::have_error_exception:
  657. if(v)
  658. {
  659. status_value = status::have_error_exception_error_is_errno;
  660. }
  661. break;
  662. case status::have_value_lost_consistency:
  663. make_ub(*this);
  664. break;
  665. case status::have_error_lost_consistency:
  666. if(v)
  667. {
  668. status_value = status::have_error_lost_consistency_error_is_errno;
  669. }
  670. break;
  671. case status::have_exception_lost_consistency:
  672. make_ub(*this);
  673. break;
  674. case status::have_error_exception_lost_consistency:
  675. if(v)
  676. {
  677. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  678. }
  679. break;
  680. case status::have_error_error_is_errno:
  681. if(!v)
  682. {
  683. status_value = status::have_error;
  684. }
  685. break;
  686. case status::have_error_exception_error_is_errno:
  687. if(!v)
  688. {
  689. status_value = status::have_error_exception;
  690. }
  691. break;
  692. case status::have_error_lost_consistency_error_is_errno:
  693. if(!v)
  694. {
  695. status_value = status::have_error_lost_consistency;
  696. }
  697. break;
  698. case status::have_error_exception_lost_consistency_error_is_errno:
  699. if(!v)
  700. {
  701. status_value = status::have_error_exception_lost_consistency;
  702. }
  703. break;
  704. }
  705. #else
  706. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error_is_errno)) :
  707. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error_is_errno)));
  708. #endif
  709. return *this;
  710. }
  711. constexpr status_bitfield_type &set_have_lost_consistency(bool v) noexcept
  712. {
  713. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  714. switch(status_value)
  715. {
  716. case status::none:
  717. if(v)
  718. {
  719. make_ub(*this);
  720. }
  721. break;
  722. case status::have_value:
  723. if(v)
  724. {
  725. status_value = status::have_value_lost_consistency;
  726. }
  727. break;
  728. case status::have_error:
  729. if(v)
  730. {
  731. status_value = status::have_error_lost_consistency;
  732. }
  733. break;
  734. case status::have_exception:
  735. if(v)
  736. {
  737. status_value = status::have_exception_lost_consistency;
  738. }
  739. break;
  740. case status::have_error_exception:
  741. if(v)
  742. {
  743. status_value = status::have_error_exception_lost_consistency;
  744. }
  745. break;
  746. case status::have_value_lost_consistency:
  747. if(!v)
  748. {
  749. status_value = status::have_value;
  750. }
  751. break;
  752. case status::have_error_lost_consistency:
  753. if(!v)
  754. {
  755. status_value = status::have_error;
  756. }
  757. break;
  758. case status::have_exception_lost_consistency:
  759. if(!v)
  760. {
  761. status_value = status::have_exception;
  762. }
  763. break;
  764. case status::have_error_exception_lost_consistency:
  765. if(!v)
  766. {
  767. status_value = status::have_error_exception;
  768. }
  769. break;
  770. case status::have_error_error_is_errno:
  771. if(v)
  772. {
  773. status_value = status::have_error_lost_consistency_error_is_errno;
  774. }
  775. break;
  776. case status::have_error_exception_error_is_errno:
  777. if(v)
  778. {
  779. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  780. }
  781. break;
  782. case status::have_error_lost_consistency_error_is_errno:
  783. if(!v)
  784. {
  785. status_value = status::have_error_exception_error_is_errno;
  786. }
  787. break;
  788. case status::have_error_exception_lost_consistency_error_is_errno:
  789. if(!v)
  790. {
  791. status_value = status::have_error_exception_error_is_errno;
  792. }
  793. break;
  794. }
  795. #else
  796. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_lost_consistency)) :
  797. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_lost_consistency)));
  798. #endif
  799. return *this;
  800. }
  801. constexpr status_bitfield_type &set_have_moved_from(bool v) noexcept
  802. {
  803. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  804. #error Fixme
  805. #else
  806. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_moved_from)) :
  807. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_moved_from)));
  808. #endif
  809. return *this;
  810. }
  811. };
  812. #if !defined(NDEBUG)
  813. // Check is trivial in all ways except default constructibility
  814. static_assert(sizeof(status_bitfield_type) == 4, "status_bitfield_type is not sized 4 bytes!");
  815. static_assert(std::is_trivially_copyable<status_bitfield_type>::value, "status_bitfield_type is not trivially copyable!");
  816. static_assert(std::is_trivially_assignable<status_bitfield_type, status_bitfield_type>::value, "status_bitfield_type is not trivially assignable!");
  817. static_assert(std::is_trivially_destructible<status_bitfield_type>::value, "status_bitfield_type is not trivially destructible!");
  818. static_assert(std::is_trivially_copy_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially copy constructible!");
  819. static_assert(std::is_trivially_move_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially move constructible!");
  820. static_assert(std::is_trivially_copy_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially copy assignable!");
  821. static_assert(std::is_trivially_move_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially move assignable!");
  822. // Also check is standard layout
  823. static_assert(std::is_standard_layout<status_bitfield_type>::value, "status_bitfield_type is not a standard layout type!");
  824. #endif
  825. template <class State> constexpr inline void _set_error_is_errno(State & /*unused*/) {}
  826. #ifdef _MSC_VER
  827. #pragma warning(push)
  828. #pragma warning(disable : 4127) // conditional expression is constant
  829. #pragma warning(disable : 4624) // destructor was implicitly defined as deleted
  830. #endif
  831. // Used if both T and E are trivial
  832. template <class T, class E> struct value_storage_trivial
  833. {
  834. using value_type = T;
  835. using error_type = E;
  836. // Disable in place construction if they are the same type
  837. struct disable_in_place_value_type
  838. {
  839. };
  840. struct disable_in_place_error_type
  841. {
  842. };
  843. using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
  844. using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
  845. using _value_type_ = devoid<value_type>;
  846. using _error_type_ = devoid<error_type>;
  847. union
  848. {
  849. empty_type _empty;
  850. _value_type_ _value;
  851. _error_type_ _error;
  852. };
  853. status_bitfield_type _status;
  854. constexpr value_storage_trivial() noexcept
  855. : _empty{}
  856. {
  857. }
  858. value_storage_trivial(const value_storage_trivial &) = default; // NOLINT
  859. value_storage_trivial(value_storage_trivial &&) = default; // NOLINT
  860. value_storage_trivial &operator=(const value_storage_trivial &) = default; // NOLINT
  861. value_storage_trivial &operator=(value_storage_trivial &&) = default; // NOLINT
  862. ~value_storage_trivial() = default;
  863. constexpr explicit value_storage_trivial(status_bitfield_type status)
  864. : _empty()
  865. , _status(status)
  866. {
  867. }
  868. template <class... Args>
  869. constexpr explicit value_storage_trivial(in_place_type_t<_value_type> /*unused*/,
  870. Args &&...args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
  871. : _value(static_cast<Args &&>(args)...)
  872. , _status(status::have_value)
  873. {
  874. }
  875. template <class U, class... Args>
  876. constexpr value_storage_trivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
  877. Args &&...args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
  878. : _value(il, static_cast<Args &&>(args)...)
  879. , _status(status::have_value)
  880. {
  881. }
  882. template <class... Args>
  883. constexpr explicit value_storage_trivial(in_place_type_t<_error_type> /*unused*/,
  884. Args &&...args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
  885. : _error(static_cast<Args &&>(args)...)
  886. , _status(status::have_error)
  887. {
  888. _set_error_is_errno(*this);
  889. }
  890. template <class U, class... Args>
  891. constexpr value_storage_trivial(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il,
  892. Args &&...args) noexcept(detail::is_nothrow_constructible<_error_type_, std::initializer_list<U>, Args...>)
  893. : _error(il, static_cast<Args &&>(args)...)
  894. , _status(status::have_error)
  895. {
  896. _set_error_is_errno(*this);
  897. }
  898. struct nonvoid_converting_constructor_tag
  899. {
  900. };
  901. template <class U, class V>
  902. static constexpr bool enable_nonvoid_converting_constructor =
  903. !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
  904. && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
  905. BOOST_OUTCOME_TEMPLATE(class U, class V)
  906. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  907. constexpr explicit value_storage_trivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  908. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  909. : value_storage_trivial(o._status.have_value() ?
  910. value_storage_trivial(in_place_type<value_type>, o._value) :
  911. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
  912. {
  913. _status = o._status;
  914. }
  915. BOOST_OUTCOME_TEMPLATE(class U, class V)
  916. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  917. constexpr explicit value_storage_trivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  918. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  919. : value_storage_trivial(
  920. o._status.have_value() ?
  921. value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  922. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
  923. {
  924. _status = o._status;
  925. }
  926. struct void_value_converting_constructor_tag
  927. {
  928. };
  929. template <class V>
  930. static constexpr bool enable_void_value_converting_constructor = std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
  931. BOOST_OUTCOME_TEMPLATE(class V)
  932. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  933. constexpr explicit value_storage_trivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  934. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  935. : value_storage_trivial(o._status.have_value() ?
  936. value_storage_trivial(in_place_type<value_type>) :
  937. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
  938. {
  939. _status = o._status;
  940. }
  941. BOOST_OUTCOME_TEMPLATE(class V)
  942. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  943. constexpr explicit value_storage_trivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  944. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  945. : value_storage_trivial(
  946. o._status.have_value() ?
  947. value_storage_trivial(in_place_type<value_type>) :
  948. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
  949. {
  950. _status = o._status;
  951. }
  952. struct void_error_converting_constructor_tag
  953. {
  954. };
  955. template <class U>
  956. static constexpr bool enable_void_error_converting_constructor = std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
  957. BOOST_OUTCOME_TEMPLATE(class U)
  958. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  959. constexpr explicit value_storage_trivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  960. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  961. : value_storage_trivial(o._status.have_value() ?
  962. value_storage_trivial(in_place_type<value_type>, o._value) :
  963. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
  964. {
  965. _status = o._status;
  966. }
  967. BOOST_OUTCOME_TEMPLATE(class U)
  968. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  969. constexpr explicit value_storage_trivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  970. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  971. : value_storage_trivial(o._status.have_value() ?
  972. value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  973. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
  974. {
  975. _status = o._status;
  976. }
  977. constexpr void swap(value_storage_trivial &o) noexcept
  978. {
  979. // storage is trivial, so just use assignment
  980. auto temp = static_cast<value_storage_trivial &&>(*this);
  981. *this = static_cast<value_storage_trivial &&>(o);
  982. o = static_cast<value_storage_trivial &&>(temp);
  983. }
  984. };
  985. /* Used if T or E is non-trivial. The additional constexpr is injected in C++ 20 to enable Outcome to
  986. work in constexpr evaluation contexts in C++ 20 where non-trivial constexpr destructors are now allowed.
  987. */
  988. template <class T, class E> struct value_storage_nontrivial
  989. {
  990. using value_type = T;
  991. using error_type = E;
  992. struct disable_in_place_value_type
  993. {
  994. };
  995. struct disable_in_place_error_type
  996. {
  997. };
  998. using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
  999. using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
  1000. using _value_type_ = devoid<value_type>;
  1001. using _error_type_ = devoid<error_type>;
  1002. union
  1003. {
  1004. empty_type _empty1;
  1005. _value_type_ _value;
  1006. };
  1007. status_bitfield_type _status;
  1008. union
  1009. {
  1010. empty_type _empty2;
  1011. _error_type_ _error;
  1012. };
  1013. #if __cplusplus >= 202000L || _HAS_CXX20
  1014. constexpr
  1015. #endif
  1016. value_storage_nontrivial() noexcept
  1017. : _empty1{}
  1018. , _empty2{}
  1019. {
  1020. }
  1021. value_storage_nontrivial &operator=(const value_storage_nontrivial &) = default; // if reaches here, copy assignment is trivial
  1022. value_storage_nontrivial &operator=(value_storage_nontrivial &&) = default; // NOLINT if reaches here, move assignment is trivial
  1023. #if __cplusplus >= 202000L || _HAS_CXX20
  1024. constexpr
  1025. #endif
  1026. value_storage_nontrivial(value_storage_nontrivial &&o) noexcept(
  1027. std::is_nothrow_move_constructible<_value_type_>::value &&std::is_nothrow_move_constructible<_error_type_>::value) // NOLINT
  1028. {
  1029. if(o._status.have_value())
  1030. {
  1031. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  1032. }
  1033. else if(o._status.have_error())
  1034. {
  1035. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  1036. }
  1037. _status = o._status;
  1038. o._status.set_have_moved_from(true);
  1039. }
  1040. #if __cplusplus >= 202000L || _HAS_CXX20
  1041. constexpr
  1042. #endif
  1043. value_storage_nontrivial(const value_storage_nontrivial &o) noexcept(
  1044. std::is_nothrow_copy_constructible<_value_type_>::value &&std::is_nothrow_copy_constructible<_error_type_>::value)
  1045. {
  1046. if(o._status.have_value())
  1047. {
  1048. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(o._value); // NOLINT
  1049. }
  1050. else if(o._status.have_error())
  1051. {
  1052. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(o._error); // NOLINT
  1053. }
  1054. _status = o._status;
  1055. }
  1056. #if __cplusplus >= 202000L || _HAS_CXX20
  1057. constexpr
  1058. #endif
  1059. explicit value_storage_nontrivial(status_bitfield_type status)
  1060. : _empty1()
  1061. , _status(status)
  1062. , _empty2()
  1063. {
  1064. }
  1065. template <class... Args>
  1066. constexpr explicit value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/,
  1067. Args &&...args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
  1068. : _value(static_cast<Args &&>(args)...) // NOLINT
  1069. , _status(status::have_value)
  1070. {
  1071. }
  1072. template <class U, class... Args>
  1073. constexpr value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
  1074. Args &&...args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
  1075. : _value(il, static_cast<Args &&>(args)...)
  1076. , _status(status::have_value)
  1077. {
  1078. }
  1079. template <class... Args>
  1080. constexpr explicit value_storage_nontrivial(in_place_type_t<_error_type> /*unused*/,
  1081. Args &&...args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
  1082. : _status(status::have_error)
  1083. , _error(static_cast<Args &&>(args)...) // NOLINT
  1084. {
  1085. _set_error_is_errno(*this);
  1086. }
  1087. template <class U, class... Args>
  1088. constexpr value_storage_nontrivial(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il,
  1089. Args &&...args) noexcept(detail::is_nothrow_constructible<_error_type_, std::initializer_list<U>, Args...>)
  1090. : _status(status::have_error)
  1091. , _error(il, static_cast<Args &&>(args)...)
  1092. {
  1093. _set_error_is_errno(*this);
  1094. }
  1095. struct nonvoid_converting_constructor_tag
  1096. {
  1097. };
  1098. template <class U, class V>
  1099. static constexpr bool enable_nonvoid_converting_constructor =
  1100. !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
  1101. && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
  1102. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1103. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1104. constexpr explicit value_storage_nontrivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1105. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1106. : value_storage_nontrivial(o._status.have_value() ?
  1107. value_storage_nontrivial(in_place_type<value_type>, o._value) :
  1108. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
  1109. {
  1110. _status = o._status;
  1111. }
  1112. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1113. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1114. constexpr explicit value_storage_nontrivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1115. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1116. : value_storage_nontrivial(
  1117. o._status.have_value() ?
  1118. value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  1119. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
  1120. {
  1121. _status = o._status;
  1122. }
  1123. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1124. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1125. constexpr explicit value_storage_nontrivial(const value_storage_nontrivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1126. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1127. : value_storage_nontrivial(o._status.have_value() ?
  1128. value_storage_nontrivial(in_place_type<value_type>, o._value) :
  1129. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
  1130. {
  1131. _status = o._status;
  1132. }
  1133. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1134. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1135. constexpr explicit value_storage_nontrivial(value_storage_nontrivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1136. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1137. : value_storage_nontrivial(
  1138. o._status.have_value() ?
  1139. value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  1140. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
  1141. {
  1142. _status = o._status;
  1143. }
  1144. struct void_value_converting_constructor_tag
  1145. {
  1146. };
  1147. template <class V>
  1148. static constexpr bool enable_void_value_converting_constructor = std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
  1149. BOOST_OUTCOME_TEMPLATE(class V)
  1150. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  1151. constexpr explicit value_storage_nontrivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  1152. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  1153. {
  1154. if(o._status.have_value())
  1155. {
  1156. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(); // NOLINT
  1157. }
  1158. else if(o._status.have_error())
  1159. {
  1160. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(o._error); // NOLINT
  1161. }
  1162. _status = o._status;
  1163. }
  1164. BOOST_OUTCOME_TEMPLATE(class V)
  1165. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  1166. constexpr explicit value_storage_nontrivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  1167. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  1168. {
  1169. if(o._status.have_value())
  1170. {
  1171. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(); // NOLINT
  1172. }
  1173. else if(o._status.have_error())
  1174. {
  1175. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  1176. }
  1177. _status = o._status;
  1178. o._status.set_have_moved_from(true);
  1179. }
  1180. struct void_error_converting_constructor_tag
  1181. {
  1182. };
  1183. template <class U>
  1184. static constexpr bool enable_void_error_converting_constructor = std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
  1185. BOOST_OUTCOME_TEMPLATE(class U)
  1186. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  1187. constexpr explicit value_storage_nontrivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  1188. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  1189. {
  1190. if(o._status.have_value())
  1191. {
  1192. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(o._value); // NOLINT
  1193. }
  1194. else if(o._status.have_error())
  1195. {
  1196. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(); // NOLINT
  1197. }
  1198. _status = o._status;
  1199. }
  1200. BOOST_OUTCOME_TEMPLATE(class U)
  1201. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  1202. constexpr explicit value_storage_nontrivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  1203. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  1204. {
  1205. if(o._status.have_value())
  1206. {
  1207. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  1208. }
  1209. else if(o._status.have_error())
  1210. {
  1211. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(); // NOLINT
  1212. }
  1213. _status = o._status;
  1214. o._status.set_have_moved_from(true);
  1215. }
  1216. #if __cplusplus >= 202000L || _HAS_CXX20
  1217. constexpr
  1218. #endif
  1219. ~value_storage_nontrivial() noexcept(std::is_nothrow_destructible<_value_type_>::value &&std::is_nothrow_destructible<_error_type_>::value)
  1220. {
  1221. if(this->_status.have_value())
  1222. {
  1223. if(!trait::is_move_bitcopying<value_type>::value || !this->_status.have_moved_from())
  1224. {
  1225. this->_value.~_value_type_(); // NOLINT
  1226. }
  1227. this->_status.set_have_value(false);
  1228. }
  1229. else if(this->_status.have_error())
  1230. {
  1231. if(!trait::is_move_bitcopying<error_type>::value || !this->_status.have_moved_from())
  1232. {
  1233. this->_error.~_error_type_(); // NOLINT
  1234. }
  1235. this->_status.set_have_error(false);
  1236. }
  1237. }
  1238. #if __cplusplus >= 202000L || _HAS_CXX20
  1239. constexpr
  1240. #endif
  1241. void
  1242. swap(value_storage_nontrivial &o) noexcept(detail::is_nothrow_swappable<_value_type_>::value &&detail::is_nothrow_swappable<_error_type_>::value)
  1243. {
  1244. using std::swap;
  1245. // empty/empty
  1246. if(!_status.have_value() && !o._status.have_value() && !_status.have_error() && !o._status.have_error())
  1247. {
  1248. swap(_status, o._status);
  1249. return;
  1250. }
  1251. // value/value
  1252. if(_status.have_value() && o._status.have_value())
  1253. {
  1254. struct _
  1255. {
  1256. status_bitfield_type &a, &b;
  1257. bool all_good{false};
  1258. ~_()
  1259. {
  1260. if(!this->all_good)
  1261. {
  1262. // We lost one of the values
  1263. this->a.set_have_lost_consistency(true);
  1264. this->b.set_have_lost_consistency(true);
  1265. }
  1266. }
  1267. } _{_status, o._status};
  1268. strong_swap(_.all_good, _value, o._value);
  1269. swap(_status, o._status);
  1270. return;
  1271. }
  1272. // error/error
  1273. if(_status.have_error() && o._status.have_error())
  1274. {
  1275. struct _
  1276. {
  1277. status_bitfield_type &a, &b;
  1278. bool all_good{false};
  1279. ~_()
  1280. {
  1281. if(!this->all_good)
  1282. {
  1283. // We lost one of the values
  1284. this->a.set_have_lost_consistency(true);
  1285. this->b.set_have_lost_consistency(true);
  1286. }
  1287. }
  1288. } _{_status, o._status};
  1289. strong_swap(_.all_good, _error, o._error);
  1290. swap(_status, o._status);
  1291. return;
  1292. }
  1293. // Could be value/empty, error/empty, etc
  1294. if(_status.have_value() && !o._status.have_error())
  1295. {
  1296. // Move construct me into other
  1297. new(BOOST_OUTCOME_ADDRESS_OF(o._value)) _value_type_(static_cast<_value_type_ &&>(_value)); // NOLINT
  1298. if(!trait::is_move_bitcopying<value_type>::value)
  1299. {
  1300. this->_value.~value_type(); // NOLINT
  1301. }
  1302. swap(_status, o._status);
  1303. return;
  1304. }
  1305. if(o._status.have_value() && !_status.have_error())
  1306. {
  1307. // Move construct other into me
  1308. new(BOOST_OUTCOME_ADDRESS_OF(_value)) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  1309. if(!trait::is_move_bitcopying<value_type>::value)
  1310. {
  1311. o._value.~value_type(); // NOLINT
  1312. }
  1313. swap(_status, o._status);
  1314. return;
  1315. }
  1316. if(_status.have_error() && !o._status.have_value())
  1317. {
  1318. // Move construct me into other
  1319. new(BOOST_OUTCOME_ADDRESS_OF(o._error)) _error_type_(static_cast<_error_type_ &&>(_error)); // NOLINT
  1320. if(!trait::is_move_bitcopying<error_type>::value)
  1321. {
  1322. this->_error.~error_type(); // NOLINT
  1323. }
  1324. swap(_status, o._status);
  1325. return;
  1326. }
  1327. if(o._status.have_error() && !_status.have_value())
  1328. {
  1329. // Move construct other into me
  1330. new(BOOST_OUTCOME_ADDRESS_OF(_error)) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  1331. if(!trait::is_move_bitcopying<error_type>::value)
  1332. {
  1333. o._error.~error_type(); // NOLINT
  1334. }
  1335. swap(_status, o._status);
  1336. return;
  1337. }
  1338. // It can now only be value/error, or error/value
  1339. struct _
  1340. {
  1341. status_bitfield_type &a, &b;
  1342. _value_type_ *value, *o_value;
  1343. _error_type_ *error, *o_error;
  1344. bool all_good{true};
  1345. ~_()
  1346. {
  1347. if(!this->all_good)
  1348. {
  1349. // We lost one of the values
  1350. this->a.set_have_lost_consistency(true);
  1351. this->b.set_have_lost_consistency(true);
  1352. }
  1353. }
  1354. } _{_status, o._status, BOOST_OUTCOME_ADDRESS_OF(_value), BOOST_OUTCOME_ADDRESS_OF(o._value), BOOST_OUTCOME_ADDRESS_OF(_error), BOOST_OUTCOME_ADDRESS_OF(o._error)};
  1355. if(_status.have_value() && o._status.have_error())
  1356. {
  1357. strong_placement(_.all_good, _.o_value, _.value, [&_] { //
  1358. strong_placement(_.all_good, _.error, _.o_error, [&_] { //
  1359. swap(_.a, _.b); //
  1360. });
  1361. });
  1362. return;
  1363. }
  1364. if(_status.have_error() && o._status.have_value())
  1365. {
  1366. strong_placement(_.all_good, _.o_error, _.error, [&_] { //
  1367. strong_placement(_.all_good, _.value, _.o_value, [&_] { //
  1368. swap(_.a, _.b); //
  1369. });
  1370. });
  1371. return;
  1372. }
  1373. // Should never reach here
  1374. make_ub(_value);
  1375. }
  1376. };
  1377. template <class Base> struct value_storage_delete_copy_constructor : Base // NOLINT
  1378. {
  1379. using Base::Base;
  1380. using value_type = typename Base::value_type;
  1381. using error_type = typename Base::error_type;
  1382. value_storage_delete_copy_constructor() = default;
  1383. value_storage_delete_copy_constructor(const value_storage_delete_copy_constructor &) = delete;
  1384. value_storage_delete_copy_constructor(value_storage_delete_copy_constructor &&) = default; // NOLINT
  1385. value_storage_delete_copy_constructor &operator=(const value_storage_delete_copy_constructor &o) = default;
  1386. value_storage_delete_copy_constructor &operator=(value_storage_delete_copy_constructor &&o) = default; // NOLINT
  1387. ~value_storage_delete_copy_constructor() = default;
  1388. };
  1389. template <class Base> struct value_storage_delete_copy_assignment : Base // NOLINT
  1390. {
  1391. using Base::Base;
  1392. using value_type = typename Base::value_type;
  1393. using error_type = typename Base::error_type;
  1394. value_storage_delete_copy_assignment() = default;
  1395. value_storage_delete_copy_assignment(const value_storage_delete_copy_assignment &) = default;
  1396. value_storage_delete_copy_assignment(value_storage_delete_copy_assignment &&) = default; // NOLINT
  1397. value_storage_delete_copy_assignment &operator=(const value_storage_delete_copy_assignment &o) = delete;
  1398. value_storage_delete_copy_assignment &operator=(value_storage_delete_copy_assignment &&o) = default; // NOLINT
  1399. ~value_storage_delete_copy_assignment() = default;
  1400. };
  1401. template <class Base> struct value_storage_delete_move_assignment : Base // NOLINT
  1402. {
  1403. using Base::Base;
  1404. using value_type = typename Base::value_type;
  1405. using error_type = typename Base::error_type;
  1406. value_storage_delete_move_assignment() = default;
  1407. value_storage_delete_move_assignment(const value_storage_delete_move_assignment &) = default;
  1408. value_storage_delete_move_assignment(value_storage_delete_move_assignment &&) = default; // NOLINT
  1409. value_storage_delete_move_assignment &operator=(const value_storage_delete_move_assignment &o) = default;
  1410. value_storage_delete_move_assignment &operator=(value_storage_delete_move_assignment &&o) = delete;
  1411. ~value_storage_delete_move_assignment() = default;
  1412. };
  1413. template <class Base> struct value_storage_delete_move_constructor : Base // NOLINT
  1414. {
  1415. using Base::Base;
  1416. using value_type = typename Base::value_type;
  1417. using error_type = typename Base::error_type;
  1418. value_storage_delete_move_constructor() = default;
  1419. value_storage_delete_move_constructor(const value_storage_delete_move_constructor &) = default;
  1420. value_storage_delete_move_constructor(value_storage_delete_move_constructor &&) = delete;
  1421. value_storage_delete_move_constructor &operator=(const value_storage_delete_move_constructor &o) = default;
  1422. value_storage_delete_move_constructor &operator=(value_storage_delete_move_constructor &&o) = default;
  1423. ~value_storage_delete_move_constructor() = default;
  1424. };
  1425. template <class Base> struct value_storage_nontrivial_move_assignment : Base // NOLINT
  1426. {
  1427. using Base::Base;
  1428. using value_type = typename Base::value_type;
  1429. using error_type = typename Base::error_type;
  1430. value_storage_nontrivial_move_assignment() = default;
  1431. value_storage_nontrivial_move_assignment(const value_storage_nontrivial_move_assignment &) = default;
  1432. value_storage_nontrivial_move_assignment(value_storage_nontrivial_move_assignment &&) = default; // NOLINT
  1433. value_storage_nontrivial_move_assignment &operator=(const value_storage_nontrivial_move_assignment &o) = default;
  1434. ~value_storage_nontrivial_move_assignment() = default;
  1435. #if __cplusplus >= 202000L || _HAS_CXX20
  1436. constexpr
  1437. #endif
  1438. value_storage_nontrivial_move_assignment &
  1439. operator=(value_storage_nontrivial_move_assignment &&o) noexcept(
  1440. std::is_nothrow_move_assignable<value_type>::value &&std::is_nothrow_move_assignable<error_type>::value &&noexcept(move_assign_to_empty<value_type>(
  1441. static_cast<value_type *>(nullptr),
  1442. static_cast<value_type *>(nullptr))) &&noexcept(move_assign_to_empty<error_type>(static_cast<error_type *>(nullptr),
  1443. static_cast<error_type *>(nullptr)))) // NOLINT
  1444. {
  1445. using _value_type_ = typename Base::_value_type_;
  1446. using _error_type_ = typename Base::_error_type_;
  1447. if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1448. {
  1449. this->_status = o._status;
  1450. o._status.set_have_moved_from(true);
  1451. return *this;
  1452. }
  1453. if(this->_status.have_value() && o._status.have_value())
  1454. {
  1455. this->_value = static_cast<_value_type_ &&>(o._value); // NOLINT
  1456. this->_status = o._status;
  1457. o._status.set_have_moved_from(true);
  1458. return *this;
  1459. }
  1460. if(this->_status.have_error() && o._status.have_error())
  1461. {
  1462. this->_error = static_cast<_error_type_ &&>(o._error); // NOLINT
  1463. this->_status = o._status;
  1464. o._status.set_have_moved_from(true);
  1465. return *this;
  1466. }
  1467. if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
  1468. {
  1469. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1470. {
  1471. this->_value.~_value_type_(); // NOLINT
  1472. }
  1473. this->_status = o._status;
  1474. o._status.set_have_moved_from(true);
  1475. return *this;
  1476. }
  1477. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
  1478. {
  1479. move_assign_to_empty<_value_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_value), BOOST_OUTCOME_ADDRESS_OF(o._value));
  1480. this->_status = o._status;
  1481. o._status.set_have_moved_from(true);
  1482. return *this;
  1483. }
  1484. if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1485. {
  1486. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1487. {
  1488. this->_error.~_error_type_(); // NOLINT
  1489. }
  1490. this->_status = o._status;
  1491. o._status.set_have_moved_from(true);
  1492. return *this;
  1493. }
  1494. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
  1495. {
  1496. move_assign_to_empty<_error_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_error), BOOST_OUTCOME_ADDRESS_OF(o._error));
  1497. this->_status = o._status;
  1498. o._status.set_have_moved_from(true);
  1499. return *this;
  1500. }
  1501. if(this->_status.have_value() && o._status.have_error())
  1502. {
  1503. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1504. {
  1505. this->_value.~_value_type_(); // NOLINT
  1506. }
  1507. move_assign_to_empty<_error_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_error), BOOST_OUTCOME_ADDRESS_OF(o._error));
  1508. this->_status = o._status;
  1509. o._status.set_have_moved_from(true);
  1510. return *this;
  1511. }
  1512. if(this->_status.have_error() && o._status.have_value())
  1513. {
  1514. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1515. {
  1516. this->_error.~_error_type_(); // NOLINT
  1517. }
  1518. move_assign_to_empty<_value_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_value), BOOST_OUTCOME_ADDRESS_OF(o._value));
  1519. this->_status = o._status;
  1520. o._status.set_have_moved_from(true);
  1521. return *this;
  1522. }
  1523. // Should never reach here
  1524. make_ub(this->_value);
  1525. }
  1526. };
  1527. template <class Base> struct value_storage_nontrivial_copy_assignment : Base // NOLINT
  1528. {
  1529. using Base::Base;
  1530. using value_type = typename Base::value_type;
  1531. using error_type = typename Base::error_type;
  1532. value_storage_nontrivial_copy_assignment() = default;
  1533. value_storage_nontrivial_copy_assignment(const value_storage_nontrivial_copy_assignment &) = default;
  1534. value_storage_nontrivial_copy_assignment(value_storage_nontrivial_copy_assignment &&) = default; // NOLINT
  1535. value_storage_nontrivial_copy_assignment &operator=(value_storage_nontrivial_copy_assignment &&o) = default; // NOLINT
  1536. ~value_storage_nontrivial_copy_assignment() = default;
  1537. #if __cplusplus >= 202000L || _HAS_CXX20
  1538. constexpr
  1539. #endif
  1540. value_storage_nontrivial_copy_assignment &
  1541. operator=(const value_storage_nontrivial_copy_assignment &o) noexcept(
  1542. std::is_nothrow_copy_assignable<value_type>::value &&std::is_nothrow_copy_assignable<error_type>::value &&noexcept(copy_assign_to_empty<value_type>(
  1543. static_cast<value_type *>(nullptr), static_cast<value_type *>(nullptr))) &&noexcept(copy_assign_to_empty<error_type>(static_cast<error_type *>(nullptr),
  1544. static_cast<error_type *>(nullptr))))
  1545. {
  1546. using _value_type_ = typename Base::_value_type_;
  1547. using _error_type_ = typename Base::_error_type_;
  1548. if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1549. {
  1550. this->_status = o._status;
  1551. return *this;
  1552. }
  1553. if(this->_status.have_value() && o._status.have_value())
  1554. {
  1555. this->_value = o._value; // NOLINT
  1556. this->_status = o._status;
  1557. return *this;
  1558. }
  1559. if(this->_status.have_error() && o._status.have_error())
  1560. {
  1561. this->_error = o._error; // NOLINT
  1562. this->_status = o._status;
  1563. return *this;
  1564. }
  1565. if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
  1566. {
  1567. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1568. {
  1569. this->_value.~_value_type_(); // NOLINT
  1570. }
  1571. this->_status = o._status;
  1572. return *this;
  1573. }
  1574. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
  1575. {
  1576. copy_assign_to_empty<_value_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_value), BOOST_OUTCOME_ADDRESS_OF(o._value));
  1577. this->_status = o._status;
  1578. return *this;
  1579. }
  1580. if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1581. {
  1582. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1583. {
  1584. this->_error.~_error_type_(); // NOLINT
  1585. }
  1586. this->_status = o._status;
  1587. return *this;
  1588. }
  1589. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
  1590. {
  1591. copy_assign_to_empty<_error_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_error), BOOST_OUTCOME_ADDRESS_OF(o._error));
  1592. this->_status = o._status;
  1593. return *this;
  1594. }
  1595. if(this->_status.have_value() && o._status.have_error())
  1596. {
  1597. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1598. {
  1599. this->_value.~_value_type_(); // NOLINT
  1600. }
  1601. copy_assign_to_empty<_error_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_error), BOOST_OUTCOME_ADDRESS_OF(o._error));
  1602. this->_status = o._status;
  1603. return *this;
  1604. }
  1605. if(this->_status.have_error() && o._status.have_value())
  1606. {
  1607. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1608. {
  1609. this->_error.~_error_type_(); // NOLINT
  1610. }
  1611. copy_assign_to_empty<_value_type_>(BOOST_OUTCOME_ADDRESS_OF(this->_value), BOOST_OUTCOME_ADDRESS_OF(o._value));
  1612. this->_status = o._status;
  1613. return *this;
  1614. }
  1615. // Should never reach here
  1616. make_ub(this->_value);
  1617. }
  1618. };
  1619. #ifdef _MSC_VER
  1620. #pragma warning(pop)
  1621. #endif
  1622. // is_trivially_copyable is true even if type is not copyable, so handle that here
  1623. template <class T> struct is_storage_trivial
  1624. {
  1625. static constexpr bool value = std::is_void<T>::value || (std::is_trivially_copy_constructible<T>::value && std::is_trivially_copyable<T>::value);
  1626. };
  1627. // work around libstdc++ 7 bug
  1628. template <> struct is_storage_trivial<void>
  1629. {
  1630. static constexpr bool value = true;
  1631. };
  1632. template <> struct is_storage_trivial<const void>
  1633. {
  1634. static constexpr bool value = true;
  1635. };
  1636. // Ability to do copy assigns needs more than just copy assignment
  1637. template <class T> struct is_copy_assignable
  1638. {
  1639. static constexpr bool value = std::is_copy_assignable<T>::value && (std::is_copy_constructible<T>::value || std::is_default_constructible<T>::value);
  1640. };
  1641. // Ability to do move assigns needs more than just move assignment
  1642. template <class T> struct is_move_assignable
  1643. {
  1644. static constexpr bool value = std::is_move_assignable<T>::value && (std::is_move_constructible<T>::value || std::is_default_constructible<T>::value);
  1645. };
  1646. template <class T, class E>
  1647. using value_storage_select_trivality =
  1648. std::conditional_t<is_storage_trivial<T>::value && is_storage_trivial<E>::value, value_storage_trivial<T, E>, value_storage_nontrivial<T, E>>;
  1649. template <class T, class E>
  1650. using value_storage_select_move_constructor =
  1651. std::conditional_t<std::is_move_constructible<devoid<T>>::value && std::is_move_constructible<devoid<E>>::value, value_storage_select_trivality<T, E>,
  1652. value_storage_delete_move_constructor<value_storage_select_trivality<T, E>>>;
  1653. template <class T, class E>
  1654. using value_storage_select_copy_constructor =
  1655. std::conditional_t<std::is_copy_constructible<devoid<T>>::value && std::is_copy_constructible<devoid<E>>::value, value_storage_select_move_constructor<T, E>,
  1656. value_storage_delete_copy_constructor<value_storage_select_move_constructor<T, E>>>;
  1657. template <class T, class E>
  1658. using value_storage_select_move_assignment =
  1659. std::conditional_t<std::is_trivially_move_assignable<devoid<T>>::value && std::is_trivially_move_assignable<devoid<E>>::value,
  1660. value_storage_select_copy_constructor<T, E>,
  1661. std::conditional_t<is_move_assignable<devoid<T>>::value && is_move_assignable<devoid<E>>::value,
  1662. value_storage_nontrivial_move_assignment<value_storage_select_copy_constructor<T, E>>,
  1663. value_storage_delete_move_assignment<value_storage_select_copy_constructor<T, E>>>>;
  1664. template <class T, class E>
  1665. using value_storage_select_copy_assignment =
  1666. std::conditional_t<std::is_trivially_copy_assignable<devoid<T>>::value && std::is_trivially_copy_assignable<devoid<E>>::value,
  1667. value_storage_select_move_assignment<T, E>,
  1668. std::conditional_t<is_copy_assignable<devoid<T>>::value && is_copy_assignable<devoid<E>>::value,
  1669. value_storage_nontrivial_copy_assignment<value_storage_select_move_assignment<T, E>>,
  1670. value_storage_delete_copy_assignment<value_storage_select_move_assignment<T, E>>>>;
  1671. template <class T, class E> using value_storage_select_impl = value_storage_select_copy_assignment<T, E>;
  1672. #ifndef NDEBUG
  1673. // Check is trivial in all ways except default constructibility
  1674. // static_assert(std::is_trivial<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivial!");
  1675. // static_assert(std::is_trivially_default_constructible<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially
  1676. // default constructible!");
  1677. static_assert(std::is_trivially_copyable<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially copyable!");
  1678. static_assert(std::is_trivially_assignable<value_storage_select_impl<int, long>, value_storage_select_impl<int, long>>::value,
  1679. "value_storage_select_impl<int, long> is not trivially assignable!");
  1680. static_assert(std::is_trivially_destructible<value_storage_select_impl<int, long>>::value,
  1681. "value_storage_select_impl<int, long> is not trivially destructible!");
  1682. static_assert(std::is_trivially_copy_constructible<value_storage_select_impl<int, long>>::value,
  1683. "value_storage_select_impl<int, long> is not trivially copy constructible!");
  1684. static_assert(std::is_trivially_move_constructible<value_storage_select_impl<int, long>>::value,
  1685. "value_storage_select_impl<int, long> is not trivially move constructible!");
  1686. static_assert(std::is_trivially_copy_assignable<value_storage_select_impl<int, long>>::value,
  1687. "value_storage_select_impl<int, long> is not trivially copy assignable!");
  1688. static_assert(std::is_trivially_move_assignable<value_storage_select_impl<int, long>>::value,
  1689. "value_storage_select_impl<int, long> is not trivially move assignable!");
  1690. // Also check is standard layout
  1691. static_assert(std::is_standard_layout<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not a standard layout type!");
  1692. #endif
  1693. } // namespace detail
  1694. BOOST_OUTCOME_V2_NAMESPACE_END
  1695. #endif