variant.hpp 74 KB

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