expected.hpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555
  1. // This version targets C++11 and later.
  2. //
  3. // Copyright (C) 2016-2020 Martin Moene.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // expected lite is based on:
  9. // A proposal to add a utility class to represent expected monad
  10. // by Vicente J. Botet Escriba and Pierre Talbot. http:://wg21.link/p0323
  11. #ifndef NONSTD_EXPECTED_LITE_HPP
  12. #define NONSTD_EXPECTED_LITE_HPP
  13. #define expected_lite_MAJOR 0
  14. #define expected_lite_MINOR 8
  15. #define expected_lite_PATCH 0
  16. #define expected_lite_VERSION expected_STRINGIFY(expected_lite_MAJOR) "." expected_STRINGIFY(expected_lite_MINOR) "." expected_STRINGIFY(expected_lite_PATCH)
  17. #define expected_STRINGIFY( x ) expected_STRINGIFY_( x )
  18. #define expected_STRINGIFY_( x ) #x
  19. // expected-lite configuration:
  20. #define nsel_EXPECTED_DEFAULT 0
  21. #define nsel_EXPECTED_NONSTD 1
  22. #define nsel_EXPECTED_STD 2
  23. // tweak header support:
  24. #ifdef __has_include
  25. # if __has_include(<nonstd/expected.tweak.hpp>)
  26. # error #include <nonstd/expected.tweak.hpp>
  27. # endif
  28. #define expected_HAVE_TWEAK_HEADER 1
  29. #else
  30. #define expected_HAVE_TWEAK_HEADER 0
  31. //# pragma message("expected.hpp: Note: Tweak header not supported.")
  32. #endif
  33. // expected selection and configuration:
  34. #if !defined( nsel_CONFIG_SELECT_EXPECTED )
  35. # define nsel_CONFIG_SELECT_EXPECTED ( nsel_HAVE_STD_EXPECTED ? nsel_EXPECTED_STD : nsel_EXPECTED_NONSTD )
  36. #endif
  37. // Proposal revisions:
  38. //
  39. // DXXXXR0: --
  40. // N4015 : -2 (2014-05-26)
  41. // N4109 : -1 (2014-06-29)
  42. // P0323R0: 0 (2016-05-28)
  43. // P0323R1: 1 (2016-10-12)
  44. // -------:
  45. // P0323R2: 2 (2017-06-15)
  46. // P0323R3: 3 (2017-10-15)
  47. // P0323R4: 4 (2017-11-26)
  48. // P0323R5: 5 (2018-02-08)
  49. // P0323R6: 6 (2018-04-02)
  50. // P0323R7: 7 (2018-06-22) *
  51. //
  52. // expected-lite uses 2 and higher
  53. #ifndef nsel_P0323R
  54. # define nsel_P0323R 7
  55. #endif
  56. // Monadic operations proposal revisions:
  57. //
  58. // P2505R0: 0 (2021-12-12)
  59. // P2505R1: 1 (2022-02-10)
  60. // P2505R2: 2 (2022-04-15)
  61. // P2505R3: 3 (2022-06-05)
  62. // P2505R4: 4 (2022-06-15)
  63. // P2505R5: 5 (2022-09-20) *
  64. //
  65. // expected-lite uses 5
  66. #ifndef nsel_P2505R
  67. # define nsel_P2505R 5
  68. #endif
  69. // Control presence of C++ exception handling (try and auto discover):
  70. #ifndef nsel_CONFIG_NO_EXCEPTIONS
  71. # if defined(_MSC_VER)
  72. # include <cstddef> // for _HAS_EXCEPTIONS
  73. # endif
  74. # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
  75. # define nsel_CONFIG_NO_EXCEPTIONS 0
  76. # else
  77. # define nsel_CONFIG_NO_EXCEPTIONS 1
  78. # endif
  79. #endif
  80. // at default use SEH with MSVC for no C++ exceptions
  81. #ifndef nsel_CONFIG_NO_EXCEPTIONS_SEH
  82. # define nsel_CONFIG_NO_EXCEPTIONS_SEH ( nsel_CONFIG_NO_EXCEPTIONS && _MSC_VER )
  83. #endif
  84. // C++ language version detection (C++23 is speculative):
  85. // Note: VC14.0/1900 (VS2015) lacks too much from C++14.
  86. #ifndef nsel_CPLUSPLUS
  87. # if defined(_MSVC_LANG ) && !defined(__clang__)
  88. # define nsel_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
  89. # else
  90. # define nsel_CPLUSPLUS __cplusplus
  91. # endif
  92. #endif
  93. #define nsel_CPP98_OR_GREATER ( nsel_CPLUSPLUS >= 199711L )
  94. #define nsel_CPP11_OR_GREATER ( nsel_CPLUSPLUS >= 201103L )
  95. #define nsel_CPP14_OR_GREATER ( nsel_CPLUSPLUS >= 201402L )
  96. #define nsel_CPP17_OR_GREATER ( nsel_CPLUSPLUS >= 201703L )
  97. #define nsel_CPP20_OR_GREATER ( nsel_CPLUSPLUS >= 202002L )
  98. #define nsel_CPP23_OR_GREATER ( nsel_CPLUSPLUS >= 202300L )
  99. // Use C++23 std::expected if available and requested:
  100. #if nsel_CPP23_OR_GREATER && defined(__has_include )
  101. # if __has_include( <expected> )
  102. # define nsel_HAVE_STD_EXPECTED 1
  103. # else
  104. # define nsel_HAVE_STD_EXPECTED 0
  105. # endif
  106. #else
  107. # define nsel_HAVE_STD_EXPECTED 0
  108. #endif
  109. #define nsel_USES_STD_EXPECTED ( (nsel_CONFIG_SELECT_EXPECTED == nsel_EXPECTED_STD) || ((nsel_CONFIG_SELECT_EXPECTED == nsel_EXPECTED_DEFAULT) && nsel_HAVE_STD_EXPECTED) )
  110. //
  111. // in_place: code duplicated in any-lite, expected-lite, expected-lite, value-ptr-lite, variant-lite:
  112. //
  113. #ifndef nonstd_lite_HAVE_IN_PLACE_TYPES
  114. #define nonstd_lite_HAVE_IN_PLACE_TYPES 1
  115. // C++17 std::in_place in <utility>:
  116. #if nsel_CPP17_OR_GREATER
  117. #include <utility>
  118. namespace nonstd {
  119. using std::in_place;
  120. using std::in_place_type;
  121. using std::in_place_index;
  122. using std::in_place_t;
  123. using std::in_place_type_t;
  124. using std::in_place_index_t;
  125. #define nonstd_lite_in_place_t( T) std::in_place_t
  126. #define nonstd_lite_in_place_type_t( T) std::in_place_type_t<T>
  127. #define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K>
  128. #define nonstd_lite_in_place( T) std::in_place_t{}
  129. #define nonstd_lite_in_place_type( T) std::in_place_type_t<T>{}
  130. #define nonstd_lite_in_place_index(K) std::in_place_index_t<K>{}
  131. } // namespace nonstd
  132. #else // nsel_CPP17_OR_GREATER
  133. #include <cstddef>
  134. namespace nonstd {
  135. namespace detail {
  136. template< class T >
  137. struct in_place_type_tag {};
  138. template< std::size_t K >
  139. struct in_place_index_tag {};
  140. } // namespace detail
  141. struct in_place_t {};
  142. template< class T >
  143. inline in_place_t in_place( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
  144. {
  145. return in_place_t();
  146. }
  147. template< std::size_t K >
  148. inline in_place_t in_place( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
  149. {
  150. return in_place_t();
  151. }
  152. template< class T >
  153. inline in_place_t in_place_type( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
  154. {
  155. return in_place_t();
  156. }
  157. template< std::size_t K >
  158. inline in_place_t in_place_index( detail::in_place_index_tag<K> = detail::in_place_index_tag<K>() )
  159. {
  160. return in_place_t();
  161. }
  162. // mimic templated typedef:
  163. #define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
  164. #define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> )
  165. #define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<K> )
  166. #define nonstd_lite_in_place( T) nonstd::in_place_type<T>
  167. #define nonstd_lite_in_place_type( T) nonstd::in_place_type<T>
  168. #define nonstd_lite_in_place_index(K) nonstd::in_place_index<K>
  169. } // namespace nonstd
  170. #endif // nsel_CPP17_OR_GREATER
  171. #endif // nonstd_lite_HAVE_IN_PLACE_TYPES
  172. //
  173. // Using std::expected:
  174. //
  175. #if nsel_USES_STD_EXPECTED
  176. #error #include <expected>
  177. namespace nonstd {
  178. using std::expected;
  179. using std::unexpected;
  180. using std::bad_expected_access;
  181. using std::unexpect_t;
  182. using std::unexpect;
  183. //[[deprecated("replace unexpected_type with unexpected")]]
  184. template< typename E >
  185. using unexpected_type = unexpected<E>;
  186. // Unconditionally provide make_unexpected():
  187. template< typename E>
  188. constexpr auto make_unexpected( E && value ) -> unexpected< typename std::decay<E>::type >
  189. {
  190. return unexpected< typename std::decay<E>::type >( std::forward<E>(value) );
  191. }
  192. } // namespace nonstd
  193. #else // nsel_USES_STD_EXPECTED
  194. #include <cassert>
  195. #include <exception>
  196. #include <functional>
  197. #include <initializer_list>
  198. #include <memory>
  199. #include <new>
  200. #include <system_error>
  201. #include <type_traits>
  202. #include <utility>
  203. // additional includes:
  204. #if nsel_CONFIG_NO_EXCEPTIONS
  205. # if nsel_CONFIG_NO_EXCEPTIONS_SEH
  206. # include <windows.h> // for ExceptionCodes
  207. # else
  208. // already included: <cassert>
  209. # endif
  210. #else
  211. # include <stdexcept>
  212. #endif
  213. // C++ feature usage:
  214. #if nsel_CPP11_OR_GREATER
  215. # define nsel_constexpr constexpr
  216. #else
  217. # define nsel_constexpr /*constexpr*/
  218. #endif
  219. #if nsel_CPP14_OR_GREATER
  220. # define nsel_constexpr14 constexpr
  221. #else
  222. # define nsel_constexpr14 /*constexpr*/
  223. #endif
  224. #if nsel_CPP17_OR_GREATER
  225. # define nsel_inline17 inline
  226. #else
  227. # define nsel_inline17 /*inline*/
  228. #endif
  229. // Compiler versions:
  230. //
  231. // MSVC++ 6.0 _MSC_VER == 1200 nsel_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
  232. // MSVC++ 7.0 _MSC_VER == 1300 nsel_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
  233. // MSVC++ 7.1 _MSC_VER == 1310 nsel_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
  234. // MSVC++ 8.0 _MSC_VER == 1400 nsel_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
  235. // MSVC++ 9.0 _MSC_VER == 1500 nsel_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
  236. // MSVC++ 10.0 _MSC_VER == 1600 nsel_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
  237. // MSVC++ 11.0 _MSC_VER == 1700 nsel_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
  238. // MSVC++ 12.0 _MSC_VER == 1800 nsel_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
  239. // MSVC++ 14.0 _MSC_VER == 1900 nsel_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
  240. // MSVC++ 14.1 _MSC_VER >= 1910 nsel_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
  241. // MSVC++ 14.2 _MSC_VER >= 1920 nsel_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
  242. #if defined(_MSC_VER) && !defined(__clang__)
  243. # define nsel_COMPILER_MSVC_VER (_MSC_VER )
  244. # define nsel_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900)) )
  245. #else
  246. # define nsel_COMPILER_MSVC_VER 0
  247. # define nsel_COMPILER_MSVC_VERSION 0
  248. #endif
  249. #define nsel_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
  250. #if defined(__clang__)
  251. # define nsel_COMPILER_CLANG_VERSION nsel_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
  252. #else
  253. # define nsel_COMPILER_CLANG_VERSION 0
  254. #endif
  255. #if defined(__GNUC__) && !defined(__clang__)
  256. # define nsel_COMPILER_GNUC_VERSION nsel_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  257. #else
  258. # define nsel_COMPILER_GNUC_VERSION 0
  259. #endif
  260. // half-open range [lo..hi):
  261. //#define nsel_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
  262. // Method enabling
  263. #define nsel_REQUIRES_0(...) \
  264. template< bool B = (__VA_ARGS__), typename std::enable_if<B, int>::type = 0 >
  265. #define nsel_REQUIRES_T(...) \
  266. , typename std::enable_if< (__VA_ARGS__), int >::type = 0
  267. #define nsel_REQUIRES_R(R, ...) \
  268. typename std::enable_if< (__VA_ARGS__), R>::type
  269. #define nsel_REQUIRES_A(...) \
  270. , typename std::enable_if< (__VA_ARGS__), void*>::type = nullptr
  271. // Clang, GNUC, MSVC warning suppression macros:
  272. #ifdef __clang__
  273. # pragma clang diagnostic push
  274. #elif defined __GNUC__
  275. # pragma GCC diagnostic push
  276. #endif // __clang__
  277. #if nsel_COMPILER_MSVC_VERSION >= 140
  278. # define nsel_DISABLE_MSVC_WARNINGS(codes) __pragma( warning(push) ) __pragma( warning(disable: codes) )
  279. #else
  280. # define nsel_DISABLE_MSVC_WARNINGS(codes)
  281. #endif
  282. #ifdef __clang__
  283. # define nsel_RESTORE_WARNINGS() _Pragma("clang diagnostic pop")
  284. # define nsel_RESTORE_MSVC_WARNINGS()
  285. #elif defined __GNUC__
  286. # define nsel_RESTORE_WARNINGS() _Pragma("GCC diagnostic pop")
  287. # define nsel_RESTORE_MSVC_WARNINGS()
  288. #elif nsel_COMPILER_MSVC_VERSION >= 140
  289. # define nsel_RESTORE_WARNINGS() __pragma( warning( pop ) )
  290. # define nsel_RESTORE_MSVC_WARNINGS() nsel_RESTORE_WARNINGS()
  291. #else
  292. # define nsel_RESTORE_WARNINGS()
  293. # define nsel_RESTORE_MSVC_WARNINGS()
  294. #endif
  295. // Suppress the following MSVC (GSL) warnings:
  296. // - C26409: Avoid calling new and delete explicitly, use std::make_unique<T> instead (r.11)
  297. nsel_DISABLE_MSVC_WARNINGS( 26409 )
  298. // Presence of language and library features:
  299. #ifdef _HAS_CPP0X
  300. # define nsel_HAS_CPP0X _HAS_CPP0X
  301. #else
  302. # define nsel_HAS_CPP0X 0
  303. #endif
  304. // Presence of language and library features:
  305. #define nsel_CPP17_000 (nsel_CPP17_OR_GREATER)
  306. // Presence of C++17 language features:
  307. #define nsel_HAVE_DEPRECATED nsel_CPP17_000
  308. // C++ feature usage:
  309. #if nsel_HAVE_DEPRECATED
  310. # define nsel_deprecated(msg) [[deprecated(msg)]]
  311. #else
  312. # define nsel_deprecated(msg) /*[[deprecated]]*/
  313. #endif
  314. //
  315. // expected:
  316. //
  317. namespace nonstd { namespace expected_lite {
  318. // type traits C++17:
  319. namespace std17 {
  320. #if nsel_CPP17_OR_GREATER
  321. using std::conjunction;
  322. using std::is_swappable;
  323. using std::is_nothrow_swappable;
  324. #else // nsel_CPP17_OR_GREATER
  325. namespace detail {
  326. using std::swap;
  327. struct is_swappable
  328. {
  329. template< typename T, typename = decltype( swap( std::declval<T&>(), std::declval<T&>() ) ) >
  330. static std::true_type test( int /* unused */);
  331. template< typename >
  332. static std::false_type test(...);
  333. };
  334. struct is_nothrow_swappable
  335. {
  336. // wrap noexcept(expr) in separate function as work-around for VC140 (VS2015):
  337. template< typename T >
  338. static constexpr bool satisfies()
  339. {
  340. return noexcept( swap( std::declval<T&>(), std::declval<T&>() ) );
  341. }
  342. template< typename T >
  343. static auto test( int ) -> std::integral_constant<bool, satisfies<T>()>{}
  344. template< typename >
  345. static auto test(...) -> std::false_type;
  346. };
  347. } // namespace detail
  348. // is [nothrow] swappable:
  349. template< typename T >
  350. struct is_swappable : decltype( detail::is_swappable::test<T>(0) ){};
  351. template< typename T >
  352. struct is_nothrow_swappable : decltype( detail::is_nothrow_swappable::test<T>(0) ){};
  353. // conjunction:
  354. template< typename... > struct conjunction : std::true_type{};
  355. template< typename B1 > struct conjunction<B1> : B1{};
  356. template< typename B1, typename... Bn >
  357. struct conjunction<B1, Bn...> : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type{};
  358. #endif // nsel_CPP17_OR_GREATER
  359. } // namespace std17
  360. // type traits C++20:
  361. namespace std20 {
  362. #if defined(__cpp_lib_remove_cvref)
  363. using std::remove_cvref;
  364. #else
  365. template< typename T >
  366. struct remove_cvref
  367. {
  368. typedef typename std::remove_cv< typename std::remove_reference<T>::type >::type type;
  369. };
  370. #endif
  371. } // namespace std20
  372. // forward declaration:
  373. template< typename T, typename E >
  374. class expected;
  375. namespace detail {
  376. #if nsel_P2505R >= 3
  377. template< typename T >
  378. struct is_expected : std::false_type {};
  379. template< typename T, typename E >
  380. struct is_expected< expected< T, E > > : std::true_type {};
  381. #endif // nsel_P2505R >= 3
  382. /// discriminated union to hold value or 'error'.
  383. template< typename T, typename E >
  384. class storage_t_noncopy_nonmove_impl
  385. {
  386. template< typename, typename > friend class nonstd::expected_lite::expected;
  387. public:
  388. using value_type = T;
  389. using error_type = E;
  390. // no-op construction
  391. storage_t_noncopy_nonmove_impl() {}
  392. ~storage_t_noncopy_nonmove_impl() {}
  393. explicit storage_t_noncopy_nonmove_impl( bool has_value )
  394. : m_has_value( has_value )
  395. {}
  396. void construct_value()
  397. {
  398. new( &m_value ) value_type();
  399. }
  400. // void construct_value( value_type const & e )
  401. // {
  402. // new( &m_value ) value_type( e );
  403. // }
  404. // void construct_value( value_type && e )
  405. // {
  406. // new( &m_value ) value_type( std::move( e ) );
  407. // }
  408. template< class... Args >
  409. void emplace_value( Args&&... args )
  410. {
  411. new( &m_value ) value_type( std::forward<Args>(args)...);
  412. }
  413. template< class U, class... Args >
  414. void emplace_value( std::initializer_list<U> il, Args&&... args )
  415. {
  416. new( &m_value ) value_type( il, std::forward<Args>(args)... );
  417. }
  418. void destruct_value()
  419. {
  420. m_value.~value_type();
  421. }
  422. // void construct_error( error_type const & e )
  423. // {
  424. // // new( &m_error ) error_type( e );
  425. // }
  426. // void construct_error( error_type && e )
  427. // {
  428. // // new( &m_error ) error_type( std::move( e ) );
  429. // }
  430. template< class... Args >
  431. void emplace_error( Args&&... args )
  432. {
  433. new( &m_error ) error_type( std::forward<Args>(args)...);
  434. }
  435. template< class U, class... Args >
  436. void emplace_error( std::initializer_list<U> il, Args&&... args )
  437. {
  438. new( &m_error ) error_type( il, std::forward<Args>(args)... );
  439. }
  440. void destruct_error()
  441. {
  442. m_error.~error_type();
  443. }
  444. constexpr value_type const & value() const &
  445. {
  446. return m_value;
  447. }
  448. value_type & value() &
  449. {
  450. return m_value;
  451. }
  452. constexpr value_type const && value() const &&
  453. {
  454. return std::move( m_value );
  455. }
  456. nsel_constexpr14 value_type && value() &&
  457. {
  458. return std::move( m_value );
  459. }
  460. value_type const * value_ptr() const
  461. {
  462. return &m_value;
  463. }
  464. value_type * value_ptr()
  465. {
  466. return &m_value;
  467. }
  468. error_type const & error() const &
  469. {
  470. return m_error;
  471. }
  472. error_type & error() &
  473. {
  474. return m_error;
  475. }
  476. constexpr error_type const && error() const &&
  477. {
  478. return std::move( m_error );
  479. }
  480. nsel_constexpr14 error_type && error() &&
  481. {
  482. return std::move( m_error );
  483. }
  484. bool has_value() const
  485. {
  486. return m_has_value;
  487. }
  488. void set_has_value( bool v )
  489. {
  490. m_has_value = v;
  491. }
  492. private:
  493. union
  494. {
  495. value_type m_value;
  496. error_type m_error;
  497. };
  498. bool m_has_value = false;
  499. };
  500. template< typename T, typename E >
  501. class storage_t_impl
  502. {
  503. template< typename, typename > friend class nonstd::expected_lite::expected;
  504. public:
  505. using value_type = T;
  506. using error_type = E;
  507. // no-op construction
  508. storage_t_impl() {}
  509. ~storage_t_impl() {}
  510. explicit storage_t_impl( bool has_value )
  511. : m_has_value( has_value )
  512. {}
  513. void construct_value()
  514. {
  515. new( &m_value ) value_type();
  516. }
  517. void construct_value( value_type const & e )
  518. {
  519. new( &m_value ) value_type( e );
  520. }
  521. void construct_value( value_type && e )
  522. {
  523. new( &m_value ) value_type( std::move( e ) );
  524. }
  525. template< class... Args >
  526. void emplace_value( Args&&... args )
  527. {
  528. new( &m_value ) value_type( std::forward<Args>(args)...);
  529. }
  530. template< class U, class... Args >
  531. void emplace_value( std::initializer_list<U> il, Args&&... args )
  532. {
  533. new( &m_value ) value_type( il, std::forward<Args>(args)... );
  534. }
  535. void destruct_value()
  536. {
  537. m_value.~value_type();
  538. }
  539. void construct_error( error_type const & e )
  540. {
  541. new( &m_error ) error_type( e );
  542. }
  543. void construct_error( error_type && e )
  544. {
  545. new( &m_error ) error_type( std::move( e ) );
  546. }
  547. template< class... Args >
  548. void emplace_error( Args&&... args )
  549. {
  550. new( &m_error ) error_type( std::forward<Args>(args)...);
  551. }
  552. template< class U, class... Args >
  553. void emplace_error( std::initializer_list<U> il, Args&&... args )
  554. {
  555. new( &m_error ) error_type( il, std::forward<Args>(args)... );
  556. }
  557. void destruct_error()
  558. {
  559. m_error.~error_type();
  560. }
  561. constexpr value_type const & value() const &
  562. {
  563. return m_value;
  564. }
  565. value_type & value() &
  566. {
  567. return m_value;
  568. }
  569. constexpr value_type const && value() const &&
  570. {
  571. return std::move( m_value );
  572. }
  573. nsel_constexpr14 value_type && value() &&
  574. {
  575. return std::move( m_value );
  576. }
  577. value_type const * value_ptr() const
  578. {
  579. return &m_value;
  580. }
  581. value_type * value_ptr()
  582. {
  583. return &m_value;
  584. }
  585. error_type const & error() const &
  586. {
  587. return m_error;
  588. }
  589. error_type & error() &
  590. {
  591. return m_error;
  592. }
  593. constexpr error_type const && error() const &&
  594. {
  595. return std::move( m_error );
  596. }
  597. nsel_constexpr14 error_type && error() &&
  598. {
  599. return std::move( m_error );
  600. }
  601. bool has_value() const
  602. {
  603. return m_has_value;
  604. }
  605. void set_has_value( bool v )
  606. {
  607. m_has_value = v;
  608. }
  609. private:
  610. union
  611. {
  612. value_type m_value;
  613. error_type m_error;
  614. };
  615. bool m_has_value = false;
  616. };
  617. /// discriminated union to hold only 'error'.
  618. template< typename E >
  619. struct storage_t_impl<void, E>
  620. {
  621. template< typename, typename > friend class nonstd::expected_lite::expected;
  622. public:
  623. using value_type = void;
  624. using error_type = E;
  625. // no-op construction
  626. storage_t_impl() {}
  627. ~storage_t_impl() {}
  628. explicit storage_t_impl( bool has_value )
  629. : m_has_value( has_value )
  630. {}
  631. void construct_error( error_type const & e )
  632. {
  633. new( &m_error ) error_type( e );
  634. }
  635. void construct_error( error_type && e )
  636. {
  637. new( &m_error ) error_type( std::move( e ) );
  638. }
  639. template< class... Args >
  640. void emplace_error( Args&&... args )
  641. {
  642. new( &m_error ) error_type( std::forward<Args>(args)...);
  643. }
  644. template< class U, class... Args >
  645. void emplace_error( std::initializer_list<U> il, Args&&... args )
  646. {
  647. new( &m_error ) error_type( il, std::forward<Args>(args)... );
  648. }
  649. void destruct_error()
  650. {
  651. m_error.~error_type();
  652. }
  653. error_type const & error() const &
  654. {
  655. return m_error;
  656. }
  657. error_type & error() &
  658. {
  659. return m_error;
  660. }
  661. constexpr error_type const && error() const &&
  662. {
  663. return std::move( m_error );
  664. }
  665. nsel_constexpr14 error_type && error() &&
  666. {
  667. return std::move( m_error );
  668. }
  669. bool has_value() const
  670. {
  671. return m_has_value;
  672. }
  673. void set_has_value( bool v )
  674. {
  675. m_has_value = v;
  676. }
  677. private:
  678. union
  679. {
  680. char m_dummy;
  681. error_type m_error;
  682. };
  683. bool m_has_value = false;
  684. };
  685. template< typename T, typename E, bool isConstructable, bool isMoveable >
  686. class storage_t
  687. {
  688. public:
  689. };
  690. template< typename T, typename E >
  691. class storage_t<T, E, false, false> : public storage_t_noncopy_nonmove_impl<T, E>
  692. {
  693. public:
  694. storage_t() = default;
  695. ~storage_t() = default;
  696. explicit storage_t( bool has_value )
  697. : storage_t_noncopy_nonmove_impl<T, E>( has_value )
  698. {}
  699. storage_t( storage_t const & other ) = delete;
  700. storage_t( storage_t && other ) = delete;
  701. };
  702. template< typename T, typename E >
  703. class storage_t<T, E, true, true> : public storage_t_impl<T, E>
  704. {
  705. public:
  706. storage_t() = default;
  707. ~storage_t() = default;
  708. explicit storage_t( bool has_value )
  709. : storage_t_impl<T, E>( has_value )
  710. {}
  711. storage_t( storage_t const & other )
  712. : storage_t_impl<T, E>( other.has_value() )
  713. {
  714. if ( this->has_value() ) this->construct_value( other.value() );
  715. else this->construct_error( other.error() );
  716. }
  717. storage_t(storage_t && other )
  718. : storage_t_impl<T, E>( other.has_value() )
  719. {
  720. if ( this->has_value() ) this->construct_value( std::move( other.value() ) );
  721. else this->construct_error( std::move( other.error() ) );
  722. }
  723. };
  724. template< typename E >
  725. class storage_t<void, E, true, true> : public storage_t_impl<void, E>
  726. {
  727. public:
  728. storage_t() = default;
  729. ~storage_t() = default;
  730. explicit storage_t( bool has_value )
  731. : storage_t_impl<void, E>( has_value )
  732. {}
  733. storage_t( storage_t const & other )
  734. : storage_t_impl<void, E>( other.has_value() )
  735. {
  736. if ( this->has_value() ) ;
  737. else this->construct_error( other.error() );
  738. }
  739. storage_t(storage_t && other )
  740. : storage_t_impl<void, E>( other.has_value() )
  741. {
  742. if ( this->has_value() ) ;
  743. else this->construct_error( std::move( other.error() ) );
  744. }
  745. };
  746. template< typename T, typename E >
  747. class storage_t<T, E, true, false> : public storage_t_impl<T, E>
  748. {
  749. public:
  750. storage_t() = default;
  751. ~storage_t() = default;
  752. explicit storage_t( bool has_value )
  753. : storage_t_impl<T, E>( has_value )
  754. {}
  755. storage_t( storage_t const & other )
  756. : storage_t_impl<T, E>(other.has_value())
  757. {
  758. if ( this->has_value() ) this->construct_value( other.value() );
  759. else this->construct_error( other.error() );
  760. }
  761. storage_t( storage_t && other ) = delete;
  762. };
  763. template< typename E >
  764. class storage_t<void, E, true, false> : public storage_t_impl<void, E>
  765. {
  766. public:
  767. storage_t() = default;
  768. ~storage_t() = default;
  769. explicit storage_t( bool has_value )
  770. : storage_t_impl<void, E>( has_value )
  771. {}
  772. storage_t( storage_t const & other )
  773. : storage_t_impl<void, E>(other.has_value())
  774. {
  775. if ( this->has_value() ) ;
  776. else this->construct_error( other.error() );
  777. }
  778. storage_t( storage_t && other ) = delete;
  779. };
  780. template< typename T, typename E >
  781. class storage_t<T, E, false, true> : public storage_t_impl<T, E>
  782. {
  783. public:
  784. storage_t() = default;
  785. ~storage_t() = default;
  786. explicit storage_t( bool has_value )
  787. : storage_t_impl<T, E>( has_value )
  788. {}
  789. storage_t( storage_t const & other ) = delete;
  790. storage_t( storage_t && other )
  791. : storage_t_impl<T, E>( other.has_value() )
  792. {
  793. if ( this->has_value() ) this->construct_value( std::move( other.value() ) );
  794. else this->construct_error( std::move( other.error() ) );
  795. }
  796. };
  797. template< typename E >
  798. class storage_t<void, E, false, true> : public storage_t_impl<void, E>
  799. {
  800. public:
  801. storage_t() = default;
  802. ~storage_t() = default;
  803. explicit storage_t( bool has_value )
  804. : storage_t_impl<void, E>( has_value )
  805. {}
  806. storage_t( storage_t const & other ) = delete;
  807. storage_t( storage_t && other )
  808. : storage_t_impl<void, E>( other.has_value() )
  809. {
  810. if ( this->has_value() ) ;
  811. else this->construct_error( std::move( other.error() ) );
  812. }
  813. };
  814. #if nsel_P2505R >= 3
  815. // C++11 invoke implementation
  816. template< typename >
  817. struct is_reference_wrapper : std::false_type {};
  818. template< typename T >
  819. struct is_reference_wrapper< std::reference_wrapper< T > > : std::true_type {};
  820. template< typename FnT, typename ClassT, typename ObjectT, typename... Args
  821. nsel_REQUIRES_T(
  822. std::is_function<FnT>::value
  823. && ( std::is_same< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  824. || std::is_base_of< ClassT, typename std20::remove_cvref< ObjectT >::type >::value )
  825. )
  826. >
  827. nsel_constexpr auto invoke_member_function_impl( FnT ClassT::* memfnptr, ObjectT && obj, Args && ... args )
  828. noexcept( noexcept( (std::forward< ObjectT >( obj ).*memfnptr)( std::forward< Args >( args )... ) ) )
  829. -> decltype( (std::forward< ObjectT >( obj ).*memfnptr)( std::forward< Args >( args )...) )
  830. {
  831. return (std::forward< ObjectT >( obj ).*memfnptr)( std::forward< Args >( args )... );
  832. }
  833. template< typename FnT, typename ClassT, typename ObjectT, typename... Args
  834. nsel_REQUIRES_T(
  835. std::is_function<FnT>::value
  836. && is_reference_wrapper< typename std20::remove_cvref< ObjectT >::type >::value
  837. )
  838. >
  839. nsel_constexpr auto invoke_member_function_impl( FnT ClassT::* memfnptr, ObjectT && obj, Args && ... args )
  840. noexcept( noexcept( (obj.get().*memfnptr)( std::forward< Args >( args ) ... ) ) )
  841. -> decltype( (obj.get().*memfnptr)( std::forward< Args >( args ) ... ) )
  842. {
  843. return (obj.get().*memfnptr)( std::forward< Args >( args ) ... );
  844. }
  845. template< typename FnT, typename ClassT, typename ObjectT, typename... Args
  846. nsel_REQUIRES_T(
  847. std::is_function<FnT>::value
  848. && !std::is_same< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  849. && !std::is_base_of< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  850. && !is_reference_wrapper< typename std20::remove_cvref< ObjectT >::type >::value
  851. )
  852. >
  853. nsel_constexpr auto invoke_member_function_impl( FnT ClassT::* memfnptr, ObjectT && obj, Args && ... args )
  854. noexcept( noexcept( ((*std::forward< ObjectT >( obj )).*memfnptr)( std::forward< Args >( args ) ... ) ) )
  855. -> decltype( ((*std::forward< ObjectT >( obj )).*memfnptr)( std::forward< Args >( args ) ... ) )
  856. {
  857. return ((*std::forward<ObjectT>(obj)).*memfnptr)( std::forward< Args >( args ) ... );
  858. }
  859. template< typename MemberT, typename ClassT, typename ObjectT
  860. nsel_REQUIRES_T(
  861. std::is_same< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  862. || std::is_base_of< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  863. )
  864. >
  865. nsel_constexpr auto invoke_member_object_impl( MemberT ClassT::* memobjptr, ObjectT && obj )
  866. noexcept( noexcept( std::forward< ObjectT >( obj ).*memobjptr ) )
  867. -> decltype( std::forward< ObjectT >( obj ).*memobjptr )
  868. {
  869. return std::forward< ObjectT >( obj ).*memobjptr;
  870. }
  871. template< typename MemberT, typename ClassT, typename ObjectT
  872. nsel_REQUIRES_T(
  873. is_reference_wrapper< typename std20::remove_cvref< ObjectT >::type >::value
  874. )
  875. >
  876. nsel_constexpr auto invoke_member_object_impl( MemberT ClassT::* memobjptr, ObjectT && obj )
  877. noexcept( noexcept( obj.get().*memobjptr ) )
  878. -> decltype( obj.get().*memobjptr )
  879. {
  880. return obj.get().*memobjptr;
  881. }
  882. template< typename MemberT, typename ClassT, typename ObjectT
  883. nsel_REQUIRES_T(
  884. !std::is_same< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  885. && !std::is_base_of< ClassT, typename std20::remove_cvref< ObjectT >::type >::value
  886. && !is_reference_wrapper< typename std20::remove_cvref< ObjectT >::type >::value
  887. )
  888. >
  889. nsel_constexpr auto invoke_member_object_impl( MemberT ClassT::* memobjptr, ObjectT && obj )
  890. noexcept( noexcept( (*std::forward< ObjectT >( obj )).*memobjptr ) )
  891. -> decltype( (*std::forward< ObjectT >( obj )).*memobjptr )
  892. {
  893. return (*std::forward< ObjectT >( obj )).*memobjptr;
  894. }
  895. template< typename F, typename... Args
  896. nsel_REQUIRES_T(
  897. std::is_member_function_pointer< typename std20::remove_cvref< F >::type >::value
  898. )
  899. >
  900. nsel_constexpr auto invoke( F && f, Args && ... args )
  901. noexcept( noexcept( invoke_member_function_impl( std::forward< F >( f ), std::forward< Args >( args ) ... ) ) )
  902. -> decltype( invoke_member_function_impl( std::forward< F >( f ), std::forward< Args >( args ) ... ) )
  903. {
  904. return invoke_member_function_impl( std::forward< F >( f ), std::forward< Args >( args ) ... );
  905. }
  906. template< typename F, typename... Args
  907. nsel_REQUIRES_T(
  908. std::is_member_object_pointer< typename std20::remove_cvref< F >::type >::value
  909. )
  910. >
  911. nsel_constexpr auto invoke( F && f, Args && ... args )
  912. noexcept( noexcept( invoke_member_object_impl( std::forward< F >( f ), std::forward< Args >( args ) ... ) ) )
  913. -> decltype( invoke_member_object_impl( std::forward< F >( f ), std::forward< Args >( args ) ... ) )
  914. {
  915. return invoke_member_object_impl( std::forward< F >( f ), std::forward< Args >( args ) ... );
  916. }
  917. template< typename F, typename... Args
  918. nsel_REQUIRES_T(
  919. !std::is_member_function_pointer< typename std20::remove_cvref< F >::type >::value
  920. && !std::is_member_object_pointer< typename std20::remove_cvref< F >::type >::value
  921. )
  922. >
  923. nsel_constexpr auto invoke( F && f, Args && ... args )
  924. noexcept( noexcept( std::forward< F >( f )( std::forward< Args >( args ) ... ) ) )
  925. -> decltype( std::forward< F >( f )( std::forward< Args >( args ) ... ) )
  926. {
  927. return std::forward< F >( f )( std::forward< Args >( args ) ... );
  928. }
  929. template< typename F, typename ... Args >
  930. using invoke_result_nocvref_t = typename std20::remove_cvref< decltype( invoke( std::declval< F >(), std::declval< Args >()... ) ) >::type;
  931. #if nsel_P2505R >= 5
  932. template< typename F, typename ... Args >
  933. using transform_invoke_result_t = typename std::remove_cv< decltype( invoke( std::declval< F >(), std::declval< Args >()... ) ) >::type;
  934. #else
  935. template< typename F, typename ... Args >
  936. using transform_invoke_result_t = invoke_result_nocvref_t
  937. #endif // nsel_P2505R >= 5
  938. template< typename T >
  939. struct valid_expected_value_type : std::integral_constant< bool, std::is_destructible< T >::value && !std::is_reference< T >::value && !std::is_array< T >::value > {};
  940. #endif // nsel_P2505R >= 3
  941. } // namespace detail
  942. /// x.x.5 Unexpected object type; unexpected_type; C++17 and later can also use aliased type unexpected.
  943. #if nsel_P0323R <= 2
  944. template< typename E = std::exception_ptr >
  945. class unexpected_type
  946. #else
  947. template< typename E >
  948. class unexpected_type
  949. #endif // nsel_P0323R
  950. {
  951. public:
  952. using error_type = E;
  953. // x.x.5.2.1 Constructors
  954. // unexpected_type() = delete;
  955. constexpr unexpected_type( unexpected_type const & ) = default;
  956. constexpr unexpected_type( unexpected_type && ) = default;
  957. template< typename... Args
  958. nsel_REQUIRES_T(
  959. std::is_constructible<E, Args&&...>::value
  960. )
  961. >
  962. constexpr explicit unexpected_type( nonstd_lite_in_place_t(E), Args &&... args )
  963. : m_error( std::forward<Args>( args )...)
  964. {}
  965. template< typename U, typename... Args
  966. nsel_REQUIRES_T(
  967. std::is_constructible<E, std::initializer_list<U>, Args&&...>::value
  968. )
  969. >
  970. constexpr explicit unexpected_type( nonstd_lite_in_place_t(E), std::initializer_list<U> il, Args &&... args )
  971. : m_error( il, std::forward<Args>( args )...)
  972. {}
  973. template< typename E2
  974. nsel_REQUIRES_T(
  975. std::is_constructible<E,E2>::value
  976. && !std::is_same< typename std20::remove_cvref<E2>::type, nonstd_lite_in_place_t(E2) >::value
  977. && !std::is_same< typename std20::remove_cvref<E2>::type, unexpected_type >::value
  978. )
  979. >
  980. constexpr explicit unexpected_type( E2 && error )
  981. : m_error( std::forward<E2>( error ) )
  982. {}
  983. template< typename E2
  984. nsel_REQUIRES_T(
  985. std::is_constructible< E, E2>::value
  986. && !std::is_constructible<E, unexpected_type<E2> & >::value
  987. && !std::is_constructible<E, unexpected_type<E2> >::value
  988. && !std::is_constructible<E, unexpected_type<E2> const & >::value
  989. && !std::is_constructible<E, unexpected_type<E2> const >::value
  990. && !std::is_convertible< unexpected_type<E2> &, E>::value
  991. && !std::is_convertible< unexpected_type<E2> , E>::value
  992. && !std::is_convertible< unexpected_type<E2> const &, E>::value
  993. && !std::is_convertible< unexpected_type<E2> const , E>::value
  994. && !std::is_convertible< E2 const &, E>::value /*=> explicit */
  995. )
  996. >
  997. constexpr explicit unexpected_type( unexpected_type<E2> const & error )
  998. : m_error( E{ error.error() } )
  999. {}
  1000. template< typename E2
  1001. nsel_REQUIRES_T(
  1002. std::is_constructible< E, E2>::value
  1003. && !std::is_constructible<E, unexpected_type<E2> & >::value
  1004. && !std::is_constructible<E, unexpected_type<E2> >::value
  1005. && !std::is_constructible<E, unexpected_type<E2> const & >::value
  1006. && !std::is_constructible<E, unexpected_type<E2> const >::value
  1007. && !std::is_convertible< unexpected_type<E2> &, E>::value
  1008. && !std::is_convertible< unexpected_type<E2> , E>::value
  1009. && !std::is_convertible< unexpected_type<E2> const &, E>::value
  1010. && !std::is_convertible< unexpected_type<E2> const , E>::value
  1011. && std::is_convertible< E2 const &, E>::value /*=> explicit */
  1012. )
  1013. >
  1014. constexpr /*non-explicit*/ unexpected_type( unexpected_type<E2> const & error )
  1015. : m_error( error.error() )
  1016. {}
  1017. template< typename E2
  1018. nsel_REQUIRES_T(
  1019. std::is_constructible< E, E2>::value
  1020. && !std::is_constructible<E, unexpected_type<E2> & >::value
  1021. && !std::is_constructible<E, unexpected_type<E2> >::value
  1022. && !std::is_constructible<E, unexpected_type<E2> const & >::value
  1023. && !std::is_constructible<E, unexpected_type<E2> const >::value
  1024. && !std::is_convertible< unexpected_type<E2> &, E>::value
  1025. && !std::is_convertible< unexpected_type<E2> , E>::value
  1026. && !std::is_convertible< unexpected_type<E2> const &, E>::value
  1027. && !std::is_convertible< unexpected_type<E2> const , E>::value
  1028. && !std::is_convertible< E2 const &, E>::value /*=> explicit */
  1029. )
  1030. >
  1031. constexpr explicit unexpected_type( unexpected_type<E2> && error )
  1032. : m_error( E{ std::move( error.error() ) } )
  1033. {}
  1034. template< typename E2
  1035. nsel_REQUIRES_T(
  1036. std::is_constructible< E, E2>::value
  1037. && !std::is_constructible<E, unexpected_type<E2> & >::value
  1038. && !std::is_constructible<E, unexpected_type<E2> >::value
  1039. && !std::is_constructible<E, unexpected_type<E2> const & >::value
  1040. && !std::is_constructible<E, unexpected_type<E2> const >::value
  1041. && !std::is_convertible< unexpected_type<E2> &, E>::value
  1042. && !std::is_convertible< unexpected_type<E2> , E>::value
  1043. && !std::is_convertible< unexpected_type<E2> const &, E>::value
  1044. && !std::is_convertible< unexpected_type<E2> const , E>::value
  1045. && std::is_convertible< E2 const &, E>::value /*=> non-explicit */
  1046. )
  1047. >
  1048. constexpr /*non-explicit*/ unexpected_type( unexpected_type<E2> && error )
  1049. : m_error( std::move( error.error() ) )
  1050. {}
  1051. // x.x.5.2.2 Assignment
  1052. nsel_constexpr14 unexpected_type& operator=( unexpected_type const & ) = default;
  1053. nsel_constexpr14 unexpected_type& operator=( unexpected_type && ) = default;
  1054. template< typename E2 = E >
  1055. nsel_constexpr14 unexpected_type & operator=( unexpected_type<E2> const & other )
  1056. {
  1057. unexpected_type{ other.error() }.swap( *this );
  1058. return *this;
  1059. }
  1060. template< typename E2 = E >
  1061. nsel_constexpr14 unexpected_type & operator=( unexpected_type<E2> && other )
  1062. {
  1063. unexpected_type{ std::move( other.error() ) }.swap( *this );
  1064. return *this;
  1065. }
  1066. // x.x.5.2.3 Observers
  1067. nsel_constexpr14 E & error() & noexcept
  1068. {
  1069. return m_error;
  1070. }
  1071. constexpr E const & error() const & noexcept
  1072. {
  1073. return m_error;
  1074. }
  1075. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1076. nsel_constexpr14 E && error() && noexcept
  1077. {
  1078. return std::move( m_error );
  1079. }
  1080. constexpr E const && error() const && noexcept
  1081. {
  1082. return std::move( m_error );
  1083. }
  1084. #endif
  1085. // x.x.5.2.3 Observers - deprecated
  1086. nsel_deprecated("replace value() with error()")
  1087. nsel_constexpr14 E & value() & noexcept
  1088. {
  1089. return m_error;
  1090. }
  1091. nsel_deprecated("replace value() with error()")
  1092. constexpr E const & value() const & noexcept
  1093. {
  1094. return m_error;
  1095. }
  1096. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1097. nsel_deprecated("replace value() with error()")
  1098. nsel_constexpr14 E && value() && noexcept
  1099. {
  1100. return std::move( m_error );
  1101. }
  1102. nsel_deprecated("replace value() with error()")
  1103. constexpr E const && value() const && noexcept
  1104. {
  1105. return std::move( m_error );
  1106. }
  1107. #endif
  1108. // x.x.5.2.4 Swap
  1109. template< typename U=E >
  1110. nsel_REQUIRES_R( void,
  1111. std17::is_swappable<U>::value
  1112. )
  1113. swap( unexpected_type & other ) noexcept (
  1114. std17::is_nothrow_swappable<U>::value
  1115. )
  1116. {
  1117. using std::swap;
  1118. swap( m_error, other.m_error );
  1119. }
  1120. // TODO: ??? unexpected_type: in-class friend operator==, !=
  1121. private:
  1122. error_type m_error;
  1123. };
  1124. #if nsel_CPP17_OR_GREATER
  1125. /// template deduction guide:
  1126. template< typename E >
  1127. unexpected_type( E ) -> unexpected_type< E >;
  1128. #endif
  1129. /// class unexpected_type, std::exception_ptr specialization (P0323R2)
  1130. #if !nsel_CONFIG_NO_EXCEPTIONS
  1131. #if nsel_P0323R <= 2
  1132. // TODO: Should expected be specialized for particular E types such as exception_ptr and how?
  1133. // See p0323r7 2.1. Ergonomics, http://wg21.link/p0323
  1134. template<>
  1135. class unexpected_type< std::exception_ptr >
  1136. {
  1137. public:
  1138. using error_type = std::exception_ptr;
  1139. unexpected_type() = delete;
  1140. ~unexpected_type(){}
  1141. explicit unexpected_type( std::exception_ptr const & error )
  1142. : m_error( error )
  1143. {}
  1144. explicit unexpected_type(std::exception_ptr && error )
  1145. : m_error( std::move( error ) )
  1146. {}
  1147. template< typename E >
  1148. explicit unexpected_type( E error )
  1149. : m_error( std::make_exception_ptr( error ) )
  1150. {}
  1151. std::exception_ptr const & value() const
  1152. {
  1153. return m_error;
  1154. }
  1155. std::exception_ptr & value()
  1156. {
  1157. return m_error;
  1158. }
  1159. private:
  1160. std::exception_ptr m_error;
  1161. };
  1162. #endif // nsel_P0323R
  1163. #endif // !nsel_CONFIG_NO_EXCEPTIONS
  1164. /// x.x.4, Unexpected equality operators
  1165. template< typename E1, typename E2 >
  1166. constexpr bool operator==( unexpected_type<E1> const & x, unexpected_type<E2> const & y )
  1167. {
  1168. return x.error() == y.error();
  1169. }
  1170. template< typename E1, typename E2 >
  1171. constexpr bool operator!=( unexpected_type<E1> const & x, unexpected_type<E2> const & y )
  1172. {
  1173. return ! ( x == y );
  1174. }
  1175. #if nsel_P0323R <= 2
  1176. template< typename E >
  1177. constexpr bool operator<( unexpected_type<E> const & x, unexpected_type<E> const & y )
  1178. {
  1179. return x.error() < y.error();
  1180. }
  1181. template< typename E >
  1182. constexpr bool operator>( unexpected_type<E> const & x, unexpected_type<E> const & y )
  1183. {
  1184. return ( y < x );
  1185. }
  1186. template< typename E >
  1187. constexpr bool operator<=( unexpected_type<E> const & x, unexpected_type<E> const & y )
  1188. {
  1189. return ! ( y < x );
  1190. }
  1191. template< typename E >
  1192. constexpr bool operator>=( unexpected_type<E> const & x, unexpected_type<E> const & y )
  1193. {
  1194. return ! ( x < y );
  1195. }
  1196. #endif // nsel_P0323R
  1197. /// x.x.5 Specialized algorithms
  1198. template< typename E
  1199. nsel_REQUIRES_T(
  1200. std17::is_swappable<E>::value
  1201. )
  1202. >
  1203. void swap( unexpected_type<E> & x, unexpected_type<E> & y) noexcept ( noexcept ( x.swap(y) ) )
  1204. {
  1205. x.swap( y );
  1206. }
  1207. #if nsel_P0323R <= 2
  1208. // unexpected: relational operators for std::exception_ptr:
  1209. inline constexpr bool operator<( unexpected_type<std::exception_ptr> const & /*x*/, unexpected_type<std::exception_ptr> const & /*y*/ )
  1210. {
  1211. return false;
  1212. }
  1213. inline constexpr bool operator>( unexpected_type<std::exception_ptr> const & /*x*/, unexpected_type<std::exception_ptr> const & /*y*/ )
  1214. {
  1215. return false;
  1216. }
  1217. inline constexpr bool operator<=( unexpected_type<std::exception_ptr> const & x, unexpected_type<std::exception_ptr> const & y )
  1218. {
  1219. return ( x == y );
  1220. }
  1221. inline constexpr bool operator>=( unexpected_type<std::exception_ptr> const & x, unexpected_type<std::exception_ptr> const & y )
  1222. {
  1223. return ( x == y );
  1224. }
  1225. #endif // nsel_P0323R
  1226. // unexpected: traits
  1227. #if nsel_P0323R <= 3
  1228. template< typename E>
  1229. struct is_unexpected : std::false_type {};
  1230. template< typename E>
  1231. struct is_unexpected< unexpected_type<E> > : std::true_type {};
  1232. #endif // nsel_P0323R
  1233. // unexpected: factory
  1234. // keep make_unexpected() removed in p0323r2 for pre-C++17:
  1235. template< typename E>
  1236. nsel_constexpr14 auto
  1237. make_unexpected( E && value ) -> unexpected_type< typename std::decay<E>::type >
  1238. {
  1239. return unexpected_type< typename std::decay<E>::type >( std::forward<E>(value) );
  1240. }
  1241. #if nsel_P0323R <= 3
  1242. /*nsel_constexpr14*/ auto inline
  1243. make_unexpected_from_current_exception() -> unexpected_type< std::exception_ptr >
  1244. {
  1245. return unexpected_type< std::exception_ptr >( std::current_exception() );
  1246. }
  1247. #endif // nsel_P0323R
  1248. /// x.x.6, x.x.7 expected access error
  1249. template< typename E >
  1250. class bad_expected_access;
  1251. /// x.x.7 bad_expected_access<void>: expected access error
  1252. template <>
  1253. class bad_expected_access< void > : public std::exception
  1254. {
  1255. public:
  1256. explicit bad_expected_access()
  1257. : std::exception()
  1258. {}
  1259. };
  1260. /// x.x.6 bad_expected_access: expected access error
  1261. #if !nsel_CONFIG_NO_EXCEPTIONS
  1262. template< typename E >
  1263. class bad_expected_access : public bad_expected_access< void >
  1264. {
  1265. public:
  1266. using error_type = E;
  1267. explicit bad_expected_access( error_type error )
  1268. : m_error( error )
  1269. {}
  1270. virtual char const * what() const noexcept override
  1271. {
  1272. return "bad_expected_access";
  1273. }
  1274. nsel_constexpr14 error_type & error() &
  1275. {
  1276. return m_error;
  1277. }
  1278. constexpr error_type const & error() const &
  1279. {
  1280. return m_error;
  1281. }
  1282. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1283. nsel_constexpr14 error_type && error() &&
  1284. {
  1285. return std::move( m_error );
  1286. }
  1287. constexpr error_type const && error() const &&
  1288. {
  1289. return std::move( m_error );
  1290. }
  1291. #endif
  1292. private:
  1293. error_type m_error;
  1294. };
  1295. #endif // nsel_CONFIG_NO_EXCEPTIONS
  1296. /// x.x.8 unexpect tag, in_place_unexpected tag: construct an error
  1297. struct unexpect_t{};
  1298. using in_place_unexpected_t = unexpect_t;
  1299. nsel_inline17 constexpr unexpect_t unexpect{};
  1300. nsel_inline17 constexpr unexpect_t in_place_unexpected{};
  1301. /// class error_traits
  1302. #if nsel_CONFIG_NO_EXCEPTIONS
  1303. namespace detail {
  1304. inline bool text( char const * /*text*/ ) { return true; }
  1305. }
  1306. template< typename Error >
  1307. struct error_traits
  1308. {
  1309. static void rethrow( Error const & /*e*/ )
  1310. {
  1311. #if nsel_CONFIG_NO_EXCEPTIONS_SEH
  1312. RaiseException( EXCEPTION_ACCESS_VIOLATION, EXCEPTION_NONCONTINUABLE, 0, NULL );
  1313. #else
  1314. assert( false && detail::text("throw bad_expected_access<Error>{ e };") );
  1315. #endif
  1316. }
  1317. };
  1318. template<>
  1319. struct error_traits< std::exception_ptr >
  1320. {
  1321. static void rethrow( std::exception_ptr const & /*e*/ )
  1322. {
  1323. #if nsel_CONFIG_NO_EXCEPTIONS_SEH
  1324. RaiseException( EXCEPTION_ACCESS_VIOLATION, EXCEPTION_NONCONTINUABLE, 0, NULL );
  1325. #else
  1326. assert( false && detail::text("throw bad_expected_access<std::exception_ptr>{ e };") );
  1327. #endif
  1328. }
  1329. };
  1330. template<>
  1331. struct error_traits< std::error_code >
  1332. {
  1333. static void rethrow( std::error_code const & /*e*/ )
  1334. {
  1335. #if nsel_CONFIG_NO_EXCEPTIONS_SEH
  1336. RaiseException( EXCEPTION_ACCESS_VIOLATION, EXCEPTION_NONCONTINUABLE, 0, NULL );
  1337. #else
  1338. assert( false && detail::text("throw std::system_error( e );") );
  1339. #endif
  1340. }
  1341. };
  1342. #else // nsel_CONFIG_NO_EXCEPTIONS
  1343. template< typename Error >
  1344. struct error_traits
  1345. {
  1346. static void rethrow( Error const & e )
  1347. {
  1348. throw bad_expected_access<Error>{ e };
  1349. }
  1350. };
  1351. template<>
  1352. struct error_traits< std::exception_ptr >
  1353. {
  1354. static void rethrow( std::exception_ptr const & e )
  1355. {
  1356. std::rethrow_exception( e );
  1357. }
  1358. };
  1359. template<>
  1360. struct error_traits< std::error_code >
  1361. {
  1362. static void rethrow( std::error_code const & e )
  1363. {
  1364. throw std::system_error( e );
  1365. }
  1366. };
  1367. #endif // nsel_CONFIG_NO_EXCEPTIONS
  1368. #if nsel_P2505R >= 3
  1369. namespace detail {
  1370. // from https://en.cppreference.com/w/cpp/utility/expected/unexpected:
  1371. // "the type of the unexpected value. The type must not be an array type, a non-object type, a specialization of std::unexpected, or a cv-qualified type."
  1372. template< typename T >
  1373. struct valid_unexpected_type : std::integral_constant< bool,
  1374. std::is_same< T, typename std20::remove_cvref< T >::type >::value
  1375. && std::is_object< T >::value
  1376. && !std::is_array< T >::value
  1377. > {};
  1378. template< typename T >
  1379. struct valid_unexpected_type< unexpected_type< T > > : std::false_type {};
  1380. } // namespace detail
  1381. #endif // nsel_P2505R >= 3
  1382. } // namespace expected_lite
  1383. // provide nonstd::unexpected_type:
  1384. using expected_lite::unexpected_type;
  1385. namespace expected_lite {
  1386. /// class expected
  1387. #if nsel_P0323R <= 2
  1388. template< typename T, typename E = std::exception_ptr >
  1389. class expected
  1390. #else
  1391. template< typename T, typename E >
  1392. class expected
  1393. #endif // nsel_P0323R
  1394. {
  1395. private:
  1396. template< typename, typename > friend class expected;
  1397. public:
  1398. using value_type = T;
  1399. using error_type = E;
  1400. using unexpected_type = nonstd::unexpected_type<E>;
  1401. template< typename U >
  1402. struct rebind
  1403. {
  1404. using type = expected<U, error_type>;
  1405. };
  1406. // x.x.4.1 constructors
  1407. nsel_REQUIRES_0(
  1408. std::is_default_constructible<T>::value
  1409. )
  1410. nsel_constexpr14 expected()
  1411. : contained( true )
  1412. {
  1413. contained.construct_value();
  1414. }
  1415. nsel_constexpr14 expected( expected const & ) = default;
  1416. nsel_constexpr14 expected( expected && ) = default;
  1417. template< typename U, typename G
  1418. nsel_REQUIRES_T(
  1419. std::is_constructible< T, U const &>::value
  1420. && std::is_constructible<E, G const &>::value
  1421. && !std::is_constructible<T, expected<U, G> & >::value
  1422. && !std::is_constructible<T, expected<U, G> && >::value
  1423. && !std::is_constructible<T, expected<U, G> const & >::value
  1424. && !std::is_constructible<T, expected<U, G> const && >::value
  1425. && !std::is_convertible< expected<U, G> & , T>::value
  1426. && !std::is_convertible< expected<U, G> &&, T>::value
  1427. && !std::is_convertible< expected<U, G> const & , T>::value
  1428. && !std::is_convertible< expected<U, G> const &&, T>::value
  1429. && (!std::is_convertible<U const &, T>::value || !std::is_convertible<G const &, E>::value ) /*=> explicit */
  1430. )
  1431. >
  1432. nsel_constexpr14 explicit expected( expected<U, G> const & other )
  1433. : contained( other.has_value() )
  1434. {
  1435. if ( has_value() ) contained.construct_value( T{ other.contained.value() } );
  1436. else contained.construct_error( E{ other.contained.error() } );
  1437. }
  1438. template< typename U, typename G
  1439. nsel_REQUIRES_T(
  1440. std::is_constructible< T, U const &>::value
  1441. && std::is_constructible<E, G const &>::value
  1442. && !std::is_constructible<T, expected<U, G> & >::value
  1443. && !std::is_constructible<T, expected<U, G> && >::value
  1444. && !std::is_constructible<T, expected<U, G> const & >::value
  1445. && !std::is_constructible<T, expected<U, G> const && >::value
  1446. && !std::is_convertible< expected<U, G> & , T>::value
  1447. && !std::is_convertible< expected<U, G> &&, T>::value
  1448. && !std::is_convertible< expected<U, G> const &, T>::value
  1449. && !std::is_convertible< expected<U, G> const &&, T>::value
  1450. && !(!std::is_convertible<U const &, T>::value || !std::is_convertible<G const &, E>::value ) /*=> non-explicit */
  1451. )
  1452. >
  1453. nsel_constexpr14 /*non-explicit*/ expected( expected<U, G> const & other )
  1454. : contained( other.has_value() )
  1455. {
  1456. if ( has_value() ) contained.construct_value( other.contained.value() );
  1457. else contained.construct_error( other.contained.error() );
  1458. }
  1459. template< typename U, typename G
  1460. nsel_REQUIRES_T(
  1461. std::is_constructible< T, U>::value
  1462. && std::is_constructible<E, G>::value
  1463. && !std::is_constructible<T, expected<U, G> & >::value
  1464. && !std::is_constructible<T, expected<U, G> && >::value
  1465. && !std::is_constructible<T, expected<U, G> const & >::value
  1466. && !std::is_constructible<T, expected<U, G> const && >::value
  1467. && !std::is_convertible< expected<U, G> & , T>::value
  1468. && !std::is_convertible< expected<U, G> &&, T>::value
  1469. && !std::is_convertible< expected<U, G> const & , T>::value
  1470. && !std::is_convertible< expected<U, G> const &&, T>::value
  1471. && (!std::is_convertible<U, T>::value || !std::is_convertible<G, E>::value ) /*=> explicit */
  1472. )
  1473. >
  1474. nsel_constexpr14 explicit expected( expected<U, G> && other )
  1475. : contained( other.has_value() )
  1476. {
  1477. if ( has_value() ) contained.construct_value( T{ std::move( other.contained.value() ) } );
  1478. else contained.construct_error( E{ std::move( other.contained.error() ) } );
  1479. }
  1480. template< typename U, typename G
  1481. nsel_REQUIRES_T(
  1482. std::is_constructible< T, U>::value
  1483. && std::is_constructible<E, G>::value
  1484. && !std::is_constructible<T, expected<U, G> & >::value
  1485. && !std::is_constructible<T, expected<U, G> && >::value
  1486. && !std::is_constructible<T, expected<U, G> const & >::value
  1487. && !std::is_constructible<T, expected<U, G> const && >::value
  1488. && !std::is_convertible< expected<U, G> & , T>::value
  1489. && !std::is_convertible< expected<U, G> &&, T>::value
  1490. && !std::is_convertible< expected<U, G> const & , T>::value
  1491. && !std::is_convertible< expected<U, G> const &&, T>::value
  1492. && !(!std::is_convertible<U, T>::value || !std::is_convertible<G, E>::value ) /*=> non-explicit */
  1493. )
  1494. >
  1495. nsel_constexpr14 /*non-explicit*/ expected( expected<U, G> && other )
  1496. : contained( other.has_value() )
  1497. {
  1498. if ( has_value() ) contained.construct_value( std::move( other.contained.value() ) );
  1499. else contained.construct_error( std::move( other.contained.error() ) );
  1500. }
  1501. template< typename U = T
  1502. nsel_REQUIRES_T(
  1503. std::is_copy_constructible<U>::value
  1504. )
  1505. >
  1506. nsel_constexpr14 expected( value_type const & value )
  1507. : contained( true )
  1508. {
  1509. contained.construct_value( value );
  1510. }
  1511. template< typename U = T
  1512. nsel_REQUIRES_T(
  1513. std::is_constructible<T,U&&>::value
  1514. && !std::is_same<typename std20::remove_cvref<U>::type, nonstd_lite_in_place_t(U)>::value
  1515. && !std::is_same< expected<T,E> , typename std20::remove_cvref<U>::type>::value
  1516. && !std::is_same<nonstd::unexpected_type<E>, typename std20::remove_cvref<U>::type>::value
  1517. && !std::is_convertible<U&&,T>::value /*=> explicit */
  1518. )
  1519. >
  1520. nsel_constexpr14 explicit expected( U && value ) noexcept
  1521. (
  1522. std::is_nothrow_move_constructible<U>::value &&
  1523. std::is_nothrow_move_constructible<E>::value
  1524. )
  1525. : contained( true )
  1526. {
  1527. contained.construct_value( T{ std::forward<U>( value ) } );
  1528. }
  1529. template< typename U = T
  1530. nsel_REQUIRES_T(
  1531. std::is_constructible<T,U&&>::value
  1532. && !std::is_same<typename std20::remove_cvref<U>::type, nonstd_lite_in_place_t(U)>::value
  1533. && !std::is_same< expected<T,E> , typename std20::remove_cvref<U>::type>::value
  1534. && !std::is_same<nonstd::unexpected_type<E>, typename std20::remove_cvref<U>::type>::value
  1535. && std::is_convertible<U&&,T>::value /*=> non-explicit */
  1536. )
  1537. >
  1538. nsel_constexpr14 /*non-explicit*/ expected( U && value ) noexcept
  1539. (
  1540. std::is_nothrow_move_constructible<U>::value &&
  1541. std::is_nothrow_move_constructible<E>::value
  1542. )
  1543. : contained( true )
  1544. {
  1545. contained.construct_value( std::forward<U>( value ) );
  1546. }
  1547. // construct error:
  1548. template< typename G = E
  1549. nsel_REQUIRES_T(
  1550. std::is_constructible<E, G const & >::value
  1551. && !std::is_convertible< G const &, E>::value /*=> explicit */
  1552. )
  1553. >
  1554. nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> const & error )
  1555. : contained( false )
  1556. {
  1557. contained.construct_error( E{ error.error() } );
  1558. }
  1559. template< typename G = E
  1560. nsel_REQUIRES_T(
  1561. std::is_constructible<E, G const & >::value
  1562. && std::is_convertible< G const &, E>::value /*=> non-explicit */
  1563. )
  1564. >
  1565. nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> const & error )
  1566. : contained( false )
  1567. {
  1568. contained.construct_error( error.error() );
  1569. }
  1570. template< typename G = E
  1571. nsel_REQUIRES_T(
  1572. std::is_constructible<E, G&& >::value
  1573. && !std::is_convertible< G&&, E>::value /*=> explicit */
  1574. )
  1575. >
  1576. nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> && error )
  1577. : contained( false )
  1578. {
  1579. contained.construct_error( E{ std::move( error.error() ) } );
  1580. }
  1581. template< typename G = E
  1582. nsel_REQUIRES_T(
  1583. std::is_constructible<E, G&& >::value
  1584. && std::is_convertible< G&&, E>::value /*=> non-explicit */
  1585. )
  1586. >
  1587. nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> && error )
  1588. : contained( false )
  1589. {
  1590. contained.construct_error( std::move( error.error() ) );
  1591. }
  1592. // in-place construction, value
  1593. template< typename... Args
  1594. nsel_REQUIRES_T(
  1595. std::is_constructible<T, Args&&...>::value
  1596. )
  1597. >
  1598. nsel_constexpr14 explicit expected( nonstd_lite_in_place_t(T), Args&&... args )
  1599. : contained( true )
  1600. {
  1601. contained.emplace_value( std::forward<Args>( args )... );
  1602. }
  1603. template< typename U, typename... Args
  1604. nsel_REQUIRES_T(
  1605. std::is_constructible<T, std::initializer_list<U>, Args&&...>::value
  1606. )
  1607. >
  1608. nsel_constexpr14 explicit expected( nonstd_lite_in_place_t(T), std::initializer_list<U> il, Args&&... args )
  1609. : contained( true )
  1610. {
  1611. contained.emplace_value( il, std::forward<Args>( args )... );
  1612. }
  1613. // in-place construction, error
  1614. template< typename... Args
  1615. nsel_REQUIRES_T(
  1616. std::is_constructible<E, Args&&...>::value
  1617. )
  1618. >
  1619. nsel_constexpr14 explicit expected( unexpect_t, Args&&... args )
  1620. : contained( false )
  1621. {
  1622. contained.emplace_error( std::forward<Args>( args )... );
  1623. }
  1624. template< typename U, typename... Args
  1625. nsel_REQUIRES_T(
  1626. std::is_constructible<E, std::initializer_list<U>, Args&&...>::value
  1627. )
  1628. >
  1629. nsel_constexpr14 explicit expected( unexpect_t, std::initializer_list<U> il, Args&&... args )
  1630. : contained( false )
  1631. {
  1632. contained.emplace_error( il, std::forward<Args>( args )... );
  1633. }
  1634. // x.x.4.2 destructor
  1635. // TODO: ~expected: triviality
  1636. // Effects: If T is not cv void and is_trivially_destructible_v<T> is false and bool(*this), calls val.~T(). If is_trivially_destructible_v<E> is false and !bool(*this), calls unexpect.~unexpected<E>().
  1637. // Remarks: If either T is cv void or is_trivially_destructible_v<T> is true, and is_trivially_destructible_v<E> is true, then this destructor shall be a trivial destructor.
  1638. ~expected()
  1639. {
  1640. if ( has_value() ) contained.destruct_value();
  1641. else contained.destruct_error();
  1642. }
  1643. // x.x.4.3 assignment
  1644. expected & operator=( expected const & other )
  1645. {
  1646. expected( other ).swap( *this );
  1647. return *this;
  1648. }
  1649. expected & operator=( expected && other ) noexcept
  1650. (
  1651. std::is_nothrow_move_constructible< T>::value
  1652. && std::is_nothrow_move_assignable< T>::value
  1653. && std::is_nothrow_move_constructible<E>::value // added for missing
  1654. && std::is_nothrow_move_assignable< E>::value ) // nothrow above
  1655. {
  1656. expected( std::move( other ) ).swap( *this );
  1657. return *this;
  1658. }
  1659. template< typename U
  1660. nsel_REQUIRES_T(
  1661. !std::is_same<expected<T,E>, typename std20::remove_cvref<U>::type>::value
  1662. && std17::conjunction<std::is_scalar<T>, std::is_same<T, std::decay<U>> >::value
  1663. && std::is_constructible<T ,U>::value
  1664. && std::is_assignable< T&,U>::value
  1665. && std::is_nothrow_move_constructible<E>::value )
  1666. >
  1667. expected & operator=( U && value )
  1668. {
  1669. expected( std::forward<U>( value ) ).swap( *this );
  1670. return *this;
  1671. }
  1672. template< typename G = E
  1673. nsel_REQUIRES_T(
  1674. std::is_constructible<E, G const&>::value &&
  1675. std::is_copy_constructible<G>::value // TODO: std::is_nothrow_copy_constructible<G>
  1676. && std::is_copy_assignable<G>::value
  1677. )
  1678. >
  1679. expected & operator=( nonstd::unexpected_type<G> const & error )
  1680. {
  1681. expected( unexpect, error.error() ).swap( *this );
  1682. return *this;
  1683. }
  1684. template< typename G = E
  1685. nsel_REQUIRES_T(
  1686. std::is_constructible<E, G&&>::value &&
  1687. std::is_move_constructible<G>::value // TODO: std::is_nothrow_move_constructible<G>
  1688. && std::is_move_assignable<G>::value
  1689. )
  1690. >
  1691. expected & operator=( nonstd::unexpected_type<G> && error )
  1692. {
  1693. expected( unexpect, std::move( error.error() ) ).swap( *this );
  1694. return *this;
  1695. }
  1696. template< typename... Args
  1697. nsel_REQUIRES_T(
  1698. std::is_nothrow_constructible<T, Args&&...>::value
  1699. )
  1700. >
  1701. value_type & emplace( Args &&... args )
  1702. {
  1703. expected( nonstd_lite_in_place(T), std::forward<Args>(args)... ).swap( *this );
  1704. return value();
  1705. }
  1706. template< typename U, typename... Args
  1707. nsel_REQUIRES_T(
  1708. std::is_nothrow_constructible<T, std::initializer_list<U>&, Args&&...>::value
  1709. )
  1710. >
  1711. value_type & emplace( std::initializer_list<U> il, Args &&... args )
  1712. {
  1713. expected( nonstd_lite_in_place(T), il, std::forward<Args>(args)... ).swap( *this );
  1714. return value();
  1715. }
  1716. // x.x.4.4 swap
  1717. template< typename U=T, typename G=E >
  1718. nsel_REQUIRES_R( void,
  1719. std17::is_swappable< U>::value
  1720. && std17::is_swappable<G>::value
  1721. && ( std::is_move_constructible<U>::value || std::is_move_constructible<G>::value )
  1722. )
  1723. swap( expected & other ) noexcept
  1724. (
  1725. std::is_nothrow_move_constructible<T>::value && std17::is_nothrow_swappable<T&>::value &&
  1726. std::is_nothrow_move_constructible<E>::value && std17::is_nothrow_swappable<E&>::value
  1727. )
  1728. {
  1729. using std::swap;
  1730. if ( bool(*this) && bool(other) ) { swap( contained.value(), other.contained.value() ); }
  1731. else if ( ! bool(*this) && ! bool(other) ) { swap( contained.error(), other.contained.error() ); }
  1732. else if ( bool(*this) && ! bool(other) ) { error_type t( std::move( other.error() ) );
  1733. other.contained.destruct_error();
  1734. other.contained.construct_value( std::move( contained.value() ) );
  1735. contained.destruct_value();
  1736. contained.construct_error( std::move( t ) );
  1737. bool has_value = contained.has_value();
  1738. bool other_has_value = other.has_value();
  1739. other.contained.set_has_value(has_value);
  1740. contained.set_has_value(other_has_value);
  1741. }
  1742. else if ( ! bool(*this) && bool(other) ) { other.swap( *this ); }
  1743. }
  1744. // x.x.4.5 observers
  1745. constexpr value_type const * operator ->() const
  1746. {
  1747. return assert( has_value() ), contained.value_ptr();
  1748. }
  1749. value_type * operator ->()
  1750. {
  1751. return assert( has_value() ), contained.value_ptr();
  1752. }
  1753. constexpr value_type const & operator *() const &
  1754. {
  1755. return assert( has_value() ), contained.value();
  1756. }
  1757. value_type & operator *() &
  1758. {
  1759. return assert( has_value() ), contained.value();
  1760. }
  1761. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1762. constexpr value_type const && operator *() const &&
  1763. {
  1764. return std::move( ( assert( has_value() ), contained.value() ) );
  1765. }
  1766. nsel_constexpr14 value_type && operator *() &&
  1767. {
  1768. return std::move( ( assert( has_value() ), contained.value() ) );
  1769. }
  1770. #endif
  1771. constexpr explicit operator bool() const noexcept
  1772. {
  1773. return has_value();
  1774. }
  1775. constexpr bool has_value() const noexcept
  1776. {
  1777. return contained.has_value();
  1778. }
  1779. nsel_DISABLE_MSVC_WARNINGS( 4702 ) // warning C4702: unreachable code, see issue 65.
  1780. constexpr value_type const & value() const &
  1781. {
  1782. return has_value()
  1783. ? ( contained.value() )
  1784. : ( error_traits<error_type>::rethrow( contained.error() ), contained.value() );
  1785. }
  1786. value_type & value() &
  1787. {
  1788. return has_value()
  1789. ? ( contained.value() )
  1790. : ( error_traits<error_type>::rethrow( contained.error() ), contained.value() );
  1791. }
  1792. nsel_RESTORE_MSVC_WARNINGS()
  1793. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1794. constexpr value_type const && value() const &&
  1795. {
  1796. return std::move( has_value()
  1797. ? ( contained.value() )
  1798. : ( error_traits<error_type>::rethrow( contained.error() ), contained.value() ) );
  1799. }
  1800. nsel_constexpr14 value_type && value() &&
  1801. {
  1802. return std::move( has_value()
  1803. ? ( contained.value() )
  1804. : ( error_traits<error_type>::rethrow( contained.error() ), contained.value() ) );
  1805. }
  1806. #endif
  1807. constexpr error_type const & error() const &
  1808. {
  1809. return assert( ! has_value() ), contained.error();
  1810. }
  1811. error_type & error() &
  1812. {
  1813. return assert( ! has_value() ), contained.error();
  1814. }
  1815. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1816. constexpr error_type const && error() const &&
  1817. {
  1818. return std::move( ( assert( ! has_value() ), contained.error() ) );
  1819. }
  1820. error_type && error() &&
  1821. {
  1822. return std::move( ( assert( ! has_value() ), contained.error() ) );
  1823. }
  1824. #endif
  1825. constexpr unexpected_type get_unexpected() const
  1826. {
  1827. return make_unexpected( contained.error() );
  1828. }
  1829. template< typename Ex >
  1830. bool has_exception() const
  1831. {
  1832. using ContainedEx = typename std::remove_reference< decltype( get_unexpected().value() ) >::type;
  1833. return ! has_value() && std::is_base_of< Ex, ContainedEx>::value;
  1834. }
  1835. template< typename U
  1836. nsel_REQUIRES_T(
  1837. std::is_copy_constructible< T>::value
  1838. && std::is_convertible<U&&, T>::value
  1839. )
  1840. >
  1841. value_type value_or( U && v ) const &
  1842. {
  1843. return has_value()
  1844. ? contained.value()
  1845. : static_cast<T>( std::forward<U>( v ) );
  1846. }
  1847. template< typename U
  1848. nsel_REQUIRES_T(
  1849. std::is_move_constructible< T>::value
  1850. && std::is_convertible<U&&, T>::value
  1851. )
  1852. >
  1853. value_type value_or( U && v ) &&
  1854. {
  1855. return has_value()
  1856. ? std::move( contained.value() )
  1857. : static_cast<T>( std::forward<U>( v ) );
  1858. }
  1859. #if nsel_P2505R >= 4
  1860. template< typename G = E
  1861. nsel_REQUIRES_T(
  1862. std::is_copy_constructible< E >::value
  1863. && std::is_convertible< G, E >::value
  1864. )
  1865. >
  1866. nsel_constexpr error_type error_or( G && e ) const &
  1867. {
  1868. return has_value()
  1869. ? static_cast< E >( std::forward< G >( e ) )
  1870. : contained.error();
  1871. }
  1872. template< typename G = E
  1873. nsel_REQUIRES_T(
  1874. std::is_move_constructible< E >::value
  1875. && std::is_convertible< G, E >::value
  1876. )
  1877. >
  1878. nsel_constexpr14 error_type error_or( G && e ) &&
  1879. {
  1880. return has_value()
  1881. ? static_cast< E >( std::forward< G >( e ) )
  1882. : std::move( contained.error() );
  1883. }
  1884. #endif // nsel_P2505R >= 4
  1885. #if nsel_P2505R >= 3
  1886. // Monadic operations (P2505)
  1887. template< typename F
  1888. nsel_REQUIRES_T(
  1889. detail::is_expected < detail::invoke_result_nocvref_t< F, value_type & > > ::value
  1890. && std::is_same< typename detail::invoke_result_nocvref_t< F, value_type & >::error_type, error_type >::value
  1891. && std::is_constructible< error_type, error_type & >::value
  1892. )
  1893. >
  1894. nsel_constexpr14 detail::invoke_result_nocvref_t< F, value_type & > and_then( F && f ) &
  1895. {
  1896. return has_value()
  1897. ? detail::invoke_result_nocvref_t< F, value_type & >( detail::invoke( std::forward< F >( f ), value() ) )
  1898. : detail::invoke_result_nocvref_t< F, value_type & >( unexpect, error() );
  1899. }
  1900. template<typename F
  1901. nsel_REQUIRES_T(
  1902. detail::is_expected< detail::invoke_result_nocvref_t< F, const value_type & > >::value
  1903. && std::is_same< typename detail::invoke_result_nocvref_t< F, const value_type & >::error_type, error_type >::value
  1904. && std::is_constructible< error_type, const error_type & >::value
  1905. )
  1906. >
  1907. nsel_constexpr detail::invoke_result_nocvref_t< F, const value_type & > and_then( F && f ) const &
  1908. {
  1909. return has_value()
  1910. ? detail::invoke_result_nocvref_t< F, const value_type & >( detail::invoke( std::forward< F >( f ), value() ) )
  1911. : detail::invoke_result_nocvref_t< F, const value_type & >( unexpect, error() );
  1912. }
  1913. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1914. template<typename F
  1915. nsel_REQUIRES_T(
  1916. detail::is_expected< detail::invoke_result_nocvref_t< F, value_type && > >::value
  1917. && std::is_same< typename detail::invoke_result_nocvref_t< F, value_type && >::error_type, error_type >::value
  1918. && std::is_constructible< error_type, error_type && >::value
  1919. )
  1920. >
  1921. nsel_constexpr14 detail::invoke_result_nocvref_t< F, value_type && > and_then( F && f ) &&
  1922. {
  1923. return has_value()
  1924. ? detail::invoke_result_nocvref_t< F, value_type && >( detail::invoke( std::forward< F >( f ), std::move( value() ) ) )
  1925. : detail::invoke_result_nocvref_t< F, value_type && >( unexpect, std::move( error() ) );
  1926. }
  1927. template<typename F
  1928. nsel_REQUIRES_T(
  1929. detail::is_expected< detail::invoke_result_nocvref_t< F, const value_type && > >::value
  1930. && std::is_same< typename detail::invoke_result_nocvref_t< F, const value_type & >::error_type, error_type >::value
  1931. && std::is_constructible< error_type, const error_type && >::value
  1932. )
  1933. >
  1934. nsel_constexpr detail::invoke_result_nocvref_t< F, const value_type && > and_then( F && f ) const &&
  1935. {
  1936. return has_value()
  1937. ? detail::invoke_result_nocvref_t< F, const value_type && >( detail::invoke( std::forward< F >( f ), std::move( value() ) ) )
  1938. : detail::invoke_result_nocvref_t< F, const value_type && >( unexpect, std::move( error() ) );
  1939. }
  1940. #endif
  1941. template<typename F
  1942. nsel_REQUIRES_T(
  1943. detail::is_expected< detail::invoke_result_nocvref_t< F, error_type & > >::value
  1944. && std::is_same< typename detail::invoke_result_nocvref_t< F, error_type & >::value_type, value_type >::value
  1945. && std::is_constructible< value_type, value_type & >::value
  1946. )
  1947. >
  1948. nsel_constexpr14 detail::invoke_result_nocvref_t< F, error_type & > or_else( F && f ) &
  1949. {
  1950. return has_value()
  1951. ? detail::invoke_result_nocvref_t< F, error_type & >( value() )
  1952. : detail::invoke_result_nocvref_t< F, error_type & >( detail::invoke( std::forward< F >( f ), error() ) );
  1953. }
  1954. template<typename F
  1955. nsel_REQUIRES_T(
  1956. detail::is_expected< detail::invoke_result_nocvref_t< F, const error_type & > >::value
  1957. && std::is_same< typename detail::invoke_result_nocvref_t< F, const error_type & >::value_type, value_type >::value
  1958. && std::is_constructible< value_type, const value_type & >::value
  1959. )
  1960. >
  1961. nsel_constexpr detail::invoke_result_nocvref_t< F, const error_type & > or_else( F && f ) const &
  1962. {
  1963. return has_value()
  1964. ? detail::invoke_result_nocvref_t< F, const error_type & >( value() )
  1965. : detail::invoke_result_nocvref_t< F, const error_type & >( detail::invoke( std::forward< F >( f ), error() ) );
  1966. }
  1967. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  1968. template<typename F
  1969. nsel_REQUIRES_T(
  1970. detail::is_expected< detail::invoke_result_nocvref_t< F, error_type && > >::value
  1971. && std::is_same< typename detail::invoke_result_nocvref_t< F, error_type && >::value_type, value_type >::value
  1972. && std::is_constructible< value_type, value_type && >::value
  1973. )
  1974. >
  1975. nsel_constexpr14 detail::invoke_result_nocvref_t< F, error_type && > or_else( F && f ) &&
  1976. {
  1977. return has_value()
  1978. ? detail::invoke_result_nocvref_t< F, error_type && >( std::move( value() ) )
  1979. : detail::invoke_result_nocvref_t< F, error_type && >( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  1980. }
  1981. template<typename F
  1982. nsel_REQUIRES_T(
  1983. detail::is_expected< detail::invoke_result_nocvref_t< F, const error_type && > >::value
  1984. && std::is_same< typename detail::invoke_result_nocvref_t< F, const error_type && >::value_type, value_type >::value
  1985. && std::is_constructible< value_type, const value_type && >::value
  1986. )
  1987. >
  1988. nsel_constexpr detail::invoke_result_nocvref_t< F, const error_type && > or_else( F && f ) const &&
  1989. {
  1990. return has_value()
  1991. ? detail::invoke_result_nocvref_t< F, const error_type && >( std::move( value() ) )
  1992. : detail::invoke_result_nocvref_t< F, const error_type && >( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  1993. }
  1994. #endif
  1995. template<typename F
  1996. nsel_REQUIRES_T(
  1997. std::is_constructible< error_type, error_type & >::value
  1998. && !std::is_void< detail::transform_invoke_result_t< F, value_type & > >::value
  1999. && detail::valid_expected_value_type< detail::transform_invoke_result_t< F, value_type & > >::value
  2000. )
  2001. >
  2002. nsel_constexpr14 expected< detail::transform_invoke_result_t< F, value_type & >, error_type > transform( F && f ) &
  2003. {
  2004. return has_value()
  2005. ? expected< detail::transform_invoke_result_t< F, value_type & >, error_type >( detail::invoke( std::forward< F >( f ), **this ) )
  2006. : make_unexpected( error() );
  2007. }
  2008. template<typename F
  2009. nsel_REQUIRES_T(
  2010. std::is_constructible< error_type, error_type & >::value
  2011. && std::is_void< detail::transform_invoke_result_t< F, value_type & > >::value
  2012. )
  2013. >
  2014. nsel_constexpr14 expected< void, error_type > transform( F && f ) &
  2015. {
  2016. return has_value()
  2017. ? ( detail::invoke( std::forward< F >( f ), **this ), expected< void, error_type >() )
  2018. : make_unexpected( error() );
  2019. }
  2020. template<typename F
  2021. nsel_REQUIRES_T(
  2022. std::is_constructible< error_type, const error_type & >::value
  2023. && !std::is_void< detail::transform_invoke_result_t< F, const value_type & > >::value
  2024. && detail::valid_expected_value_type< detail::transform_invoke_result_t< F, const value_type & > >::value
  2025. )
  2026. >
  2027. nsel_constexpr expected< detail::transform_invoke_result_t< F, const value_type & >, error_type > transform( F && f ) const &
  2028. {
  2029. return has_value()
  2030. ? expected< detail::transform_invoke_result_t< F, const value_type & >, error_type >( detail::invoke( std::forward< F >( f ), **this ) )
  2031. : make_unexpected( error() );
  2032. }
  2033. template<typename F
  2034. nsel_REQUIRES_T(
  2035. std::is_constructible< error_type, const error_type & >::value
  2036. && std::is_void< detail::transform_invoke_result_t< F, const value_type & > >::value
  2037. )
  2038. >
  2039. nsel_constexpr expected< void, error_type > transform( F && f ) const &
  2040. {
  2041. return has_value()
  2042. ? ( detail::invoke( std::forward< F >( f ), **this ), expected< void, error_type >() )
  2043. : make_unexpected( error() );
  2044. }
  2045. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2046. template<typename F
  2047. nsel_REQUIRES_T(
  2048. std::is_constructible< error_type, error_type && >::value
  2049. && !std::is_void< detail::transform_invoke_result_t< F, value_type && > >::value
  2050. && detail::valid_expected_value_type< detail::transform_invoke_result_t< F, value_type && > >::value
  2051. )
  2052. >
  2053. nsel_constexpr14 expected< detail::transform_invoke_result_t< F, value_type && >, error_type > transform( F && f ) &&
  2054. {
  2055. return has_value()
  2056. ? expected< detail::transform_invoke_result_t< F, value_type && >, error_type >( detail::invoke( std::forward< F >( f ), std::move( **this ) ) )
  2057. : make_unexpected( std::move( error() ) );
  2058. }
  2059. template<typename F
  2060. nsel_REQUIRES_T(
  2061. std::is_constructible< error_type, error_type && >::value
  2062. && std::is_void< detail::transform_invoke_result_t< F, value_type && > >::value
  2063. )
  2064. >
  2065. nsel_constexpr14 expected< void, error_type > transform( F && f ) &&
  2066. {
  2067. return has_value()
  2068. ? ( detail::invoke( std::forward< F >( f ), **this ), expected< void, error_type >() )
  2069. : make_unexpected( std::move( error() ) );
  2070. }
  2071. template<typename F
  2072. nsel_REQUIRES_T(
  2073. std::is_constructible< error_type, const error_type && >::value
  2074. && !std::is_void< detail::transform_invoke_result_t< F, const value_type && > >::value
  2075. && detail::valid_expected_value_type< detail::transform_invoke_result_t< F, const value_type && > >::value
  2076. )
  2077. >
  2078. nsel_constexpr expected< detail::transform_invoke_result_t< F, const value_type && >, error_type > transform( F && f ) const &&
  2079. {
  2080. return has_value()
  2081. ? expected< detail::transform_invoke_result_t< F, const value_type && >, error_type >( detail::invoke( std::forward< F >( f ), std::move( **this ) ) )
  2082. : make_unexpected( std::move( error() ) );
  2083. }
  2084. template<typename F
  2085. nsel_REQUIRES_T(
  2086. std::is_constructible< error_type, const error_type && >::value
  2087. && std::is_void< detail::transform_invoke_result_t< F, const value_type && > >::value
  2088. )
  2089. >
  2090. nsel_constexpr expected< void, error_type > transform( F && f ) const &&
  2091. {
  2092. return has_value()
  2093. ? ( detail::invoke( std::forward< F >( f ), **this ), expected< void, error_type >() )
  2094. : make_unexpected( std::move( error() ) );
  2095. }
  2096. #endif
  2097. template<typename F
  2098. nsel_REQUIRES_T(
  2099. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, error_type & > >::value
  2100. && std::is_constructible< value_type, value_type & >::value
  2101. )
  2102. >
  2103. nsel_constexpr14 expected< value_type, detail::transform_invoke_result_t< F, error_type & > > transform_error( F && f ) &
  2104. {
  2105. return has_value()
  2106. ? expected< value_type, detail::transform_invoke_result_t< F, error_type & > >( in_place, **this )
  2107. : make_unexpected( detail::invoke( std::forward< F >( f ), error() ) );
  2108. }
  2109. template<typename F
  2110. nsel_REQUIRES_T(
  2111. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, const error_type & > >::value
  2112. && std::is_constructible< value_type, const value_type & >::value
  2113. )
  2114. >
  2115. nsel_constexpr expected< value_type, detail::transform_invoke_result_t< F, const error_type & > > transform_error( F && f ) const &
  2116. {
  2117. return has_value()
  2118. ? expected< value_type, detail::transform_invoke_result_t< F, const error_type & > >( in_place, **this )
  2119. : make_unexpected( detail::invoke( std::forward< F >( f ), error() ) );
  2120. }
  2121. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2122. template<typename F
  2123. nsel_REQUIRES_T(
  2124. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, error_type && > >::value
  2125. && std::is_constructible< value_type, value_type && >::value
  2126. )
  2127. >
  2128. nsel_constexpr14 expected< value_type, detail::transform_invoke_result_t< F, error_type && > > transform_error( F && f ) &&
  2129. {
  2130. return has_value()
  2131. ? expected< value_type, detail::transform_invoke_result_t< F, error_type && > >( in_place, std::move( **this ) )
  2132. : make_unexpected( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2133. }
  2134. template<typename F
  2135. nsel_REQUIRES_T(
  2136. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, const error_type && > >::value
  2137. && std::is_constructible< value_type, const value_type && >::value
  2138. )
  2139. >
  2140. nsel_constexpr expected< value_type, detail::transform_invoke_result_t< F, const error_type && > > transform_error( F && f ) const &&
  2141. {
  2142. return has_value()
  2143. ? expected< value_type, detail::transform_invoke_result_t< F, const error_type && > >( in_place, std::move( **this ) )
  2144. : make_unexpected( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2145. }
  2146. #endif
  2147. #endif // nsel_P2505R >= 3
  2148. // unwrap()
  2149. // template <class U, class E>
  2150. // constexpr expected<U,E> expected<expected<U,E>,E>::unwrap() const&;
  2151. // template <class T, class E>
  2152. // constexpr expected<T,E> expected<T,E>::unwrap() const&;
  2153. // template <class U, class E>
  2154. // expected<U,E> expected<expected<U,E>, E>::unwrap() &&;
  2155. // template <class T, class E>
  2156. // template expected<T,E> expected<T,E>::unwrap() &&;
  2157. // factories
  2158. // template< typename Ex, typename F>
  2159. // expected<T,E> catch_exception(F&& f);
  2160. // template< typename F>
  2161. // expected<decltype(func(declval<T>())),E> map(F&& func) ;
  2162. // template< typename F>
  2163. // 'see below' bind(F&& func);
  2164. // template< typename F>
  2165. // expected<T,E> catch_error(F&& f);
  2166. // template< typename F>
  2167. // 'see below' then(F&& func);
  2168. private:
  2169. detail::storage_t
  2170. <
  2171. T
  2172. ,E
  2173. , std::is_copy_constructible<T>::value && std::is_copy_constructible<E>::value
  2174. , std::is_move_constructible<T>::value && std::is_move_constructible<E>::value
  2175. >
  2176. contained;
  2177. };
  2178. /// class expected, void specialization
  2179. template< typename E >
  2180. class expected<void, E>
  2181. {
  2182. private:
  2183. template< typename, typename > friend class expected;
  2184. public:
  2185. using value_type = void;
  2186. using error_type = E;
  2187. using unexpected_type = nonstd::unexpected_type<E>;
  2188. // x.x.4.1 constructors
  2189. constexpr expected() noexcept
  2190. : contained( true )
  2191. {}
  2192. nsel_constexpr14 expected( expected const & other ) = default;
  2193. nsel_constexpr14 expected( expected && other ) = default;
  2194. constexpr explicit expected( nonstd_lite_in_place_t(void) )
  2195. : contained( true )
  2196. {}
  2197. template< typename G = E
  2198. nsel_REQUIRES_T(
  2199. !std::is_convertible<G const &, E>::value /*=> explicit */
  2200. )
  2201. >
  2202. nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> const & error )
  2203. : contained( false )
  2204. {
  2205. contained.construct_error( E{ error.error() } );
  2206. }
  2207. template< typename G = E
  2208. nsel_REQUIRES_T(
  2209. std::is_convertible<G const &, E>::value /*=> non-explicit */
  2210. )
  2211. >
  2212. nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> const & error )
  2213. : contained( false )
  2214. {
  2215. contained.construct_error( error.error() );
  2216. }
  2217. template< typename G = E
  2218. nsel_REQUIRES_T(
  2219. !std::is_convertible<G&&, E>::value /*=> explicit */
  2220. )
  2221. >
  2222. nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> && error )
  2223. : contained( false )
  2224. {
  2225. contained.construct_error( E{ std::move( error.error() ) } );
  2226. }
  2227. template< typename G = E
  2228. nsel_REQUIRES_T(
  2229. std::is_convertible<G&&, E>::value /*=> non-explicit */
  2230. )
  2231. >
  2232. nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> && error )
  2233. : contained( false )
  2234. {
  2235. contained.construct_error( std::move( error.error() ) );
  2236. }
  2237. template< typename... Args
  2238. nsel_REQUIRES_T(
  2239. std::is_constructible<E, Args&&...>::value
  2240. )
  2241. >
  2242. nsel_constexpr14 explicit expected( unexpect_t, Args&&... args )
  2243. : contained( false )
  2244. {
  2245. contained.emplace_error( std::forward<Args>( args )... );
  2246. }
  2247. template< typename U, typename... Args
  2248. nsel_REQUIRES_T(
  2249. std::is_constructible<E, std::initializer_list<U>, Args&&...>::value
  2250. )
  2251. >
  2252. nsel_constexpr14 explicit expected( unexpect_t, std::initializer_list<U> il, Args&&... args )
  2253. : contained( false )
  2254. {
  2255. contained.emplace_error( il, std::forward<Args>( args )... );
  2256. }
  2257. // destructor
  2258. ~expected()
  2259. {
  2260. if ( ! has_value() )
  2261. {
  2262. contained.destruct_error();
  2263. }
  2264. }
  2265. // x.x.4.3 assignment
  2266. expected & operator=( expected const & other )
  2267. {
  2268. expected( other ).swap( *this );
  2269. return *this;
  2270. }
  2271. expected & operator=( expected && other ) noexcept
  2272. (
  2273. std::is_nothrow_move_assignable<E>::value &&
  2274. std::is_nothrow_move_constructible<E>::value )
  2275. {
  2276. expected( std::move( other ) ).swap( *this );
  2277. return *this;
  2278. }
  2279. void emplace()
  2280. {
  2281. expected().swap( *this );
  2282. }
  2283. // x.x.4.4 swap
  2284. template< typename G = E >
  2285. nsel_REQUIRES_R( void,
  2286. std17::is_swappable<G>::value
  2287. && std::is_move_constructible<G>::value
  2288. )
  2289. swap( expected & other ) noexcept
  2290. (
  2291. std::is_nothrow_move_constructible<E>::value && std17::is_nothrow_swappable<E&>::value
  2292. )
  2293. {
  2294. using std::swap;
  2295. if ( ! bool(*this) && ! bool(other) ) { swap( contained.error(), other.contained.error() ); }
  2296. else if ( bool(*this) && ! bool(other) ) { contained.construct_error( std::move( other.error() ) );
  2297. bool has_value = contained.has_value();
  2298. bool other_has_value = other.has_value();
  2299. other.contained.set_has_value(has_value);
  2300. contained.set_has_value(other_has_value);
  2301. }
  2302. else if ( ! bool(*this) && bool(other) ) { other.swap( *this ); }
  2303. }
  2304. // x.x.4.5 observers
  2305. constexpr explicit operator bool() const noexcept
  2306. {
  2307. return has_value();
  2308. }
  2309. constexpr bool has_value() const noexcept
  2310. {
  2311. return contained.has_value();
  2312. }
  2313. void value() const
  2314. {
  2315. if ( ! has_value() )
  2316. {
  2317. error_traits<error_type>::rethrow( contained.error() );
  2318. }
  2319. }
  2320. constexpr error_type const & error() const &
  2321. {
  2322. return assert( ! has_value() ), contained.error();
  2323. }
  2324. error_type & error() &
  2325. {
  2326. return assert( ! has_value() ), contained.error();
  2327. }
  2328. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2329. constexpr error_type const && error() const &&
  2330. {
  2331. return std::move( ( assert( ! has_value() ), contained.error() ) );
  2332. }
  2333. error_type && error() &&
  2334. {
  2335. return std::move( ( assert( ! has_value() ), contained.error() ) );
  2336. }
  2337. #endif
  2338. constexpr unexpected_type get_unexpected() const
  2339. {
  2340. return make_unexpected( contained.error() );
  2341. }
  2342. template< typename Ex >
  2343. bool has_exception() const
  2344. {
  2345. using ContainedEx = typename std::remove_reference< decltype( get_unexpected().value() ) >::type;
  2346. return ! has_value() && std::is_base_of< Ex, ContainedEx>::value;
  2347. }
  2348. #if nsel_P2505R >= 4
  2349. template< typename G = E
  2350. nsel_REQUIRES_T(
  2351. std::is_copy_constructible< E >::value
  2352. && std::is_convertible< G, E >::value
  2353. )
  2354. >
  2355. nsel_constexpr error_type error_or( G && e ) const &
  2356. {
  2357. return has_value()
  2358. ? static_cast< E >( std::forward< G >( e ) )
  2359. : contained.error();
  2360. }
  2361. template< typename G = E
  2362. nsel_REQUIRES_T(
  2363. std::is_move_constructible< E >::value
  2364. && std::is_convertible< G, E >::value
  2365. )
  2366. >
  2367. nsel_constexpr14 error_type error_or( G && e ) &&
  2368. {
  2369. return has_value()
  2370. ? static_cast< E >( std::forward< G >( e ) )
  2371. : std::move( contained.error() );
  2372. }
  2373. #endif // nsel_P2505R >= 4
  2374. #if nsel_P2505R >= 3
  2375. // Monadic operations (P2505)
  2376. template<typename F
  2377. nsel_REQUIRES_T(
  2378. detail::is_expected< detail::invoke_result_nocvref_t< F > >::value
  2379. && std::is_same< typename detail::invoke_result_nocvref_t< F >::error_type, error_type >::value
  2380. && std::is_constructible< error_type, error_type & >::value
  2381. )
  2382. >
  2383. nsel_constexpr14 detail::invoke_result_nocvref_t< F > and_then( F && f ) &
  2384. {
  2385. return has_value()
  2386. ? detail::invoke_result_nocvref_t< F >( detail::invoke( std::forward< F >( f ) ) )
  2387. : detail::invoke_result_nocvref_t< F >( unexpect, error() );
  2388. }
  2389. template<typename F
  2390. nsel_REQUIRES_T(
  2391. detail::is_expected< detail::invoke_result_nocvref_t< F > >::value
  2392. && std::is_same< typename detail::invoke_result_nocvref_t< F >::error_type, error_type >::value
  2393. && std::is_constructible< error_type, const error_type & >::value
  2394. )
  2395. >
  2396. nsel_constexpr detail::invoke_result_nocvref_t< F > and_then( F && f ) const &
  2397. {
  2398. return has_value()
  2399. ? detail::invoke_result_nocvref_t< F >( detail::invoke( std::forward< F >( f ) ) )
  2400. : detail::invoke_result_nocvref_t< F >( unexpect, error() );
  2401. }
  2402. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2403. template<typename F
  2404. nsel_REQUIRES_T(
  2405. detail::is_expected< detail::invoke_result_nocvref_t< F > >::value
  2406. && std::is_same< typename detail::invoke_result_nocvref_t< F >::error_type, error_type >::value
  2407. && std::is_constructible< error_type, error_type && >::value
  2408. )
  2409. >
  2410. nsel_constexpr14 detail::invoke_result_nocvref_t< F > and_then( F && f ) &&
  2411. {
  2412. return has_value()
  2413. ? detail::invoke_result_nocvref_t< F >( detail::invoke( std::forward< F >( f ) ) )
  2414. : detail::invoke_result_nocvref_t< F >( unexpect, std::move( error() ) );
  2415. }
  2416. template<typename F
  2417. nsel_REQUIRES_T(
  2418. detail::is_expected< detail::invoke_result_nocvref_t< F > >::value
  2419. && std::is_same< typename detail::invoke_result_nocvref_t< F >::error_type, error_type >::value
  2420. && std::is_constructible< error_type, const error_type && >::value
  2421. )
  2422. >
  2423. nsel_constexpr detail::invoke_result_nocvref_t< F > and_then( F && f ) const &&
  2424. {
  2425. return has_value()
  2426. ? detail::invoke_result_nocvref_t< F >( detail::invoke( std::forward< F >( f ) ) )
  2427. : detail::invoke_result_nocvref_t< F >( unexpect, std::move( error() ) );
  2428. }
  2429. #endif
  2430. template<typename F
  2431. nsel_REQUIRES_T(
  2432. detail::is_expected< detail::invoke_result_nocvref_t< F, error_type & > >::value
  2433. && std::is_void< typename detail::invoke_result_nocvref_t< F, error_type & >::value_type >::value
  2434. )
  2435. >
  2436. nsel_constexpr14 detail::invoke_result_nocvref_t< F, error_type & > or_else( F && f ) &
  2437. {
  2438. return has_value()
  2439. ? detail::invoke_result_nocvref_t< F, error_type & >()
  2440. : detail::invoke_result_nocvref_t< F, error_type & >( detail::invoke( std::forward< F >( f ), error() ) );
  2441. }
  2442. template<typename F
  2443. nsel_REQUIRES_T(
  2444. detail::is_expected< detail::invoke_result_nocvref_t< F, const error_type & > >::value
  2445. && std::is_void< typename detail::invoke_result_nocvref_t< F, const error_type & >::value_type >::value
  2446. )
  2447. >
  2448. nsel_constexpr detail::invoke_result_nocvref_t< F, const error_type & > or_else( F && f ) const &
  2449. {
  2450. return has_value()
  2451. ? detail::invoke_result_nocvref_t< F, const error_type & >()
  2452. : detail::invoke_result_nocvref_t< F, const error_type & >( detail::invoke( std::forward< F >( f ), error() ) );
  2453. }
  2454. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2455. template<typename F
  2456. nsel_REQUIRES_T(
  2457. detail::is_expected< detail::invoke_result_nocvref_t< F, error_type && > >::value
  2458. && std::is_void< typename detail::invoke_result_nocvref_t< F, error_type && >::value_type >::value
  2459. )
  2460. >
  2461. nsel_constexpr14 detail::invoke_result_nocvref_t< F, error_type && > or_else( F && f ) &&
  2462. {
  2463. return has_value()
  2464. ? detail::invoke_result_nocvref_t< F, error_type && >()
  2465. : detail::invoke_result_nocvref_t< F, error_type && >( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2466. }
  2467. template<typename F
  2468. nsel_REQUIRES_T(
  2469. detail::is_expected< detail::invoke_result_nocvref_t< F, const error_type && > >::value
  2470. && std::is_void< typename detail::invoke_result_nocvref_t< F, const error_type && >::value_type >::value
  2471. )
  2472. >
  2473. nsel_constexpr detail::invoke_result_nocvref_t< F, const error_type && > or_else( F && f ) const &&
  2474. {
  2475. return has_value()
  2476. ? detail::invoke_result_nocvref_t< F, const error_type && >()
  2477. : detail::invoke_result_nocvref_t< F, const error_type && >( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2478. }
  2479. #endif
  2480. template<typename F
  2481. nsel_REQUIRES_T(
  2482. std::is_constructible< error_type, error_type & >::value
  2483. && !std::is_void< detail::transform_invoke_result_t< F > >::value
  2484. )
  2485. >
  2486. nsel_constexpr14 expected< detail::transform_invoke_result_t< F >, error_type > transform( F && f ) &
  2487. {
  2488. return has_value()
  2489. ? expected< detail::transform_invoke_result_t< F >, error_type >( detail::invoke( std::forward< F >( f ) ) )
  2490. : make_unexpected( error() );
  2491. }
  2492. template<typename F
  2493. nsel_REQUIRES_T(
  2494. std::is_constructible< error_type, error_type & >::value
  2495. && std::is_void< detail::transform_invoke_result_t< F > >::value
  2496. )
  2497. >
  2498. nsel_constexpr14 expected< void, error_type > transform( F && f ) &
  2499. {
  2500. return has_value()
  2501. ? ( detail::invoke( std::forward< F >( f ) ), expected< void, error_type >() )
  2502. : make_unexpected( error() );
  2503. }
  2504. template<typename F
  2505. nsel_REQUIRES_T(
  2506. std::is_constructible< error_type, const error_type & >::value
  2507. && !std::is_void< detail::transform_invoke_result_t< F > >::value
  2508. )
  2509. >
  2510. nsel_constexpr expected< detail::transform_invoke_result_t< F >, error_type > transform( F && f ) const &
  2511. {
  2512. return has_value()
  2513. ? expected< detail::transform_invoke_result_t< F >, error_type >( detail::invoke( std::forward< F >( f ) ) )
  2514. : make_unexpected( error() );
  2515. }
  2516. template<typename F
  2517. nsel_REQUIRES_T(
  2518. std::is_constructible< error_type, const error_type & >::value
  2519. && std::is_void< detail::transform_invoke_result_t< F > >::value
  2520. )
  2521. >
  2522. nsel_constexpr expected< void, error_type > transform( F && f ) const &
  2523. {
  2524. return has_value()
  2525. ? ( detail::invoke( std::forward< F >( f ) ), expected< void, error_type >() )
  2526. : make_unexpected( error() );
  2527. }
  2528. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2529. template<typename F
  2530. nsel_REQUIRES_T(
  2531. std::is_constructible< error_type, error_type && >::value
  2532. && !std::is_void< detail::transform_invoke_result_t< F > >::value
  2533. )
  2534. >
  2535. nsel_constexpr14 expected< detail::transform_invoke_result_t< F >, error_type > transform( F && f ) &&
  2536. {
  2537. return has_value()
  2538. ? expected< detail::transform_invoke_result_t< F >, error_type >( detail::invoke( std::forward< F >( f ) ) )
  2539. : make_unexpected( error() );
  2540. }
  2541. template<typename F
  2542. nsel_REQUIRES_T(
  2543. std::is_constructible< error_type, error_type && >::value
  2544. && std::is_void< detail::transform_invoke_result_t< F > >::value
  2545. )
  2546. >
  2547. nsel_constexpr14 expected< void, error_type > transform( F && f ) &&
  2548. {
  2549. return has_value()
  2550. ? ( detail::invoke( std::forward< F >( f ) ), expected< void, error_type >() )
  2551. : make_unexpected( error() );
  2552. }
  2553. template<typename F
  2554. nsel_REQUIRES_T(
  2555. std::is_constructible< error_type, const error_type && >::value
  2556. && !std::is_void< detail::transform_invoke_result_t< F > >::value
  2557. )
  2558. >
  2559. nsel_constexpr expected< detail::transform_invoke_result_t< F >, error_type > transform( F && f ) const &&
  2560. {
  2561. return has_value()
  2562. ? expected< detail::transform_invoke_result_t< F >, error_type >( detail::invoke( std::forward< F >( f ) ) )
  2563. : make_unexpected( error() );
  2564. }
  2565. template<typename F
  2566. nsel_REQUIRES_T(
  2567. std::is_constructible< error_type, const error_type && >::value
  2568. && std::is_void< detail::transform_invoke_result_t< F > >::value
  2569. )
  2570. >
  2571. nsel_constexpr expected< void, error_type > transform( F && f ) const &&
  2572. {
  2573. return has_value()
  2574. ? ( detail::invoke( std::forward< F >( f ) ), expected< void, error_type >() )
  2575. : make_unexpected( error() );
  2576. }
  2577. #endif
  2578. template<typename F
  2579. nsel_REQUIRES_T(
  2580. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, error_type & > >::value
  2581. )
  2582. >
  2583. nsel_constexpr14 expected< void, detail::transform_invoke_result_t< F, error_type & > > transform_error( F && f ) &
  2584. {
  2585. return has_value()
  2586. ? expected< void, detail::transform_invoke_result_t< F, error_type & > >()
  2587. : make_unexpected( detail::invoke( std::forward< F >( f ), error() ) );
  2588. }
  2589. template<typename F
  2590. nsel_REQUIRES_T(
  2591. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, const error_type & > >::value
  2592. )
  2593. >
  2594. nsel_constexpr expected< void, detail::transform_invoke_result_t< F, const error_type & > > transform_error( F && f ) const &
  2595. {
  2596. return has_value()
  2597. ? expected< void, detail::transform_invoke_result_t< F, const error_type & > >()
  2598. : make_unexpected( detail::invoke( std::forward< F >( f ), error() ) );
  2599. }
  2600. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  2601. template<typename F
  2602. nsel_REQUIRES_T(
  2603. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, error_type && > >::value
  2604. )
  2605. >
  2606. nsel_constexpr14 expected< void, detail::transform_invoke_result_t< F, error_type && > > transform_error( F && f ) &&
  2607. {
  2608. return has_value()
  2609. ? expected< void, detail::transform_invoke_result_t< F, error_type && > >()
  2610. : make_unexpected( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2611. }
  2612. template<typename F
  2613. nsel_REQUIRES_T(
  2614. detail::valid_unexpected_type< detail::transform_invoke_result_t< F, const error_type && > >::value
  2615. )
  2616. >
  2617. nsel_constexpr expected< void, detail::transform_invoke_result_t< F, const error_type && > > transform_error( F && f ) const &&
  2618. {
  2619. return has_value()
  2620. ? expected< void, detail::transform_invoke_result_t< F, const error_type && > >()
  2621. : make_unexpected( detail::invoke( std::forward< F >( f ), std::move( error() ) ) );
  2622. }
  2623. #endif
  2624. #endif // nsel_P2505R >= 3
  2625. // template constexpr 'see below' unwrap() const&;
  2626. //
  2627. // template 'see below' unwrap() &&;
  2628. // factories
  2629. // template< typename Ex, typename F>
  2630. // expected<void,E> catch_exception(F&& f);
  2631. //
  2632. // template< typename F>
  2633. // expected<decltype(func()), E> map(F&& func) ;
  2634. //
  2635. // template< typename F>
  2636. // 'see below' bind(F&& func) ;
  2637. //
  2638. // template< typename F>
  2639. // expected<void,E> catch_error(F&& f);
  2640. //
  2641. // template< typename F>
  2642. // 'see below' then(F&& func);
  2643. private:
  2644. detail::storage_t
  2645. <
  2646. void
  2647. , E
  2648. , std::is_copy_constructible<E>::value
  2649. , std::is_move_constructible<E>::value
  2650. >
  2651. contained;
  2652. };
  2653. // x.x.4.6 expected<>: comparison operators
  2654. template< typename T1, typename E1, typename T2, typename E2
  2655. nsel_REQUIRES_T(
  2656. !std::is_void<T1>::value && !std::is_void<T2>::value
  2657. )
  2658. >
  2659. constexpr bool operator==( expected<T1,E1> const & x, expected<T2,E2> const & y )
  2660. {
  2661. return bool(x) != bool(y) ? false : bool(x) ? *x == *y : x.error() == y.error();
  2662. }
  2663. template< typename T1, typename E1, typename T2, typename E2
  2664. nsel_REQUIRES_T(
  2665. std::is_void<T1>::value && std::is_void<T2>::value
  2666. )
  2667. >
  2668. constexpr bool operator==( expected<T1,E1> const & x, expected<T2,E2> const & y )
  2669. {
  2670. return bool(x) != bool(y) ? false : bool(x) || static_cast<bool>( x.error() == y.error() );
  2671. }
  2672. template< typename T1, typename E1, typename T2, typename E2 >
  2673. constexpr bool operator!=( expected<T1,E1> const & x, expected<T2,E2> const & y )
  2674. {
  2675. return !(x == y);
  2676. }
  2677. #if nsel_P0323R <= 2
  2678. template< typename T, typename E >
  2679. constexpr bool operator<( expected<T,E> const & x, expected<T,E> const & y )
  2680. {
  2681. return (!y) ? false : (!x) ? true : *x < *y;
  2682. }
  2683. template< typename T, typename E >
  2684. constexpr bool operator>( expected<T,E> const & x, expected<T,E> const & y )
  2685. {
  2686. return (y < x);
  2687. }
  2688. template< typename T, typename E >
  2689. constexpr bool operator<=( expected<T,E> const & x, expected<T,E> const & y )
  2690. {
  2691. return !(y < x);
  2692. }
  2693. template< typename T, typename E >
  2694. constexpr bool operator>=( expected<T,E> const & x, expected<T,E> const & y )
  2695. {
  2696. return !(x < y);
  2697. }
  2698. #endif
  2699. // x.x.4.7 expected: comparison with T
  2700. template< typename T1, typename E1, typename T2
  2701. nsel_REQUIRES_T(
  2702. !std::is_void<T1>::value
  2703. )
  2704. >
  2705. constexpr bool operator==( expected<T1,E1> const & x, T2 const & v )
  2706. {
  2707. return bool(x) ? *x == v : false;
  2708. }
  2709. template< typename T1, typename E1, typename T2
  2710. nsel_REQUIRES_T(
  2711. !std::is_void<T1>::value
  2712. )
  2713. >
  2714. constexpr bool operator==(T2 const & v, expected<T1,E1> const & x )
  2715. {
  2716. return bool(x) ? v == *x : false;
  2717. }
  2718. template< typename T1, typename E1, typename T2 >
  2719. constexpr bool operator!=( expected<T1,E1> const & x, T2 const & v )
  2720. {
  2721. return bool(x) ? *x != v : true;
  2722. }
  2723. template< typename T1, typename E1, typename T2 >
  2724. constexpr bool operator!=( T2 const & v, expected<T1,E1> const & x )
  2725. {
  2726. return bool(x) ? v != *x : true;
  2727. }
  2728. #if nsel_P0323R <= 2
  2729. template< typename T, typename E >
  2730. constexpr bool operator<( expected<T,E> const & x, T const & v )
  2731. {
  2732. return bool(x) ? *x < v : true;
  2733. }
  2734. template< typename T, typename E >
  2735. constexpr bool operator<( T const & v, expected<T,E> const & x )
  2736. {
  2737. return bool(x) ? v < *x : false;
  2738. }
  2739. template< typename T, typename E >
  2740. constexpr bool operator>( T const & v, expected<T,E> const & x )
  2741. {
  2742. return bool(x) ? *x < v : false;
  2743. }
  2744. template< typename T, typename E >
  2745. constexpr bool operator>( expected<T,E> const & x, T const & v )
  2746. {
  2747. return bool(x) ? v < *x : false;
  2748. }
  2749. template< typename T, typename E >
  2750. constexpr bool operator<=( T const & v, expected<T,E> const & x )
  2751. {
  2752. return bool(x) ? ! ( *x < v ) : false;
  2753. }
  2754. template< typename T, typename E >
  2755. constexpr bool operator<=( expected<T,E> const & x, T const & v )
  2756. {
  2757. return bool(x) ? ! ( v < *x ) : true;
  2758. }
  2759. template< typename T, typename E >
  2760. constexpr bool operator>=( expected<T,E> const & x, T const & v )
  2761. {
  2762. return bool(x) ? ! ( *x < v ) : false;
  2763. }
  2764. template< typename T, typename E >
  2765. constexpr bool operator>=( T const & v, expected<T,E> const & x )
  2766. {
  2767. return bool(x) ? ! ( v < *x ) : true;
  2768. }
  2769. #endif // nsel_P0323R
  2770. // x.x.4.8 expected: comparison with unexpected_type
  2771. template< typename T1, typename E1 , typename E2 >
  2772. constexpr bool operator==( expected<T1,E1> const & x, unexpected_type<E2> const & u )
  2773. {
  2774. return (!x) ? x.get_unexpected() == u : false;
  2775. }
  2776. template< typename T1, typename E1 , typename E2 >
  2777. constexpr bool operator==( unexpected_type<E2> const & u, expected<T1,E1> const & x )
  2778. {
  2779. return ( x == u );
  2780. }
  2781. template< typename T1, typename E1 , typename E2 >
  2782. constexpr bool operator!=( expected<T1,E1> const & x, unexpected_type<E2> const & u )
  2783. {
  2784. return ! ( x == u );
  2785. }
  2786. template< typename T1, typename E1 , typename E2 >
  2787. constexpr bool operator!=( unexpected_type<E2> const & u, expected<T1,E1> const & x )
  2788. {
  2789. return ! ( x == u );
  2790. }
  2791. #if nsel_P0323R <= 2
  2792. template< typename T, typename E >
  2793. constexpr bool operator<( expected<T,E> const & x, unexpected_type<E> const & u )
  2794. {
  2795. return (!x) ? ( x.get_unexpected() < u ) : false;
  2796. }
  2797. template< typename T, typename E >
  2798. constexpr bool operator<( unexpected_type<E> const & u, expected<T,E> const & x )
  2799. {
  2800. return (!x) ? ( u < x.get_unexpected() ) : true ;
  2801. }
  2802. template< typename T, typename E >
  2803. constexpr bool operator>( expected<T,E> const & x, unexpected_type<E> const & u )
  2804. {
  2805. return ( u < x );
  2806. }
  2807. template< typename T, typename E >
  2808. constexpr bool operator>( unexpected_type<E> const & u, expected<T,E> const & x )
  2809. {
  2810. return ( x < u );
  2811. }
  2812. template< typename T, typename E >
  2813. constexpr bool operator<=( expected<T,E> const & x, unexpected_type<E> const & u )
  2814. {
  2815. return ! ( u < x );
  2816. }
  2817. template< typename T, typename E >
  2818. constexpr bool operator<=( unexpected_type<E> const & u, expected<T,E> const & x)
  2819. {
  2820. return ! ( x < u );
  2821. }
  2822. template< typename T, typename E >
  2823. constexpr bool operator>=( expected<T,E> const & x, unexpected_type<E> const & u )
  2824. {
  2825. return ! ( u > x );
  2826. }
  2827. template< typename T, typename E >
  2828. constexpr bool operator>=( unexpected_type<E> const & u, expected<T,E> const & x )
  2829. {
  2830. return ! ( x > u );
  2831. }
  2832. #endif // nsel_P0323R
  2833. /// x.x.x Specialized algorithms
  2834. template< typename T, typename E
  2835. nsel_REQUIRES_T(
  2836. ( std::is_void<T>::value || std::is_move_constructible<T>::value )
  2837. && std::is_move_constructible<E>::value
  2838. && std17::is_swappable<T>::value
  2839. && std17::is_swappable<E>::value )
  2840. >
  2841. void swap( expected<T,E> & x, expected<T,E> & y ) noexcept ( noexcept ( x.swap(y) ) )
  2842. {
  2843. x.swap( y );
  2844. }
  2845. #if nsel_P0323R <= 3
  2846. template< typename T >
  2847. constexpr auto make_expected( T && v ) -> expected< typename std::decay<T>::type >
  2848. {
  2849. return expected< typename std::decay<T>::type >( std::forward<T>( v ) );
  2850. }
  2851. // expected<void> specialization:
  2852. auto inline make_expected() -> expected<void>
  2853. {
  2854. return expected<void>( in_place );
  2855. }
  2856. template< typename T >
  2857. constexpr auto make_expected_from_current_exception() -> expected<T>
  2858. {
  2859. return expected<T>( make_unexpected_from_current_exception() );
  2860. }
  2861. template< typename T >
  2862. auto make_expected_from_exception( std::exception_ptr v ) -> expected<T>
  2863. {
  2864. return expected<T>( unexpected_type<std::exception_ptr>( std::forward<std::exception_ptr>( v ) ) );
  2865. }
  2866. template< typename T, typename E >
  2867. constexpr auto make_expected_from_error( E e ) -> expected<T, typename std::decay<E>::type>
  2868. {
  2869. return expected<T, typename std::decay<E>::type>( make_unexpected( e ) );
  2870. }
  2871. template< typename F
  2872. nsel_REQUIRES_T( ! std::is_same<typename std::result_of<F()>::type, void>::value )
  2873. >
  2874. /*nsel_constexpr14*/
  2875. auto make_expected_from_call( F f ) -> expected< typename std::result_of<F()>::type >
  2876. {
  2877. try
  2878. {
  2879. return make_expected( f() );
  2880. }
  2881. catch (...)
  2882. {
  2883. return make_unexpected_from_current_exception();
  2884. }
  2885. }
  2886. template< typename F
  2887. nsel_REQUIRES_T( std::is_same<typename std::result_of<F()>::type, void>::value )
  2888. >
  2889. /*nsel_constexpr14*/
  2890. auto make_expected_from_call( F f ) -> expected<void>
  2891. {
  2892. try
  2893. {
  2894. f();
  2895. return make_expected();
  2896. }
  2897. catch (...)
  2898. {
  2899. return make_unexpected_from_current_exception();
  2900. }
  2901. }
  2902. #endif // nsel_P0323R
  2903. } // namespace expected_lite
  2904. using namespace expected_lite;
  2905. // using expected_lite::expected;
  2906. // using ...
  2907. } // namespace nonstd
  2908. namespace std {
  2909. // expected: hash support
  2910. template< typename T, typename E >
  2911. struct hash< nonstd::expected<T,E> >
  2912. {
  2913. using result_type = std::size_t;
  2914. using argument_type = nonstd::expected<T,E>;
  2915. constexpr result_type operator()(argument_type const & arg) const
  2916. {
  2917. return arg ? std::hash<T>{}(*arg) : result_type{};
  2918. }
  2919. };
  2920. // TBD - ?? remove? see spec.
  2921. template< typename T, typename E >
  2922. struct hash< nonstd::expected<T&,E> >
  2923. {
  2924. using result_type = std::size_t;
  2925. using argument_type = nonstd::expected<T&,E>;
  2926. constexpr result_type operator()(argument_type const & arg) const
  2927. {
  2928. return arg ? std::hash<T>{}(*arg) : result_type{};
  2929. }
  2930. };
  2931. // TBD - implement
  2932. // bool(e), hash<expected<void,E>>()(e) shall evaluate to the hashing true;
  2933. // otherwise it evaluates to an unspecified value if E is exception_ptr or
  2934. // a combination of hashing false and hash<E>()(e.error()).
  2935. template< typename E >
  2936. struct hash< nonstd::expected<void,E> >
  2937. {
  2938. };
  2939. } // namespace std
  2940. namespace nonstd {
  2941. // void unexpected() is deprecated && removed in C++17
  2942. #if nsel_CPP17_OR_GREATER || nsel_COMPILER_MSVC_VERSION > 141
  2943. template< typename E >
  2944. using unexpected = unexpected_type<E>;
  2945. #endif
  2946. } // namespace nonstd
  2947. #undef nsel_REQUIRES
  2948. #undef nsel_REQUIRES_0
  2949. #undef nsel_REQUIRES_T
  2950. nsel_RESTORE_WARNINGS()
  2951. #endif // nsel_USES_STD_EXPECTED
  2952. #endif // NONSTD_EXPECTED_LITE_HPP