variant.hpp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
  2. #define BOOST_VARIANT2_VARIANT_HPP_INCLUDED
  3. // Copyright 2017-2019 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #if defined(_MSC_VER) && _MSC_VER < 1910
  10. # pragma warning( push )
  11. # pragma warning( disable: 4521 4522 ) // multiple copy operators
  12. #endif
  13. #include <boost/mp11.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/assert/source_location.hpp>
  16. #include <boost/config.hpp>
  17. #include <boost/config/workaround.hpp>
  18. #include <cstddef>
  19. #include <type_traits>
  20. #include <exception>
  21. #include <utility>
  22. #include <typeindex> // std::hash
  23. #include <iosfwd>
  24. #include <cstdint>
  25. #include <cerrno>
  26. //
  27. namespace boost
  28. {
  29. #ifdef BOOST_NO_EXCEPTIONS
  30. BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
  31. #endif
  32. template<class T> struct hash;
  33. namespace variant2
  34. {
  35. // bad_variant_access
  36. class bad_variant_access: public std::exception
  37. {
  38. public:
  39. bad_variant_access() noexcept
  40. {
  41. }
  42. char const * what() const noexcept
  43. {
  44. return "bad_variant_access";
  45. }
  46. };
  47. namespace detail
  48. {
  49. BOOST_NORETURN inline void throw_bad_variant_access()
  50. {
  51. #ifdef BOOST_NO_EXCEPTIONS
  52. boost::throw_exception( bad_variant_access() );
  53. #else
  54. throw bad_variant_access();
  55. #endif
  56. }
  57. } // namespace detail
  58. // monostate
  59. struct monostate
  60. {
  61. };
  62. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1940)
  63. constexpr bool operator<(monostate, monostate) noexcept { return false; }
  64. constexpr bool operator>(monostate, monostate) noexcept { return false; }
  65. constexpr bool operator<=(monostate, monostate) noexcept { return true; }
  66. constexpr bool operator>=(monostate, monostate) noexcept { return true; }
  67. constexpr bool operator==(monostate, monostate) noexcept { return true; }
  68. constexpr bool operator!=(monostate, monostate) noexcept { return false; }
  69. #else
  70. constexpr bool operator<(monostate const&, monostate const&) noexcept { return false; }
  71. constexpr bool operator>(monostate const&, monostate const&) noexcept { return false; }
  72. constexpr bool operator<=(monostate const&, monostate const&) noexcept { return true; }
  73. constexpr bool operator>=(monostate const&, monostate const&) noexcept { return true; }
  74. constexpr bool operator==(monostate const&, monostate const&) noexcept { return true; }
  75. constexpr bool operator!=(monostate const&, monostate const&) noexcept { return false; }
  76. #endif
  77. // variant forward declaration
  78. template<class... T> class variant;
  79. // variant_size
  80. template<class T> struct variant_size
  81. {
  82. };
  83. template<class T> struct variant_size<T const>: variant_size<T>
  84. {
  85. };
  86. template<class T> struct variant_size<T volatile>: variant_size<T>
  87. {
  88. };
  89. template<class T> struct variant_size<T const volatile>: variant_size<T>
  90. {
  91. };
  92. template<class T> struct variant_size<T&>: variant_size<T>
  93. {
  94. };
  95. template<class T> struct variant_size<T&&>: variant_size<T>
  96. {
  97. };
  98. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  99. template <class T> /*inline*/ constexpr std::size_t variant_size_v = variant_size<T>::value;
  100. #endif
  101. template <class... T> struct variant_size<variant<T...>>: mp11::mp_size<variant<T...>>
  102. {
  103. };
  104. // variant_alternative
  105. template<std::size_t I, class T> struct variant_alternative;
  106. template<std::size_t I, class T> using variant_alternative_t = typename variant_alternative<I, T>::type;
  107. #if BOOST_WORKAROUND(BOOST_GCC, < 40900)
  108. namespace detail
  109. {
  110. template<std::size_t I, class T, bool E> struct variant_alternative_impl
  111. {
  112. };
  113. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...>, true>
  114. {
  115. using type = mp11::mp_at_c<variant<T...>, I>;
  116. };
  117. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const, true>: std::add_const< mp11::mp_at_c<variant<T...>, I> >
  118. {
  119. };
  120. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> volatile, true>: std::add_volatile< mp11::mp_at_c<variant<T...>, I> >
  121. {
  122. };
  123. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const volatile, true>: std::add_cv< mp11::mp_at_c<variant<T...>, I> >
  124. {
  125. };
  126. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...>&, true>: std::add_lvalue_reference< mp11::mp_at_c<variant<T...>, I> >
  127. {
  128. };
  129. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const&, true>: std::add_lvalue_reference< mp11::mp_at_c<variant<T...>, I> const >
  130. {
  131. };
  132. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c<variant<T...>, I> volatile >
  133. {
  134. };
  135. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c<variant<T...>, I> const volatile >
  136. {
  137. };
  138. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...>&&, true>: std::add_rvalue_reference< mp11::mp_at_c<variant<T...>, I> >
  139. {
  140. };
  141. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const&&, true>: std::add_rvalue_reference< mp11::mp_at_c<variant<T...>, I> const >
  142. {
  143. };
  144. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c<variant<T...>, I> volatile >
  145. {
  146. };
  147. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...> const volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c<variant<T...>, I> const volatile >
  148. {
  149. };
  150. } // namespace detail
  151. template<std::size_t I, class T> struct variant_alternative
  152. {
  153. };
  154. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>>: public detail::variant_alternative_impl<I, variant<T...>, (I < sizeof...(T))>
  155. {
  156. };
  157. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const>: public detail::variant_alternative_impl<I, variant<T...> const, (I < sizeof...(T))>
  158. {
  159. };
  160. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> volatile>: public detail::variant_alternative_impl<I, variant<T...> volatile, (I < sizeof...(T))>
  161. {
  162. };
  163. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const volatile>: public detail::variant_alternative_impl<I, variant<T...> const volatile, (I < sizeof...(T))>
  164. {
  165. };
  166. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>&>: public detail::variant_alternative_impl<I, variant<T...>&, (I < sizeof...(T))>
  167. {
  168. };
  169. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const&>: public detail::variant_alternative_impl<I, variant<T...> const&, (I < sizeof...(T))>
  170. {
  171. };
  172. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> volatile&>: public detail::variant_alternative_impl<I, variant<T...> volatile&, (I < sizeof...(T))>
  173. {
  174. };
  175. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const volatile&>: public detail::variant_alternative_impl<I, variant<T...> const volatile&, (I < sizeof...(T))>
  176. {
  177. };
  178. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>&&>: public detail::variant_alternative_impl<I, variant<T...>&&, (I < sizeof...(T))>
  179. {
  180. };
  181. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const&&>: public detail::variant_alternative_impl<I, variant<T...> const&&, (I < sizeof...(T))>
  182. {
  183. };
  184. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> volatile&&>: public detail::variant_alternative_impl<I, variant<T...> volatile&&, (I < sizeof...(T))>
  185. {
  186. };
  187. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...> const volatile&&>: public detail::variant_alternative_impl<I, variant<T...> const volatile&&, (I < sizeof...(T))>
  188. {
  189. };
  190. #else
  191. namespace detail
  192. {
  193. #if defined( BOOST_MP11_VERSION ) && BOOST_MP11_VERSION >= 107000
  194. template<class I, class T, class Q> using var_alt_impl = mp11::mp_invoke_q<Q, variant_alternative_t<I::value, T>>;
  195. #else
  196. template<class I, class T, class Q> using var_alt_impl = mp11::mp_invoke<Q, variant_alternative_t<I::value, T>>;
  197. #endif
  198. } // namespace detail
  199. template<std::size_t I, class T> struct variant_alternative
  200. {
  201. };
  202. template<std::size_t I, class T> struct variant_alternative<I, T const>: mp11::mp_defer<detail::var_alt_impl, mp11::mp_size_t<I>, T, mp11::mp_quote_trait<std::add_const>>
  203. {
  204. };
  205. template<std::size_t I, class T> struct variant_alternative<I, T volatile>: mp11::mp_defer<detail::var_alt_impl, mp11::mp_size_t<I>, T, mp11::mp_quote_trait<std::add_volatile>>
  206. {
  207. };
  208. template<std::size_t I, class T> struct variant_alternative<I, T const volatile>: mp11::mp_defer<detail::var_alt_impl, mp11::mp_size_t<I>, T, mp11::mp_quote_trait<std::add_cv>>
  209. {
  210. };
  211. template<std::size_t I, class T> struct variant_alternative<I, T&>: mp11::mp_defer<detail::var_alt_impl, mp11::mp_size_t<I>, T, mp11::mp_quote_trait<std::add_lvalue_reference>>
  212. {
  213. };
  214. template<std::size_t I, class T> struct variant_alternative<I, T&&>: mp11::mp_defer<detail::var_alt_impl, mp11::mp_size_t<I>, T, mp11::mp_quote_trait<std::add_rvalue_reference>>
  215. {
  216. };
  217. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>>: mp11::mp_defer<mp11::mp_at, variant<T...>, mp11::mp_size_t<I>>
  218. {
  219. };
  220. #endif
  221. // variant_npos
  222. constexpr std::size_t variant_npos = ~static_cast<std::size_t>( 0 );
  223. // holds_alternative
  224. template<class U, class... T> constexpr bool holds_alternative( variant<T...> const& v ) noexcept
  225. {
  226. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  227. return v.index() == mp11::mp_find<variant<T...>, U>::value;
  228. }
  229. // get (index)
  230. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
  231. {
  232. static_assert( I < sizeof...(T), "Index out of bounds" );
  233. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t<I>() );
  234. }
  235. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& get(variant<T...>&& v)
  236. {
  237. static_assert( I < sizeof...(T), "Index out of bounds" );
  238. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  239. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  240. #else
  241. if( v.index() != I ) detail::throw_bad_variant_access();
  242. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  243. #endif
  244. }
  245. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const& get(variant<T...> const& v)
  246. {
  247. static_assert( I < sizeof...(T), "Index out of bounds" );
  248. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t<I>() );
  249. }
  250. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const&& get(variant<T...> const&& v)
  251. {
  252. static_assert( I < sizeof...(T), "Index out of bounds" );
  253. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  254. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  255. #else
  256. if( v.index() != I ) detail::throw_bad_variant_access();
  257. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  258. #endif
  259. }
  260. // unsafe_get
  261. #if !defined(BOOST_NO_CXX14_CONSTEXPR)
  262. # define BOOST_VARIANT2_CX14_ASSERT(expr) BOOST_ASSERT(expr);
  263. #else
  264. # define BOOST_VARIANT2_CX14_ASSERT(expr)
  265. #endif
  266. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& unsafe_get(variant<T...>& v)
  267. {
  268. static_assert( I < sizeof...(T), "Index out of bounds" );
  269. BOOST_VARIANT2_CX14_ASSERT( v.index() == I )
  270. return v._get_impl( mp11::mp_size_t<I>() );
  271. }
  272. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& unsafe_get(variant<T...>&& v)
  273. {
  274. static_assert( I < sizeof...(T), "Index out of bounds" );
  275. BOOST_VARIANT2_CX14_ASSERT( v.index() == I )
  276. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  277. }
  278. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const& unsafe_get(variant<T...> const& v)
  279. {
  280. static_assert( I < sizeof...(T), "Index out of bounds" );
  281. BOOST_VARIANT2_CX14_ASSERT( v.index() == I )
  282. return v._get_impl( mp11::mp_size_t<I>() );
  283. }
  284. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const&& unsafe_get(variant<T...> const&& v)
  285. {
  286. static_assert( I < sizeof...(T), "Index out of bounds" );
  287. BOOST_VARIANT2_CX14_ASSERT( v.index() == I )
  288. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  289. }
  290. // get (type)
  291. template<class U, class... T> constexpr U& get(variant<T...>& v)
  292. {
  293. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  294. using I = mp11::mp_find<variant<T...>, U>;
  295. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
  296. }
  297. template<class U, class... T> constexpr U&& get(variant<T...>&& v)
  298. {
  299. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  300. using I = mp11::mp_find<variant<T...>, U>;
  301. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  302. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
  303. #else
  304. if( v.index() != I::value ) detail::throw_bad_variant_access();
  305. return std::move( v._get_impl( I() ) );
  306. #endif
  307. }
  308. template<class U, class... T> constexpr U const& get(variant<T...> const& v)
  309. {
  310. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  311. using I = mp11::mp_find<variant<T...>, U>;
  312. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
  313. }
  314. template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
  315. {
  316. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  317. using I = mp11::mp_find<variant<T...>, U>;
  318. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  319. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
  320. #else
  321. if( v.index() != I::value ) detail::throw_bad_variant_access();
  322. return std::move( v._get_impl( I() ) );
  323. #endif
  324. }
  325. // get_if
  326. template<std::size_t I, class... T> constexpr typename std::add_pointer<variant_alternative_t<I, variant<T...>>>::type get_if(variant<T...>* v) noexcept
  327. {
  328. static_assert( I < sizeof...(T), "Index out of bounds" );
  329. return v && v->index() == I? &v->_get_impl( mp11::mp_size_t<I>() ): 0;
  330. }
  331. template<std::size_t I, class... T> constexpr typename std::add_pointer<const variant_alternative_t<I, variant<T...>>>::type get_if(variant<T...> const * v) noexcept
  332. {
  333. static_assert( I < sizeof...(T), "Index out of bounds" );
  334. return v && v->index() == I? &v->_get_impl( mp11::mp_size_t<I>() ): 0;
  335. }
  336. template<class U, class... T> constexpr typename std::add_pointer<U>::type get_if(variant<T...>* v) noexcept
  337. {
  338. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  339. using I = mp11::mp_find<variant<T...>, U>;
  340. return v && v->index() == I::value? &v->_get_impl( I() ): 0;
  341. }
  342. template<class U, class... T> constexpr typename std::add_pointer<U const>::type get_if(variant<T...> const * v) noexcept
  343. {
  344. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  345. using I = mp11::mp_find<variant<T...>, U>;
  346. return v && v->index() == I::value? &v->_get_impl( I() ): 0;
  347. }
  348. //
  349. namespace detail
  350. {
  351. // trivially_*
  352. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  353. template<class T> struct is_trivially_copy_constructible: mp11::mp_bool<std::is_copy_constructible<T>::value && std::has_trivial_copy_constructor<T>::value>
  354. {
  355. };
  356. template<class T> struct is_trivially_copy_assignable: mp11::mp_bool<std::is_copy_assignable<T>::value && std::has_trivial_copy_assign<T>::value>
  357. {
  358. };
  359. template<class T> struct is_trivially_move_constructible: mp11::mp_bool<std::is_move_constructible<T>::value && std::is_trivial<T>::value>
  360. {
  361. };
  362. template<class T> struct is_trivially_move_assignable: mp11::mp_bool<std::is_move_assignable<T>::value && std::is_trivial<T>::value>
  363. {
  364. };
  365. #else
  366. using std::is_trivially_copy_constructible;
  367. using std::is_trivially_copy_assignable;
  368. using std::is_trivially_move_constructible;
  369. using std::is_trivially_move_assignable;
  370. #endif
  371. // variant_storage
  372. template<class D, class... T> union variant_storage_impl;
  373. template<class... T> using variant_storage = variant_storage_impl<mp11::mp_all<std::is_trivially_destructible<T>...>, T...>;
  374. template<class D> union variant_storage_impl<D>
  375. {
  376. };
  377. // not all trivially destructible
  378. template<class T1, class... T> union variant_storage_impl<mp11::mp_false, T1, T...>
  379. {
  380. T1 first_;
  381. variant_storage<T...> rest_;
  382. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward<A>(a)... )
  383. {
  384. }
  385. template<std::size_t I, class... A> constexpr variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-1>(), std::forward<A>(a)... )
  386. {
  387. }
  388. ~variant_storage_impl()
  389. {
  390. }
  391. template<class... A> void emplace( mp11::mp_size_t<0>, A&&... a )
  392. {
  393. ::new( &first_ ) T1( std::forward<A>(a)... );
  394. }
  395. template<std::size_t I, class... A> void emplace( mp11::mp_size_t<I>, A&&... a )
  396. {
  397. rest_.emplace( mp11::mp_size_t<I-1>(), std::forward<A>(a)... );
  398. }
  399. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; }
  400. constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; }
  401. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<mp11::mp_list<T...>, I-1>& get( mp11::mp_size_t<I> ) noexcept { return rest_.get( mp11::mp_size_t<I-1>() ); }
  402. template<std::size_t I> constexpr mp11::mp_at_c<mp11::mp_list<T...>, I-1> const& get( mp11::mp_size_t<I> ) const noexcept { return rest_.get( mp11::mp_size_t<I-1>() ); }
  403. };
  404. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class... T> union variant_storage_impl<mp11::mp_false, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T...>
  405. {
  406. T0 t0_;
  407. T1 t1_;
  408. T2 t2_;
  409. T3 t3_;
  410. T4 t4_;
  411. T5 t5_;
  412. T6 t6_;
  413. T7 t7_;
  414. T8 t8_;
  415. T9 t9_;
  416. variant_storage<T...> rest_;
  417. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): t0_( std::forward<A>(a)... ) {}
  418. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<1>, A&&... a ): t1_( std::forward<A>(a)... ) {}
  419. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<2>, A&&... a ): t2_( std::forward<A>(a)... ) {}
  420. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<3>, A&&... a ): t3_( std::forward<A>(a)... ) {}
  421. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<4>, A&&... a ): t4_( std::forward<A>(a)... ) {}
  422. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<5>, A&&... a ): t5_( std::forward<A>(a)... ) {}
  423. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<6>, A&&... a ): t6_( std::forward<A>(a)... ) {}
  424. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<7>, A&&... a ): t7_( std::forward<A>(a)... ) {}
  425. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<8>, A&&... a ): t8_( std::forward<A>(a)... ) {}
  426. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<9>, A&&... a ): t9_( std::forward<A>(a)... ) {}
  427. template<std::size_t I, class... A> constexpr variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-10>(), std::forward<A>(a)... ) {}
  428. ~variant_storage_impl()
  429. {
  430. }
  431. template<class... A> void emplace( mp11::mp_size_t<0>, A&&... a ) { ::new( &t0_ ) T0( std::forward<A>(a)... ); }
  432. template<class... A> void emplace( mp11::mp_size_t<1>, A&&... a ) { ::new( &t1_ ) T1( std::forward<A>(a)... ); }
  433. template<class... A> void emplace( mp11::mp_size_t<2>, A&&... a ) { ::new( &t2_ ) T2( std::forward<A>(a)... ); }
  434. template<class... A> void emplace( mp11::mp_size_t<3>, A&&... a ) { ::new( &t3_ ) T3( std::forward<A>(a)... ); }
  435. template<class... A> void emplace( mp11::mp_size_t<4>, A&&... a ) { ::new( &t4_ ) T4( std::forward<A>(a)... ); }
  436. template<class... A> void emplace( mp11::mp_size_t<5>, A&&... a ) { ::new( &t5_ ) T5( std::forward<A>(a)... ); }
  437. template<class... A> void emplace( mp11::mp_size_t<6>, A&&... a ) { ::new( &t6_ ) T6( std::forward<A>(a)... ); }
  438. template<class... A> void emplace( mp11::mp_size_t<7>, A&&... a ) { ::new( &t7_ ) T7( std::forward<A>(a)... ); }
  439. template<class... A> void emplace( mp11::mp_size_t<8>, A&&... a ) { ::new( &t8_ ) T8( std::forward<A>(a)... ); }
  440. template<class... A> void emplace( mp11::mp_size_t<9>, A&&... a ) { ::new( &t9_ ) T9( std::forward<A>(a)... ); }
  441. template<std::size_t I, class... A> void emplace( mp11::mp_size_t<I>, A&&... a )
  442. {
  443. rest_.emplace( mp11::mp_size_t<I-10>(), std::forward<A>(a)... );
  444. }
  445. BOOST_CXX14_CONSTEXPR T0& get( mp11::mp_size_t<0> ) noexcept { return t0_; }
  446. constexpr T0 const& get( mp11::mp_size_t<0> ) const noexcept { return t0_; }
  447. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<1> ) noexcept { return t1_; }
  448. constexpr T1 const& get( mp11::mp_size_t<1> ) const noexcept { return t1_; }
  449. BOOST_CXX14_CONSTEXPR T2& get( mp11::mp_size_t<2> ) noexcept { return t2_; }
  450. constexpr T2 const& get( mp11::mp_size_t<2> ) const noexcept { return t2_; }
  451. BOOST_CXX14_CONSTEXPR T3& get( mp11::mp_size_t<3> ) noexcept { return t3_; }
  452. constexpr T3 const& get( mp11::mp_size_t<3> ) const noexcept { return t3_; }
  453. BOOST_CXX14_CONSTEXPR T4& get( mp11::mp_size_t<4> ) noexcept { return t4_; }
  454. constexpr T4 const& get( mp11::mp_size_t<4> ) const noexcept { return t4_; }
  455. BOOST_CXX14_CONSTEXPR T5& get( mp11::mp_size_t<5> ) noexcept { return t5_; }
  456. constexpr T5 const& get( mp11::mp_size_t<5> ) const noexcept { return t5_; }
  457. BOOST_CXX14_CONSTEXPR T6& get( mp11::mp_size_t<6> ) noexcept { return t6_; }
  458. constexpr T6 const& get( mp11::mp_size_t<6> ) const noexcept { return t6_; }
  459. BOOST_CXX14_CONSTEXPR T7& get( mp11::mp_size_t<7> ) noexcept { return t7_; }
  460. constexpr T7 const& get( mp11::mp_size_t<7> ) const noexcept { return t7_; }
  461. BOOST_CXX14_CONSTEXPR T8& get( mp11::mp_size_t<8> ) noexcept { return t8_; }
  462. constexpr T8 const& get( mp11::mp_size_t<8> ) const noexcept { return t8_; }
  463. BOOST_CXX14_CONSTEXPR T9& get( mp11::mp_size_t<9> ) noexcept { return t9_; }
  464. constexpr T9 const& get( mp11::mp_size_t<9> ) const noexcept { return t9_; }
  465. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<mp11::mp_list<T...>, I-10>& get( mp11::mp_size_t<I> ) noexcept { return rest_.get( mp11::mp_size_t<I-10>() ); }
  466. template<std::size_t I> constexpr mp11::mp_at_c<mp11::mp_list<T...>, I-10> const& get( mp11::mp_size_t<I> ) const noexcept { return rest_.get( mp11::mp_size_t<I-10>() ); }
  467. };
  468. // all trivially destructible
  469. template<class T1, class... T> union variant_storage_impl<mp11::mp_true, T1, T...>
  470. {
  471. T1 first_;
  472. variant_storage<T...> rest_;
  473. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward<A>(a)... )
  474. {
  475. }
  476. template<std::size_t I, class... A> constexpr variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-1>(), std::forward<A>(a)... )
  477. {
  478. }
  479. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a )
  480. {
  481. ::new( &first_ ) T1( std::forward<A>(a)... );
  482. }
  483. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t<I>, A&&... a )
  484. {
  485. rest_.emplace( mp11::mp_size_t<I-1>(), std::forward<A>(a)... );
  486. }
  487. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t<I>, A&&... a )
  488. {
  489. #if defined(BOOST_GCC) && (__GNUC__ >= 7)
  490. # pragma GCC diagnostic push
  491. // False positive in at least GCC 7 and GCC 10 ASAN triggered by monostate (via result<void>)
  492. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  493. #if __GNUC__ >= 12
  494. // False positive in at least GCC 12 and GCC 13 ASAN and -Og triggered by monostate (via result<void>)
  495. # pragma GCC diagnostic ignored "-Wuninitialized"
  496. #endif
  497. #endif
  498. *this = variant_storage_impl( mp11::mp_size_t<I>(), std::forward<A>(a)... );
  499. #if defined(BOOST_GCC) && (__GNUC__ >= 7)
  500. # pragma GCC diagnostic pop
  501. #endif
  502. }
  503. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t<I>, A&&... a )
  504. {
  505. this->emplace_impl( mp11::mp_all<detail::is_trivially_move_assignable<T1>, detail::is_trivially_move_assignable<T>...>(), mp11::mp_size_t<I>(), std::forward<A>(a)... );
  506. }
  507. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; }
  508. constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; }
  509. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<mp11::mp_list<T...>, I-1>& get( mp11::mp_size_t<I> ) noexcept { return rest_.get( mp11::mp_size_t<I-1>() ); }
  510. template<std::size_t I> constexpr mp11::mp_at_c<mp11::mp_list<T...>, I-1> const& get( mp11::mp_size_t<I> ) const noexcept { return rest_.get( mp11::mp_size_t<I-1>() ); }
  511. };
  512. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class... T> union variant_storage_impl<mp11::mp_true, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T...>
  513. {
  514. T0 t0_;
  515. T1 t1_;
  516. T2 t2_;
  517. T3 t3_;
  518. T4 t4_;
  519. T5 t5_;
  520. T6 t6_;
  521. T7 t7_;
  522. T8 t8_;
  523. T9 t9_;
  524. variant_storage<T...> rest_;
  525. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): t0_( std::forward<A>(a)... ) {}
  526. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<1>, A&&... a ): t1_( std::forward<A>(a)... ) {}
  527. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<2>, A&&... a ): t2_( std::forward<A>(a)... ) {}
  528. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<3>, A&&... a ): t3_( std::forward<A>(a)... ) {}
  529. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<4>, A&&... a ): t4_( std::forward<A>(a)... ) {}
  530. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<5>, A&&... a ): t5_( std::forward<A>(a)... ) {}
  531. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<6>, A&&... a ): t6_( std::forward<A>(a)... ) {}
  532. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<7>, A&&... a ): t7_( std::forward<A>(a)... ) {}
  533. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<8>, A&&... a ): t8_( std::forward<A>(a)... ) {}
  534. template<class... A> constexpr variant_storage_impl( mp11::mp_size_t<9>, A&&... a ): t9_( std::forward<A>(a)... ) {}
  535. template<std::size_t I, class... A> constexpr variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-10>(), std::forward<A>(a)... ) {}
  536. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a ) { ::new( &t0_ ) T0( std::forward<A>(a)... ); }
  537. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<1>, A&&... a ) { ::new( &t1_ ) T1( std::forward<A>(a)... ); }
  538. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<2>, A&&... a ) { ::new( &t2_ ) T2( std::forward<A>(a)... ); }
  539. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<3>, A&&... a ) { ::new( &t3_ ) T3( std::forward<A>(a)... ); }
  540. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<4>, A&&... a ) { ::new( &t4_ ) T4( std::forward<A>(a)... ); }
  541. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<5>, A&&... a ) { ::new( &t5_ ) T5( std::forward<A>(a)... ); }
  542. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<6>, A&&... a ) { ::new( &t6_ ) T6( std::forward<A>(a)... ); }
  543. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<7>, A&&... a ) { ::new( &t7_ ) T7( std::forward<A>(a)... ); }
  544. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<8>, A&&... a ) { ::new( &t8_ ) T8( std::forward<A>(a)... ); }
  545. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<9>, A&&... a ) { ::new( &t9_ ) T9( std::forward<A>(a)... ); }
  546. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t<I>, A&&... a )
  547. {
  548. rest_.emplace( mp11::mp_size_t<I-10>(), std::forward<A>(a)... );
  549. }
  550. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t<I>, A&&... a )
  551. {
  552. *this = variant_storage_impl( mp11::mp_size_t<I>(), std::forward<A>(a)... );
  553. }
  554. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t<I>, A&&... a )
  555. {
  556. this->emplace_impl( mp11::mp_all<
  557. detail::is_trivially_move_assignable<T0>,
  558. detail::is_trivially_move_assignable<T1>,
  559. detail::is_trivially_move_assignable<T2>,
  560. detail::is_trivially_move_assignable<T3>,
  561. detail::is_trivially_move_assignable<T4>,
  562. detail::is_trivially_move_assignable<T5>,
  563. detail::is_trivially_move_assignable<T6>,
  564. detail::is_trivially_move_assignable<T7>,
  565. detail::is_trivially_move_assignable<T8>,
  566. detail::is_trivially_move_assignable<T9>,
  567. detail::is_trivially_move_assignable<T>...>(), mp11::mp_size_t<I>(), std::forward<A>(a)... );
  568. }
  569. BOOST_CXX14_CONSTEXPR T0& get( mp11::mp_size_t<0> ) noexcept { return t0_; }
  570. constexpr T0 const& get( mp11::mp_size_t<0> ) const noexcept { return t0_; }
  571. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<1> ) noexcept { return t1_; }
  572. constexpr T1 const& get( mp11::mp_size_t<1> ) const noexcept { return t1_; }
  573. BOOST_CXX14_CONSTEXPR T2& get( mp11::mp_size_t<2> ) noexcept { return t2_; }
  574. constexpr T2 const& get( mp11::mp_size_t<2> ) const noexcept { return t2_; }
  575. BOOST_CXX14_CONSTEXPR T3& get( mp11::mp_size_t<3> ) noexcept { return t3_; }
  576. constexpr T3 const& get( mp11::mp_size_t<3> ) const noexcept { return t3_; }
  577. BOOST_CXX14_CONSTEXPR T4& get( mp11::mp_size_t<4> ) noexcept { return t4_; }
  578. constexpr T4 const& get( mp11::mp_size_t<4> ) const noexcept { return t4_; }
  579. BOOST_CXX14_CONSTEXPR T5& get( mp11::mp_size_t<5> ) noexcept { return t5_; }
  580. constexpr T5 const& get( mp11::mp_size_t<5> ) const noexcept { return t5_; }
  581. BOOST_CXX14_CONSTEXPR T6& get( mp11::mp_size_t<6> ) noexcept { return t6_; }
  582. constexpr T6 const& get( mp11::mp_size_t<6> ) const noexcept { return t6_; }
  583. BOOST_CXX14_CONSTEXPR T7& get( mp11::mp_size_t<7> ) noexcept { return t7_; }
  584. constexpr T7 const& get( mp11::mp_size_t<7> ) const noexcept { return t7_; }
  585. BOOST_CXX14_CONSTEXPR T8& get( mp11::mp_size_t<8> ) noexcept { return t8_; }
  586. constexpr T8 const& get( mp11::mp_size_t<8> ) const noexcept { return t8_; }
  587. BOOST_CXX14_CONSTEXPR T9& get( mp11::mp_size_t<9> ) noexcept { return t9_; }
  588. constexpr T9 const& get( mp11::mp_size_t<9> ) const noexcept { return t9_; }
  589. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<mp11::mp_list<T...>, I-10>& get( mp11::mp_size_t<I> ) noexcept { return rest_.get( mp11::mp_size_t<I-10>() ); }
  590. template<std::size_t I> constexpr mp11::mp_at_c<mp11::mp_list<T...>, I-10> const& get( mp11::mp_size_t<I> ) const noexcept { return rest_.get( mp11::mp_size_t<I-10>() ); }
  591. };
  592. // resolve_overload_*
  593. template<class... T> struct overload;
  594. template<> struct overload<>
  595. {
  596. void operator()() const;
  597. };
  598. template<class T1, class... T> struct overload<T1, T...>: overload<T...>
  599. {
  600. using overload<T...>::operator();
  601. mp11::mp_identity<T1> operator()(T1) const;
  602. };
  603. #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )
  604. template<class U, class... T> using resolve_overload_type_ = decltype( overload<T...>()(std::declval<U>()) );
  605. template<class U, class... T> struct resolve_overload_type_impl: mp11::mp_defer< resolve_overload_type_, U, T... >
  606. {
  607. };
  608. template<class U, class... T> using resolve_overload_type = typename resolve_overload_type_impl<U, T...>::type::type;
  609. #else
  610. template<class U, class... T> using resolve_overload_type = typename decltype( overload<T...>()(std::declval<U>()) )::type;
  611. #endif
  612. template<class U, class... T> using resolve_overload_index = mp11::mp_find<mp11::mp_list<T...>, resolve_overload_type<U, T...>>;
  613. // variant_base
  614. template<bool is_trivially_destructible, bool is_single_buffered, class... T> struct variant_base_impl;
  615. template<class... T> using variant_base = variant_base_impl<mp11::mp_all<std::is_trivially_destructible<T>...>::value, mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value, T...>;
  616. struct none {};
  617. // trivially destructible, single buffered
  618. template<class... T> struct variant_base_impl<true, true, T...>
  619. {
  620. variant_storage<none, T...> st_;
  621. unsigned ix_;
  622. constexpr variant_base_impl(): st_( mp11::mp_size_t<0>() ), ix_( 0 )
  623. {
  624. }
  625. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), ix_( I::value + 1 )
  626. {
  627. }
  628. // requires: ix_ == 0
  629. template<class I, class... A> void _replace( I, A&&... a )
  630. {
  631. ::new( &st_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  632. ix_ = I::value + 1;
  633. }
  634. constexpr std::size_t index() const noexcept
  635. {
  636. return ix_ - 1;
  637. }
  638. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  639. {
  640. size_t const J = I+1;
  641. BOOST_ASSERT( ix_ == J );
  642. return st_.get( mp11::mp_size_t<J>() );
  643. }
  644. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  645. {
  646. // size_t const J = I+1;
  647. BOOST_VARIANT2_CX14_ASSERT( ix_ == I+1 )
  648. return st_.get( mp11::mp_size_t<I+1>() );
  649. }
  650. template<std::size_t J, class U, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, A&&... a )
  651. {
  652. static_assert( std::is_nothrow_constructible<U, A&&...>::value, "Logic error: U must be nothrow constructible from A&&..." );
  653. st_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  654. ix_ = J;
  655. }
  656. template<std::size_t J, class U, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, A&&... a )
  657. {
  658. static_assert( std::is_nothrow_move_constructible<U>::value, "Logic error: U must be nothrow move constructible" );
  659. U tmp( std::forward<A>(a)... );
  660. st_.emplace( mp11::mp_size_t<J>(), std::move(tmp) );
  661. ix_ = J;
  662. }
  663. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( A&&... a )
  664. {
  665. std::size_t const J = I+1;
  666. using U = mp11::mp_at_c<variant<T...>, I>;
  667. this->emplace_impl<J, U>( std::is_nothrow_constructible<U, A&&...>(), std::forward<A>(a)... );
  668. }
  669. static constexpr bool uses_double_storage() noexcept
  670. {
  671. return false;
  672. }
  673. };
  674. // trivially destructible, double buffered
  675. template<class... T> struct variant_base_impl<true, false, T...>
  676. {
  677. variant_storage<none, T...> st_[ 2 ];
  678. unsigned ix_;
  679. constexpr variant_base_impl(): st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }, ix_( 0 )
  680. {
  681. }
  682. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }, ix_( ( I::value + 1 ) * 2 )
  683. {
  684. }
  685. // requires: ix_ == 0
  686. template<class I, class... A> void _replace( I, A&&... a )
  687. {
  688. ::new( &st_[ 0 ] ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  689. ix_ = ( I::value + 1 ) * 2;
  690. }
  691. constexpr std::size_t index() const noexcept
  692. {
  693. return ix_ / 2 - 1;
  694. }
  695. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  696. {
  697. BOOST_ASSERT( index() == I );
  698. size_t const J = I+1;
  699. constexpr mp11::mp_size_t<J> j{};
  700. return st_[ ix_ & 1 ].get( j );
  701. }
  702. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  703. {
  704. BOOST_VARIANT2_CX14_ASSERT( index() == I )
  705. // size_t const J = I+1;
  706. // constexpr mp_size_t<J> j{};
  707. return st_[ ix_ & 1 ].get( mp11::mp_size_t<I+1>() );
  708. }
  709. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( A&&... a )
  710. {
  711. size_t const J = I+1;
  712. unsigned i2 = 1 - ( ix_ & 1 );
  713. st_[ i2 ].emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  714. ix_ = J * 2 + i2;
  715. }
  716. static constexpr bool uses_double_storage() noexcept
  717. {
  718. return true;
  719. }
  720. };
  721. // not trivially destructible, single buffered
  722. template<class... T> struct variant_base_impl<false, true, T...>
  723. {
  724. variant_storage<none, T...> st_;
  725. unsigned ix_;
  726. constexpr variant_base_impl(): st_( mp11::mp_size_t<0>() ), ix_( 0 )
  727. {
  728. }
  729. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), ix_( I::value + 1 )
  730. {
  731. }
  732. // requires: ix_ == 0
  733. template<class I, class... A> void _replace( I, A&&... a )
  734. {
  735. ::new( &st_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  736. ix_ = I::value + 1;
  737. }
  738. //[&]( auto I ){
  739. // using U = mp_at_c<mp_list<none, T...>, I>;
  740. // st1_.get( I ).~U();
  741. //}
  742. struct _destroy_L1
  743. {
  744. variant_base_impl * this_;
  745. template<class I> void operator()( I ) const noexcept
  746. {
  747. using U = mp11::mp_at<mp11::mp_list<none, T...>, I>;
  748. this_->st_.get( I() ).~U();
  749. }
  750. };
  751. void _destroy() noexcept
  752. {
  753. if( ix_ > 0 )
  754. {
  755. mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } );
  756. }
  757. }
  758. ~variant_base_impl() noexcept
  759. {
  760. _destroy();
  761. }
  762. constexpr std::size_t index() const noexcept
  763. {
  764. return ix_ - 1;
  765. }
  766. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  767. {
  768. size_t const J = I+1;
  769. BOOST_ASSERT( ix_ == J );
  770. return st_.get( mp11::mp_size_t<J>() );
  771. }
  772. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  773. {
  774. // size_t const J = I+1;
  775. BOOST_VARIANT2_CX14_ASSERT( ix_ == I+1 )
  776. return st_.get( mp11::mp_size_t<I+1>() );
  777. }
  778. template<std::size_t I, class... A> void emplace( A&&... a )
  779. {
  780. size_t const J = I+1;
  781. using U = mp11::mp_at_c<variant<T...>, I>;
  782. static_assert( std::is_nothrow_move_constructible<U>::value, "Logic error: U must be nothrow move constructible" );
  783. U tmp( std::forward<A>(a)... );
  784. _destroy();
  785. st_.emplace( mp11::mp_size_t<J>(), std::move(tmp) );
  786. ix_ = J;
  787. }
  788. static constexpr bool uses_double_storage() noexcept
  789. {
  790. return false;
  791. }
  792. };
  793. // not trivially destructible, double buffered
  794. template<class... T> struct variant_base_impl<false, false, T...>
  795. {
  796. #if defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__) && !defined(__INTEL_COMPILER)
  797. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63707 :-(
  798. variant_storage<none, T...> st1_, st2_;
  799. unsigned ix_;
  800. constexpr variant_base_impl(): st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() ), ix_( 0 )
  801. {
  802. }
  803. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), st2_( mp11::mp_size_t<0>() ), ix_( ( I::value + 1 ) * 2 )
  804. {
  805. }
  806. BOOST_CXX14_CONSTEXPR variant_storage<none, T...>& storage( unsigned i2 ) noexcept
  807. {
  808. return i2 == 0? st1_: st2_;
  809. }
  810. constexpr variant_storage<none, T...> const& storage( unsigned i2 ) const noexcept
  811. {
  812. return i2 == 0? st1_: st2_;
  813. }
  814. #else
  815. variant_storage<none, T...> st_[ 2 ];
  816. unsigned ix_;
  817. constexpr variant_base_impl(): st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } }, ix_( 0 )
  818. {
  819. }
  820. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): st_{ { mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... }, { mp11::mp_size_t<0>() } }, ix_( ( I::value + 1 ) * 2 )
  821. {
  822. }
  823. BOOST_CXX14_CONSTEXPR variant_storage<none, T...>& storage( unsigned i2 ) noexcept
  824. {
  825. return st_[ i2 ];
  826. }
  827. constexpr variant_storage<none, T...> const& storage( unsigned i2 ) const noexcept
  828. {
  829. return st_[ i2 ];
  830. }
  831. #endif
  832. // requires: ix_ == 0
  833. template<class I, class... A> void _replace( I, A&&... a )
  834. {
  835. ::new( &storage( 0 ) ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  836. ix_ = ( I::value + 1 ) * 2;
  837. }
  838. //[&]( auto I ){
  839. // using U = mp_at_c<mp_list<none, T...>, I>;
  840. // st1_.get( I ).~U();
  841. //}
  842. struct _destroy_L1
  843. {
  844. variant_base_impl * this_;
  845. unsigned i2_;
  846. template<class I> void operator()( I ) const noexcept
  847. {
  848. using U = mp11::mp_at<mp11::mp_list<none, T...>, I>;
  849. this_->storage( i2_ ).get( I() ).~U();
  850. }
  851. };
  852. void _destroy() noexcept
  853. {
  854. mp11::mp_with_index<1 + sizeof...(T)>( ix_ / 2, _destroy_L1{ this, ix_ & 1 } );
  855. }
  856. ~variant_base_impl() noexcept
  857. {
  858. _destroy();
  859. }
  860. constexpr std::size_t index() const noexcept
  861. {
  862. return ix_ / 2 - 1;
  863. }
  864. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  865. {
  866. BOOST_ASSERT( index() == I );
  867. size_t const J = I+1;
  868. constexpr mp11::mp_size_t<J> j{};
  869. return storage( ix_ & 1 ).get( j );
  870. }
  871. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  872. {
  873. BOOST_VARIANT2_CX14_ASSERT( index() == I )
  874. // size_t const J = I+1;
  875. // constexpr mp_size_t<J> j{};
  876. return storage( ix_ & 1 ).get( mp11::mp_size_t<I+1>() );
  877. }
  878. template<std::size_t I, class... A> void emplace( A&&... a )
  879. {
  880. size_t const J = I+1;
  881. unsigned i2 = 1 - ( ix_ & 1 );
  882. storage( i2 ).emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  883. _destroy();
  884. ix_ = J * 2 + i2;
  885. }
  886. static constexpr bool uses_double_storage() noexcept
  887. {
  888. return true;
  889. }
  890. };
  891. } // namespace detail
  892. // in_place_type_t
  893. template<class T> struct in_place_type_t
  894. {
  895. };
  896. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  897. template<class T> constexpr in_place_type_t<T> in_place_type{};
  898. #endif
  899. namespace detail
  900. {
  901. template<class T> struct is_in_place_type: std::false_type {};
  902. template<class T> struct is_in_place_type<in_place_type_t<T>>: std::true_type {};
  903. } // namespace detail
  904. // in_place_index_t
  905. template<std::size_t I> struct in_place_index_t
  906. {
  907. };
  908. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  909. template<std::size_t I> constexpr in_place_index_t<I> in_place_index{};
  910. #endif
  911. namespace detail
  912. {
  913. template<class T> struct is_in_place_index: std::false_type {};
  914. template<std::size_t I> struct is_in_place_index<in_place_index_t<I>>: std::true_type {};
  915. } // namespace detail
  916. // is_nothrow_swappable
  917. namespace detail
  918. {
  919. namespace det2
  920. {
  921. using std::swap;
  922. template<class T> using is_swappable_impl = decltype(swap(std::declval<T&>(), std::declval<T&>()));
  923. #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  924. template<class T> struct is_nothrow_swappable_impl_
  925. {
  926. static constexpr bool value = noexcept(swap(std::declval<T&>(), std::declval<T&>()));
  927. };
  928. template<class T> using is_nothrow_swappable_impl = mp11::mp_bool< is_nothrow_swappable_impl_<T>::value >;
  929. #else
  930. template<class T> using is_nothrow_swappable_impl = typename std::enable_if<noexcept(swap(std::declval<T&>(), std::declval<T&>()))>::type;
  931. #endif
  932. } // namespace det2
  933. template<class T> struct is_swappable: mp11::mp_valid<det2::is_swappable_impl, T>
  934. {
  935. };
  936. #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  937. template<class T> struct is_nothrow_swappable: mp11::mp_eval_if<mp11::mp_not<is_swappable<T>>, mp11::mp_false, det2::is_nothrow_swappable_impl, T>
  938. {
  939. };
  940. #else
  941. template<class T> struct is_nothrow_swappable: mp11::mp_valid<det2::is_nothrow_swappable_impl, T>
  942. {
  943. };
  944. #endif
  945. // variant_cc_base
  946. template<bool CopyConstructible, bool TriviallyCopyConstructible, class... T> struct variant_cc_base_impl;
  947. template<class... T> using variant_cc_base = variant_cc_base_impl<
  948. mp11::mp_all<std::is_copy_constructible<T>...>::value,
  949. mp11::mp_all<detail::is_trivially_copy_constructible<T>...>::value,
  950. T...>;
  951. template<class... T> struct variant_cc_base_impl<true, true, T...>: public variant_base<T...>
  952. {
  953. using variant_base = detail::variant_base<T...>;
  954. using variant_base::variant_base;
  955. variant_cc_base_impl() = default;
  956. variant_cc_base_impl( variant_cc_base_impl const& ) = default;
  957. variant_cc_base_impl( variant_cc_base_impl && ) = default;
  958. variant_cc_base_impl& operator=( variant_cc_base_impl const& ) = default;
  959. variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default;
  960. };
  961. template<bool B, class... T> struct variant_cc_base_impl<false, B, T...>: public variant_base<T...>
  962. {
  963. using variant_base = detail::variant_base<T...>;
  964. using variant_base::variant_base;
  965. variant_cc_base_impl() = default;
  966. variant_cc_base_impl( variant_cc_base_impl const& ) = delete;
  967. variant_cc_base_impl( variant_cc_base_impl && ) = default;
  968. variant_cc_base_impl& operator=( variant_cc_base_impl const& ) = default;
  969. variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default;
  970. };
  971. template<class... T> struct variant_cc_base_impl<true, false, T...>: public variant_base<T...>
  972. {
  973. using variant_base = detail::variant_base<T...>;
  974. using variant_base::variant_base;
  975. public:
  976. // constructors
  977. variant_cc_base_impl() = default;
  978. // copy constructor
  979. private:
  980. struct L1
  981. {
  982. variant_base * this_;
  983. variant_base const & r;
  984. template<class I> void operator()( I i ) const
  985. {
  986. this_->_replace( i, r._get_impl( i ) );
  987. }
  988. };
  989. public:
  990. variant_cc_base_impl( variant_cc_base_impl const& r )
  991. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<T>...>::value )
  992. : variant_base()
  993. {
  994. mp11::mp_with_index<sizeof...(T)>( r.index(), L1{ this, r } );
  995. }
  996. // move constructor
  997. variant_cc_base_impl( variant_cc_base_impl && ) = default;
  998. // assignment
  999. variant_cc_base_impl& operator=( variant_cc_base_impl const & ) = default;
  1000. variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default;
  1001. };
  1002. // variant_ca_base
  1003. template<bool CopyAssignable, bool TriviallyCopyAssignable, class... T> struct variant_ca_base_impl;
  1004. template<class... T> using variant_ca_base = variant_ca_base_impl<
  1005. mp11::mp_all<std::is_copy_constructible<T>..., std::is_copy_assignable<T>...>::value,
  1006. mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_copy_constructible<T>..., detail::is_trivially_copy_assignable<T>...>::value,
  1007. T...>;
  1008. template<class... T> struct variant_ca_base_impl<true, true, T...>: public variant_cc_base<T...>
  1009. {
  1010. using variant_base = detail::variant_cc_base<T...>;
  1011. using variant_base::variant_base;
  1012. variant_ca_base_impl() = default;
  1013. variant_ca_base_impl( variant_ca_base_impl const& ) = default;
  1014. variant_ca_base_impl( variant_ca_base_impl && ) = default;
  1015. variant_ca_base_impl& operator=( variant_ca_base_impl const& ) = default;
  1016. variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default;
  1017. };
  1018. template<bool B, class... T> struct variant_ca_base_impl<false, B, T...>: public variant_cc_base<T...>
  1019. {
  1020. using variant_base = detail::variant_cc_base<T...>;
  1021. using variant_base::variant_base;
  1022. variant_ca_base_impl() = default;
  1023. variant_ca_base_impl( variant_ca_base_impl const& ) = default;
  1024. variant_ca_base_impl( variant_ca_base_impl && ) = default;
  1025. variant_ca_base_impl& operator=( variant_ca_base_impl const& ) = delete;
  1026. variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default;
  1027. };
  1028. template<class... T> struct variant_ca_base_impl<true, false, T...>: public variant_cc_base<T...>
  1029. {
  1030. using variant_base = detail::variant_cc_base<T...>;
  1031. using variant_base::variant_base;
  1032. public:
  1033. // constructors
  1034. variant_ca_base_impl() = default;
  1035. variant_ca_base_impl( variant_ca_base_impl const& ) = default;
  1036. variant_ca_base_impl( variant_ca_base_impl && ) = default;
  1037. // copy assignment
  1038. private:
  1039. struct L3
  1040. {
  1041. variant_base * this_;
  1042. variant_base const & r;
  1043. template<class I> void operator()( I i ) const
  1044. {
  1045. this_->template emplace<I::value>( r._get_impl( i ) );
  1046. }
  1047. };
  1048. public:
  1049. BOOST_CXX14_CONSTEXPR variant_ca_base_impl& operator=( variant_ca_base_impl const & r )
  1050. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<T>...>::value )
  1051. {
  1052. mp11::mp_with_index<sizeof...(T)>( r.index(), L3{ this, r } );
  1053. return *this;
  1054. }
  1055. // move assignment
  1056. variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default;
  1057. };
  1058. // variant_mc_base
  1059. template<bool MoveConstructible, bool TriviallyMoveConstructible, class... T> struct variant_mc_base_impl;
  1060. template<class... T> using variant_mc_base = variant_mc_base_impl<
  1061. mp11::mp_all<std::is_move_constructible<T>...>::value,
  1062. mp11::mp_all<detail::is_trivially_move_constructible<T>...>::value,
  1063. T...>;
  1064. template<class... T> struct variant_mc_base_impl<true, true, T...>: public variant_ca_base<T...>
  1065. {
  1066. using variant_base = detail::variant_ca_base<T...>;
  1067. using variant_base::variant_base;
  1068. variant_mc_base_impl() = default;
  1069. variant_mc_base_impl( variant_mc_base_impl const& ) = default;
  1070. variant_mc_base_impl( variant_mc_base_impl && ) = default;
  1071. variant_mc_base_impl& operator=( variant_mc_base_impl const& ) = default;
  1072. variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default;
  1073. };
  1074. template<bool B, class... T> struct variant_mc_base_impl<false, B, T...>: public variant_ca_base<T...>
  1075. {
  1076. using variant_base = detail::variant_ca_base<T...>;
  1077. using variant_base::variant_base;
  1078. variant_mc_base_impl() = default;
  1079. variant_mc_base_impl( variant_mc_base_impl const& ) = default;
  1080. variant_mc_base_impl( variant_mc_base_impl && ) = delete;
  1081. variant_mc_base_impl& operator=( variant_mc_base_impl const& ) = default;
  1082. variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default;
  1083. };
  1084. template<class... T> struct variant_mc_base_impl<true, false, T...>: public variant_ca_base<T...>
  1085. {
  1086. using variant_base = detail::variant_ca_base<T...>;
  1087. using variant_base::variant_base;
  1088. public:
  1089. // constructors
  1090. variant_mc_base_impl() = default;
  1091. variant_mc_base_impl( variant_mc_base_impl const& ) = default;
  1092. // move constructor
  1093. private:
  1094. struct L2
  1095. {
  1096. variant_base * this_;
  1097. variant_base & r;
  1098. template<class I> void operator()( I i ) const
  1099. {
  1100. this_->_replace( i, std::move( r._get_impl( i ) ) );
  1101. }
  1102. };
  1103. public:
  1104. variant_mc_base_impl( variant_mc_base_impl && r )
  1105. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value )
  1106. {
  1107. mp11::mp_with_index<sizeof...(T)>( r.index(), L2{ this, r } );
  1108. }
  1109. // assignment
  1110. variant_mc_base_impl& operator=( variant_mc_base_impl const & ) = default;
  1111. variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default;
  1112. };
  1113. // variant_ma_base
  1114. template<bool MoveAssignable, bool TriviallyMoveAssignable, class... T> struct variant_ma_base_impl;
  1115. template<class... T> using variant_ma_base = variant_ma_base_impl<
  1116. mp11::mp_all<std::is_move_constructible<T>..., std::is_move_assignable<T>...>::value,
  1117. mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_move_constructible<T>..., detail::is_trivially_move_assignable<T>...>::value,
  1118. T...>;
  1119. template<class... T> struct variant_ma_base_impl<true, true, T...>: public variant_mc_base<T...>
  1120. {
  1121. using variant_base = detail::variant_mc_base<T...>;
  1122. using variant_base::variant_base;
  1123. variant_ma_base_impl() = default;
  1124. variant_ma_base_impl( variant_ma_base_impl const& ) = default;
  1125. variant_ma_base_impl( variant_ma_base_impl && ) = default;
  1126. variant_ma_base_impl& operator=( variant_ma_base_impl const& ) = default;
  1127. variant_ma_base_impl& operator=( variant_ma_base_impl && ) = default;
  1128. };
  1129. template<bool B, class... T> struct variant_ma_base_impl<false, B, T...>: public variant_mc_base<T...>
  1130. {
  1131. using variant_base = detail::variant_mc_base<T...>;
  1132. using variant_base::variant_base;
  1133. variant_ma_base_impl() = default;
  1134. variant_ma_base_impl( variant_ma_base_impl const& ) = default;
  1135. variant_ma_base_impl( variant_ma_base_impl && ) = default;
  1136. variant_ma_base_impl& operator=( variant_ma_base_impl const& ) = default;
  1137. variant_ma_base_impl& operator=( variant_ma_base_impl && ) = delete;
  1138. };
  1139. template<class... T> struct variant_ma_base_impl<true, false, T...>: public variant_mc_base<T...>
  1140. {
  1141. using variant_base = detail::variant_mc_base<T...>;
  1142. using variant_base::variant_base;
  1143. public:
  1144. // constructors
  1145. variant_ma_base_impl() = default;
  1146. variant_ma_base_impl( variant_ma_base_impl const& ) = default;
  1147. variant_ma_base_impl( variant_ma_base_impl && ) = default;
  1148. // copy assignment
  1149. variant_ma_base_impl& operator=( variant_ma_base_impl const & ) = default;
  1150. // move assignment
  1151. private:
  1152. struct L4
  1153. {
  1154. variant_base * this_;
  1155. variant_base & r;
  1156. template<class I> void operator()( I i ) const
  1157. {
  1158. this_->template emplace<I::value>( std::move( r._get_impl( i ) ) );
  1159. }
  1160. };
  1161. public:
  1162. variant_ma_base_impl& operator=( variant_ma_base_impl && r )
  1163. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value )
  1164. {
  1165. mp11::mp_with_index<sizeof...(T)>( r.index(), L4{ this, r } );
  1166. return *this;
  1167. }
  1168. };
  1169. } // namespace detail
  1170. // variant
  1171. template<class... T> class variant: private detail::variant_ma_base<T...>
  1172. {
  1173. private:
  1174. using variant_base = detail::variant_ma_base<T...>;
  1175. public:
  1176. // constructors
  1177. template<class E1 = void, class E2 = mp11::mp_if<std::is_default_constructible< mp11::mp_first<variant<T...>> >, E1>>
  1178. constexpr variant()
  1179. noexcept( std::is_nothrow_default_constructible< mp11::mp_first<variant<T...>> >::value )
  1180. : variant_base( mp11::mp_size_t<0>() )
  1181. {
  1182. }
  1183. // variant( variant const& ) = default;
  1184. // variant( variant && ) = default;
  1185. template<class U,
  1186. class Ud = typename std::decay<U>::type,
  1187. class E1 = typename std::enable_if< !std::is_same<Ud, variant>::value && !std::is_base_of<variant, Ud>::value && !detail::is_in_place_index<Ud>::value && !detail::is_in_place_type<Ud>::value >::type,
  1188. #if BOOST_WORKAROUND(BOOST_MSVC, < 1940)
  1189. class V = mp11::mp_apply_q< mp11::mp_bind_front<detail::resolve_overload_type, U&&>, variant >,
  1190. #else
  1191. class V = detail::resolve_overload_type<U&&, T...>,
  1192. #endif
  1193. class E2 = typename std::enable_if<std::is_constructible<V, U&&>::value>::type
  1194. >
  1195. constexpr variant( U&& u )
  1196. noexcept( std::is_nothrow_constructible<V, U&&>::value )
  1197. : variant_base( detail::resolve_overload_index<U&&, T...>(), std::forward<U>(u) )
  1198. {
  1199. }
  1200. template<class U, class... A, class I = mp11::mp_find<variant<T...>, U>, class E = typename std::enable_if<std::is_constructible<U, A&&...>::value>::type>
  1201. constexpr explicit variant( in_place_type_t<U>, A&&... a ): variant_base( I(), std::forward<A>(a)... )
  1202. {
  1203. }
  1204. template<class U, class V, class... A, class I = mp11::mp_find<variant<T...>, U>, class E = typename std::enable_if<std::is_constructible<U, std::initializer_list<V>&, A&&...>::value>::type>
  1205. constexpr explicit variant( in_place_type_t<U>, std::initializer_list<V> il, A&&... a ): variant_base( I(), il, std::forward<A>(a)... )
  1206. {
  1207. }
  1208. template<std::size_t I, class... A, class E = typename std::enable_if<std::is_constructible<mp11::mp_at_c<variant<T...>, I>, A&&...>::value>::type>
  1209. constexpr explicit variant( in_place_index_t<I>, A&&... a ): variant_base( mp11::mp_size_t<I>(), std::forward<A>(a)... )
  1210. {
  1211. }
  1212. template<std::size_t I, class V, class... A, class E = typename std::enable_if<std::is_constructible<mp11::mp_at_c<variant<T...>, I>, std::initializer_list<V>&, A&&...>::value>::type>
  1213. constexpr explicit variant( in_place_index_t<I>, std::initializer_list<V> il, A&&... a ): variant_base( mp11::mp_size_t<I>(), il, std::forward<A>(a)... )
  1214. {
  1215. }
  1216. // assignment
  1217. // variant& operator=( variant const& ) = default;
  1218. // variant& operator=( variant && ) = default;
  1219. template<class U,
  1220. class E1 = typename std::enable_if<!std::is_same<typename std::decay<U>::type, variant>::value>::type,
  1221. class V = detail::resolve_overload_type<U, T...>,
  1222. class E2 = typename std::enable_if<std::is_assignable<V&, U&&>::value && std::is_constructible<V, U&&>::value>::type
  1223. >
  1224. BOOST_CXX14_CONSTEXPR variant& operator=( U&& u )
  1225. noexcept( std::is_nothrow_constructible<V, U&&>::value )
  1226. {
  1227. std::size_t const I = detail::resolve_overload_index<U, T...>::value;
  1228. this->template emplace<I>( std::forward<U>(u) );
  1229. return *this;
  1230. }
  1231. // modifiers
  1232. template<class U, class... A,
  1233. class E = typename std::enable_if< mp11::mp_count<variant<T...>, U>::value == 1 && std::is_constructible<U, A&&...>::value >::type>
  1234. BOOST_CXX14_CONSTEXPR U& emplace( A&&... a )
  1235. {
  1236. using I = mp11::mp_find<variant<T...>, U>;
  1237. variant_base::template emplace<I::value>( std::forward<A>(a)... );
  1238. return _get_impl( I() );
  1239. }
  1240. template<class U, class V, class... A,
  1241. class E = typename std::enable_if< mp11::mp_count<variant<T...>, U>::value == 1 && std::is_constructible<U, std::initializer_list<V>&, A&&...>::value >::type>
  1242. BOOST_CXX14_CONSTEXPR U& emplace( std::initializer_list<V> il, A&&... a )
  1243. {
  1244. using I = mp11::mp_find<variant<T...>, U>;
  1245. variant_base::template emplace<I::value>( il, std::forward<A>(a)... );
  1246. return _get_impl( I() );
  1247. }
  1248. template<std::size_t I, class... A, class E = typename std::enable_if<std::is_constructible<mp11::mp_at_c<variant<T...>, I>, A&&...>::value>::type>
  1249. BOOST_CXX14_CONSTEXPR variant_alternative_t<I, variant<T...>>& emplace( A&&... a )
  1250. {
  1251. variant_base::template emplace<I>( std::forward<A>(a)... );
  1252. return _get_impl( mp11::mp_size_t<I>() );
  1253. }
  1254. template<std::size_t I, class V, class... A, class E = typename std::enable_if<std::is_constructible<mp11::mp_at_c<variant<T...>, I>, std::initializer_list<V>&, A&&...>::value>::type>
  1255. BOOST_CXX14_CONSTEXPR variant_alternative_t<I, variant<T...>>& emplace( std::initializer_list<V> il, A&&... a )
  1256. {
  1257. variant_base::template emplace<I>( il, std::forward<A>(a)... );
  1258. return _get_impl( mp11::mp_size_t<I>() );
  1259. }
  1260. // value status
  1261. constexpr bool valueless_by_exception() const noexcept
  1262. {
  1263. return false;
  1264. }
  1265. using variant_base::index;
  1266. using variant_base::uses_double_storage;
  1267. // swap
  1268. private:
  1269. struct L5
  1270. {
  1271. variant * this_;
  1272. variant & r;
  1273. template<class I> void operator()( I i ) const
  1274. {
  1275. using std::swap;
  1276. swap( this_->_get_impl( i ), r._get_impl( i ) );
  1277. }
  1278. };
  1279. public:
  1280. void swap( variant& r ) noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>..., detail::is_nothrow_swappable<T>...>::value )
  1281. {
  1282. if( index() == r.index() )
  1283. {
  1284. mp11::mp_with_index<sizeof...(T)>( index(), L5{ this, r } );
  1285. }
  1286. else
  1287. {
  1288. variant tmp( std::move(*this) );
  1289. *this = std::move( r );
  1290. r = std::move( tmp );
  1291. }
  1292. }
  1293. // private accessors
  1294. using variant_base::_get_impl;
  1295. // converting constructors (extension)
  1296. private:
  1297. template<class... U> struct L6
  1298. {
  1299. variant_base * this_;
  1300. variant<U...> const & r;
  1301. template<class I> void operator()( I i ) const
  1302. {
  1303. using J = mp11::mp_find<mp11::mp_list<T...>, mp11::mp_at<mp11::mp_list<U...>, I>>;
  1304. this_->_replace( J{}, r._get_impl( i ) );
  1305. }
  1306. };
  1307. public:
  1308. template<class... U,
  1309. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1310. variant( variant<U...> const& r )
  1311. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<U>...>::value )
  1312. {
  1313. mp11::mp_with_index<sizeof...(U)>( r.index(), L6<U...>{ this, r } );
  1314. }
  1315. private:
  1316. template<class... U> struct L7
  1317. {
  1318. variant_base * this_;
  1319. variant<U...> & r;
  1320. template<class I> void operator()( I i ) const
  1321. {
  1322. using J = mp11::mp_find<mp11::mp_list<T...>, mp11::mp_at<mp11::mp_list<U...>, I>>;
  1323. this_->_replace( J{}, std::move( r._get_impl( i ) ) );
  1324. }
  1325. };
  1326. public:
  1327. template<class... U,
  1328. class E2 = mp11::mp_if<mp11::mp_all<std::is_move_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1329. variant( variant<U...> && r )
  1330. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<U>...>::value )
  1331. {
  1332. mp11::mp_with_index<sizeof...(U)>( r.index(), L7<U...>{ this, r } );
  1333. }
  1334. // subset (extension)
  1335. private:
  1336. template<class... U, class V, std::size_t J, class E = typename std::enable_if<J != sizeof...(U)>::type> static constexpr variant<U...> _subset_impl( mp11::mp_size_t<J>, V && v )
  1337. {
  1338. return variant<U...>( in_place_index_t<J>(), std::forward<V>(v) );
  1339. }
  1340. template<class... U, class V> static variant<U...> _subset_impl( mp11::mp_size_t<sizeof...(U)>, V && /*v*/ )
  1341. {
  1342. detail::throw_bad_variant_access();
  1343. }
  1344. private:
  1345. template<class... U> struct L8
  1346. {
  1347. variant * this_;
  1348. template<class I> variant<U...> operator()( I i ) const
  1349. {
  1350. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1351. return this_->_subset_impl<U...>( J{}, this_->_get_impl( i ) );
  1352. }
  1353. };
  1354. public:
  1355. template<class... U,
  1356. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1357. BOOST_CXX14_CONSTEXPR variant<U...> subset() &
  1358. {
  1359. return mp11::mp_with_index<sizeof...(T)>( index(), L8<U...>{ this } );
  1360. }
  1361. private:
  1362. template<class... U> struct L9
  1363. {
  1364. variant const * this_;
  1365. template<class I> variant<U...> operator()( I i ) const
  1366. {
  1367. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1368. return this_->_subset_impl<U...>( J{}, this_->_get_impl( i ) );
  1369. }
  1370. };
  1371. public:
  1372. template<class... U,
  1373. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1374. constexpr variant<U...> subset() const&
  1375. {
  1376. return mp11::mp_with_index<sizeof...(T)>( index(), L9<U...>{ this } );
  1377. }
  1378. private:
  1379. template<class... U> struct L10
  1380. {
  1381. variant * this_;
  1382. template<class I> variant<U...> operator()( I i ) const
  1383. {
  1384. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1385. return this_->_subset_impl<U...>( J{}, std::move( this_->_get_impl( i ) ) );
  1386. }
  1387. };
  1388. public:
  1389. template<class... U,
  1390. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1391. BOOST_CXX14_CONSTEXPR variant<U...> subset() &&
  1392. {
  1393. return mp11::mp_with_index<sizeof...(T)>( index(), L10<U...>{ this } );
  1394. }
  1395. #if !BOOST_WORKAROUND(BOOST_GCC, < 40900)
  1396. // g++ 4.8 doesn't handle const&& particularly well
  1397. private:
  1398. template<class... U> struct L11
  1399. {
  1400. variant const * this_;
  1401. template<class I> variant<U...> operator()( I i ) const
  1402. {
  1403. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1404. return this_->_subset_impl<U...>( J{}, std::move( this_->_get_impl( i ) ) );
  1405. }
  1406. };
  1407. public:
  1408. template<class... U,
  1409. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1410. constexpr variant<U...> subset() const&&
  1411. {
  1412. return mp11::mp_with_index<sizeof...(T)>( index(), L11<U...>{ this } );
  1413. }
  1414. #endif
  1415. };
  1416. // relational operators
  1417. namespace detail
  1418. {
  1419. template<class... T> struct eq_L
  1420. {
  1421. variant<T...> const & v;
  1422. variant<T...> const & w;
  1423. template<class I> constexpr bool operator()( I i ) const
  1424. {
  1425. return v._get_impl( i ) == w._get_impl( i );
  1426. }
  1427. };
  1428. } // namespace detail
  1429. template<class... T> constexpr bool operator==( variant<T...> const & v, variant<T...> const & w )
  1430. {
  1431. return v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::eq_L<T...>{ v, w } );
  1432. }
  1433. namespace detail
  1434. {
  1435. template<class... T> struct ne_L
  1436. {
  1437. variant<T...> const & v;
  1438. variant<T...> const & w;
  1439. template<class I> constexpr bool operator()( I i ) const
  1440. {
  1441. return v._get_impl( i ) != w._get_impl( i );
  1442. }
  1443. };
  1444. } // namespace detail
  1445. template<class... T> constexpr bool operator!=( variant<T...> const & v, variant<T...> const & w )
  1446. {
  1447. return v.index() != w.index() || mp11::mp_with_index<sizeof...(T)>( v.index(), detail::ne_L<T...>{ v, w } );
  1448. }
  1449. namespace detail
  1450. {
  1451. template<class... T> struct lt_L
  1452. {
  1453. variant<T...> const & v;
  1454. variant<T...> const & w;
  1455. template<class I> constexpr bool operator()( I i ) const
  1456. {
  1457. return v._get_impl( i ) < w._get_impl( i );
  1458. }
  1459. };
  1460. } // namespace detail
  1461. template<class... T> constexpr bool operator<( variant<T...> const & v, variant<T...> const & w )
  1462. {
  1463. return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::lt_L<T...>{ v, w } ) );
  1464. }
  1465. template<class... T> constexpr bool operator>( variant<T...> const & v, variant<T...> const & w )
  1466. {
  1467. return w < v;
  1468. }
  1469. namespace detail
  1470. {
  1471. template<class... T> struct le_L
  1472. {
  1473. variant<T...> const & v;
  1474. variant<T...> const & w;
  1475. template<class I> constexpr bool operator()( I i ) const
  1476. {
  1477. return v._get_impl( i ) <= w._get_impl( i );
  1478. }
  1479. };
  1480. } // namespace detail
  1481. template<class... T> constexpr bool operator<=( variant<T...> const & v, variant<T...> const & w )
  1482. {
  1483. return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::le_L<T...>{ v, w } ) );
  1484. }
  1485. template<class... T> constexpr bool operator>=( variant<T...> const & v, variant<T...> const & w )
  1486. {
  1487. return w <= v;
  1488. }
  1489. // visitation
  1490. namespace detail
  1491. {
  1492. template<class T> using remove_cv_ref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  1493. template<class... T> variant<T...> const & extract_variant_base_( variant<T...> const & );
  1494. #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )
  1495. template<class V> struct extract_vbase_impl
  1496. {
  1497. using type = decltype( extract_variant_base_( std::declval<V>() ) );
  1498. };
  1499. template<class V> using extract_variant_base = remove_cv_ref_t< typename extract_vbase_impl<V>::type >;
  1500. #else
  1501. template<class V> using extract_variant_base = remove_cv_ref_t< decltype( extract_variant_base_( std::declval<V>() ) ) >;
  1502. #endif
  1503. template<class V> using variant_base_size = variant_size< extract_variant_base<V> >;
  1504. template<class T, class U> struct copy_cv_ref
  1505. {
  1506. using type = T;
  1507. };
  1508. template<class T, class U> struct copy_cv_ref<T, U const>
  1509. {
  1510. using type = T const;
  1511. };
  1512. template<class T, class U> struct copy_cv_ref<T, U volatile>
  1513. {
  1514. using type = T volatile;
  1515. };
  1516. template<class T, class U> struct copy_cv_ref<T, U const volatile>
  1517. {
  1518. using type = T const volatile;
  1519. };
  1520. template<class T, class U> struct copy_cv_ref<T, U&>
  1521. {
  1522. using type = typename copy_cv_ref<T, U>::type&;
  1523. };
  1524. template<class T, class U> struct copy_cv_ref<T, U&&>
  1525. {
  1526. using type = typename copy_cv_ref<T, U>::type&&;
  1527. };
  1528. template<class T, class U> using copy_cv_ref_t = typename copy_cv_ref<T, U>::type;
  1529. template<class F> struct Qret
  1530. {
  1531. template<class... T> using fn = decltype( std::declval<F>()( std::declval<T>()... ) );
  1532. };
  1533. template<class L> using front_if_same = mp11::mp_if<mp11::mp_apply<mp11::mp_same, L>, mp11::mp_front<L>>;
  1534. template<class V> using apply_cv_ref = mp11::mp_product<copy_cv_ref_t, extract_variant_base<V>, mp11::mp_list<V>>;
  1535. struct deduced {};
  1536. #if !BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  1537. template<class R, class F, class... V> using Vret = mp11::mp_eval_if_not< std::is_same<R, deduced>, R, front_if_same, mp11::mp_product_q<Qret<F>, apply_cv_ref<V>...> >;
  1538. #else
  1539. template<class R, class F, class... V> struct Vret_impl
  1540. {
  1541. using type = mp11::mp_eval_if_not< std::is_same<R, deduced>, R, front_if_same, mp11::mp_product_q<Qret<F>, apply_cv_ref<V>...> >;
  1542. };
  1543. template<class R, class F, class... V> using Vret = typename Vret_impl<R, F, V...>::type;
  1544. #endif
  1545. } // namespace detail
  1546. template<class R = detail::deduced, class F> constexpr auto visit( F&& f ) -> detail::Vret<R, F>
  1547. {
  1548. return std::forward<F>(f)();
  1549. }
  1550. namespace detail
  1551. {
  1552. template<class R, class F, class V1> struct visit_L1
  1553. {
  1554. F&& f;
  1555. V1&& v1;
  1556. template<class I> auto operator()( I ) const -> Vret<R, F, V1>
  1557. {
  1558. return std::forward<F>(f)( unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1559. }
  1560. };
  1561. } // namespace detail
  1562. template<class R = detail::deduced, class F, class V1> constexpr auto visit( F&& f, V1&& v1 ) -> detail::Vret<R, F, V1>
  1563. {
  1564. return mp11::mp_with_index<detail::variant_base_size<V1>>( v1.index(), detail::visit_L1<R, F, V1>{ std::forward<F>(f), std::forward<V1>(v1) } );
  1565. }
  1566. #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) || BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  1567. namespace detail
  1568. {
  1569. template<class F, class A> struct bind_front_
  1570. {
  1571. F&& f;
  1572. A&& a;
  1573. template<class... T> auto operator()( T&&... t ) -> decltype( std::forward<F>(f)( std::forward<A>(a), std::forward<T>(t)... ) )
  1574. {
  1575. return std::forward<F>(f)( std::forward<A>(a), std::forward<T>(t)... );
  1576. }
  1577. };
  1578. template<class F, class A> bind_front_<F, A> bind_front( F&& f, A&& a )
  1579. {
  1580. return bind_front_<F, A>{ std::forward<F>(f), std::forward<A>(a) };
  1581. }
  1582. template<class R, class F, class V1, class V2> struct visit_L2
  1583. {
  1584. F&& f;
  1585. V1&& v1;
  1586. V2&& v2;
  1587. template<class I> auto operator()( I ) const -> Vret<R, F, V1, V2>
  1588. {
  1589. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1590. return visit<R>( f2, std::forward<V2>(v2) );
  1591. }
  1592. };
  1593. } // namespace detail
  1594. template<class R = detail::deduced, class F, class V1, class V2> constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> detail::Vret<R, F, V1, V2>
  1595. {
  1596. return mp11::mp_with_index<detail::variant_base_size<V1>>( v1.index(), detail::visit_L2<R, F, V1, V2>{ std::forward<F>(f), std::forward<V1>(v1), std::forward<V2>(v2) } );
  1597. }
  1598. namespace detail
  1599. {
  1600. template<class R, class F, class V1, class V2, class V3> struct visit_L3
  1601. {
  1602. F&& f;
  1603. V1&& v1;
  1604. V2&& v2;
  1605. V3&& v3;
  1606. template<class I> auto operator()( I ) const -> Vret<R, F, V1, V2, V3>
  1607. {
  1608. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1609. return visit<R>( f2, std::forward<V2>(v2), std::forward<V3>(v3) );
  1610. }
  1611. };
  1612. } // namespace detail
  1613. template<class R = detail::deduced, class F, class V1, class V2, class V3> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> detail::Vret<R, F, V1, V2, V3>
  1614. {
  1615. return mp11::mp_with_index<detail::variant_base_size<V1>>( v1.index(), detail::visit_L3<R, F, V1, V2, V3>{ std::forward<F>(f), std::forward<V1>(v1), std::forward<V2>(v2), std::forward<V3>(v3) } );
  1616. }
  1617. namespace detail
  1618. {
  1619. template<class R, class F, class V1, class V2, class V3, class V4> struct visit_L4
  1620. {
  1621. F&& f;
  1622. V1&& v1;
  1623. V2&& v2;
  1624. V3&& v3;
  1625. V4&& v4;
  1626. template<class I> auto operator()( I ) const -> Vret<R, F, V1, V2, V3, V4>
  1627. {
  1628. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1629. return visit<R>( f2, std::forward<V2>(v2), std::forward<V3>(v3), std::forward<V4>(v4) );
  1630. }
  1631. };
  1632. } // namespace detail
  1633. template<class R = detail::deduced, class F, class V1, class V2, class V3, class V4> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3, V4&& v4 ) -> detail::Vret<R, F, V1, V2, V3, V4>
  1634. {
  1635. return mp11::mp_with_index<detail::variant_base_size<V1>>( v1.index(), detail::visit_L4<R, F, V1, V2, V3, V4>{ std::forward<F>(f), std::forward<V1>(v1), std::forward<V2>(v2), std::forward<V3>(v3), std::forward<V4>(v4) } );
  1636. }
  1637. #else
  1638. template<class R = detail::deduced, class F, class V1, class V2, class... V> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V&&... v ) -> detail::Vret<R, F, V1, V2, V...>
  1639. {
  1640. return mp11::mp_with_index<detail::variant_base_size<V1>>( v1.index(), [&]( auto I ){
  1641. auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( unsafe_get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
  1642. return visit<R>( f2, std::forward<V2>(v2), std::forward<V>(v)... );
  1643. });
  1644. }
  1645. #endif
  1646. // specialized algorithms
  1647. template<class... T,
  1648. class E = typename std::enable_if<mp11::mp_all<std::is_move_constructible<T>..., detail::is_swappable<T>...>::value>::type>
  1649. void swap( variant<T...> & v, variant<T...> & w )
  1650. noexcept( noexcept(v.swap(w)) )
  1651. {
  1652. v.swap( w );
  1653. }
  1654. // visit_by_index
  1655. namespace detail
  1656. {
  1657. template<class R, class V, class... F> using Vret2 = mp11::mp_eval_if_not< std::is_same<R, deduced>, R, front_if_same, mp11::mp_transform<mp11::mp_invoke_q, mp11::mp_list<Qret<F>...>, apply_cv_ref<V>> >;
  1658. template<class R, class V, class... F> struct visit_by_index_L
  1659. {
  1660. V&& v;
  1661. std::tuple<F&&...> tp;
  1662. template<class I> constexpr detail::Vret2<R, V, F...> operator()( I ) const
  1663. {
  1664. return std::get<I::value>( std::move(tp) )( unsafe_get<I::value>( std::forward<V>(v) ) );
  1665. }
  1666. };
  1667. } // namespace detail
  1668. template<class R = detail::deduced, class V, class... F> constexpr auto visit_by_index( V&& v, F&&... f ) -> detail::Vret2<R, V, F...>
  1669. {
  1670. static_assert( variant_size<V>::value == sizeof...(F), "Incorrect number of function objects" );
  1671. return mp11::mp_with_index<variant_size<V>::value>( v.index(),
  1672. detail::visit_by_index_L<R, V, F...>{ std::forward<V>(v), std::tuple<F&&...>( std::forward<F>(f)... ) } );
  1673. }
  1674. // output streaming
  1675. namespace detail
  1676. {
  1677. template<class Ch, class Tr, class... T> struct ostream_insert_L
  1678. {
  1679. std::basic_ostream<Ch, Tr>& os;
  1680. variant<T...> const& v;
  1681. template<class I> std::basic_ostream<Ch, Tr>& operator()( I ) const
  1682. {
  1683. return os << unsafe_get<I::value>( v );
  1684. }
  1685. };
  1686. template<class Os, class T, class E = void> struct is_output_streamable: std::false_type
  1687. {
  1688. };
  1689. template<class Os, class T> struct is_output_streamable<Os, T, decltype( std::declval<Os&>() << std::declval<T const&>(), (void)0 )>: std::true_type
  1690. {
  1691. };
  1692. } // namespace detail
  1693. template<class Ch, class Tr>
  1694. std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, monostate const& )
  1695. {
  1696. os << "monostate";
  1697. return os;
  1698. }
  1699. template<class Ch, class Tr, class T1, class... T,
  1700. class E = typename std::enable_if< mp11::mp_all< detail::is_output_streamable<std::basic_ostream<Ch, Tr>, T>... >::value >::type >
  1701. std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& os, variant<T1, T...> const& v )
  1702. {
  1703. return mp11::mp_with_index<1 + sizeof...(T)>( v.index(),
  1704. detail::ostream_insert_L<Ch, Tr, T1, T...>{ os, v } );
  1705. }
  1706. // hashing support
  1707. namespace detail
  1708. {
  1709. inline std::size_t hash_value_impl_( mp11::mp_true, std::size_t index, std::size_t value )
  1710. {
  1711. unsigned long long hv = 0xCBF29CE484222325ull;
  1712. unsigned long long const prime = 0x100000001B3ull;
  1713. hv ^= index;
  1714. hv *= prime;
  1715. hv ^= value;
  1716. hv *= prime;
  1717. return static_cast<std::size_t>( hv );
  1718. }
  1719. inline std::size_t hash_value_impl_( mp11::mp_false, std::size_t index, std::size_t value )
  1720. {
  1721. std::size_t hv = 0x811C9DC5;
  1722. std::size_t const prime = 0x01000193;
  1723. hv ^= index;
  1724. hv *= prime;
  1725. hv ^= value;
  1726. hv *= prime;
  1727. return hv;
  1728. }
  1729. inline std::size_t hash_value_impl( std::size_t index, std::size_t value )
  1730. {
  1731. return hash_value_impl_( mp11::mp_bool< (SIZE_MAX > UINT32_MAX) >(), index, value );
  1732. }
  1733. template<template<class> class H, class V> struct hash_value_L
  1734. {
  1735. V const & v;
  1736. template<class I> std::size_t operator()( I ) const
  1737. {
  1738. auto const & t = unsafe_get<I::value>( v );
  1739. std::size_t index = I::value;
  1740. std::size_t value = H<remove_cv_ref_t<decltype(t)>>()( t );
  1741. return hash_value_impl( index, value );
  1742. }
  1743. };
  1744. template<class... T> std::size_t hash_value_std( variant<T...> const & v )
  1745. {
  1746. return mp11::mp_with_index<sizeof...(T)>( v.index(), detail::hash_value_L< std::hash, variant<T...> >{ v } );
  1747. }
  1748. } // namespace detail
  1749. inline std::size_t hash_value( monostate const & )
  1750. {
  1751. return 0xA7EE4757u;
  1752. }
  1753. template<class... T> std::size_t hash_value( variant<T...> const & v )
  1754. {
  1755. return mp11::mp_with_index<sizeof...(T)>( v.index(), detail::hash_value_L< boost::hash, variant<T...> >{ v } );
  1756. }
  1757. namespace detail
  1758. {
  1759. template<class T> using is_hash_enabled = std::is_default_constructible< std::hash<typename std::remove_const<T>::type> >;
  1760. template<class V, bool E = mp11::mp_all_of<V, is_hash_enabled>::value> struct std_hash_impl;
  1761. template<class V> struct std_hash_impl<V, false>
  1762. {
  1763. std_hash_impl() = delete;
  1764. std_hash_impl( std_hash_impl const& ) = delete;
  1765. std_hash_impl& operator=( std_hash_impl const& ) = delete;
  1766. };
  1767. template<class V> struct std_hash_impl<V, true>
  1768. {
  1769. std::size_t operator()( V const & v ) const
  1770. {
  1771. return detail::hash_value_std( v );
  1772. }
  1773. };
  1774. } // namespace detail
  1775. } // namespace variant2
  1776. } // namespace boost
  1777. namespace std
  1778. {
  1779. template<class... T> struct hash< ::boost::variant2::variant<T...> >: public ::boost::variant2::detail::std_hash_impl< ::boost::variant2::variant<T...> >
  1780. {
  1781. };
  1782. template<> struct hash< ::boost::variant2::monostate >
  1783. {
  1784. std::size_t operator()( ::boost::variant2::monostate const & v ) const
  1785. {
  1786. return hash_value( v );
  1787. }
  1788. };
  1789. } // namespace std
  1790. // JSON support
  1791. namespace boost
  1792. {
  1793. namespace json
  1794. {
  1795. class value;
  1796. struct value_from_tag;
  1797. template<class T>
  1798. void value_from( T&& t, value& jv );
  1799. template<class T>
  1800. struct try_value_to_tag;
  1801. template<class T1, class T2>
  1802. struct result_for;
  1803. template<class T>
  1804. typename result_for<T, value>::type
  1805. try_value_to( value const & jv );
  1806. template<class T>
  1807. typename result_for<T, value>::type
  1808. result_from_errno( int e, boost::source_location const* loc ) noexcept;
  1809. template<class T> struct is_null_like;
  1810. template<> struct is_null_like<variant2::monostate>: std::true_type {};
  1811. } // namespace json
  1812. namespace variant2
  1813. {
  1814. namespace detail
  1815. {
  1816. struct tag_invoke_L1
  1817. {
  1818. boost::json::value& v;
  1819. #if defined(BOOST_MSVC) && BOOST_MSVC / 10 == 191
  1820. // msvc-14.1 with /permissive- needs this
  1821. explicit tag_invoke_L1( boost::json::value& v_ ): v( v_ ) {}
  1822. #endif
  1823. template<class T> void operator()( T const& t ) const
  1824. {
  1825. boost::json::value_from( t, v );
  1826. }
  1827. };
  1828. } // namespace detail
  1829. template<class... T>
  1830. void tag_invoke( boost::json::value_from_tag const&, boost::json::value& v, variant<T...> const & w )
  1831. {
  1832. visit( detail::tag_invoke_L1{ v }, w );
  1833. }
  1834. namespace detail
  1835. {
  1836. template<class V> struct tag_invoke_L2
  1837. {
  1838. boost::json::value const& v;
  1839. typename boost::json::result_for<V, boost::json::value>::type& r;
  1840. template<class I> void operator()( I /*i*/ ) const
  1841. {
  1842. if( !r )
  1843. {
  1844. using Ti = mp11::mp_at_c<V, I::value>;
  1845. auto r2 = boost::json::try_value_to<Ti>( v );
  1846. if( r2 )
  1847. {
  1848. r.emplace( in_place_index_t<I::value>{}, std::move( *r2 ) );
  1849. }
  1850. }
  1851. }
  1852. };
  1853. } // namespace detail
  1854. template<class... T>
  1855. typename boost::json::result_for<variant<T...>, boost::json::value>::type
  1856. tag_invoke( boost::json::try_value_to_tag<variant<T...>> const&, boost::json::value const& v )
  1857. {
  1858. static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION;
  1859. auto r = boost::json::result_from_errno< variant<T...> >( EINVAL, &loc );
  1860. mp11::mp_for_each<mp11::mp_iota_c<sizeof...(T)>>( detail::tag_invoke_L2< variant<T...> >{ v, r } );
  1861. return r;
  1862. }
  1863. } // namespace variant2
  1864. } // namespace boost
  1865. #undef BOOST_VARIANT2_CX14_ASSERT
  1866. #if defined(_MSC_VER) && _MSC_VER < 1910
  1867. # pragma warning( pop )
  1868. #endif
  1869. #endif // #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED