variant.hpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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 <cstddef>
  19. #include <type_traits>
  20. #include <exception>
  21. #include <cassert>
  22. #include <initializer_list>
  23. #include <utility>
  24. //
  25. namespace boost
  26. {
  27. #ifdef BOOST_NO_EXCEPTIONS
  28. BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
  29. #endif
  30. namespace variant2
  31. {
  32. // bad_variant_access
  33. class bad_variant_access: public std::exception
  34. {
  35. public:
  36. bad_variant_access() noexcept
  37. {
  38. }
  39. char const * what() const noexcept
  40. {
  41. return "bad_variant_access";
  42. }
  43. };
  44. namespace detail
  45. {
  46. BOOST_NORETURN inline void throw_bad_variant_access()
  47. {
  48. #ifdef BOOST_NO_EXCEPTIONS
  49. boost::throw_exception( bad_variant_access() );
  50. #else
  51. throw bad_variant_access();
  52. #endif
  53. }
  54. } // namespace detail
  55. // monostate
  56. struct monostate
  57. {
  58. };
  59. constexpr bool operator<(monostate, monostate) noexcept { return false; }
  60. constexpr bool operator>(monostate, monostate) noexcept { return false; }
  61. constexpr bool operator<=(monostate, monostate) noexcept { return true; }
  62. constexpr bool operator>=(monostate, monostate) noexcept { return true; }
  63. constexpr bool operator==(monostate, monostate) noexcept { return true; }
  64. constexpr bool operator!=(monostate, monostate) noexcept { return false; }
  65. // variant forward declaration
  66. template<class... T> class variant;
  67. // variant_size
  68. template<class T> struct variant_size
  69. {
  70. };
  71. template<class T> struct variant_size<T const>: variant_size<T>
  72. {
  73. };
  74. template<class T> struct variant_size<T volatile>: variant_size<T>
  75. {
  76. };
  77. template<class T> struct variant_size<T const volatile>: variant_size<T>
  78. {
  79. };
  80. template<class T> struct variant_size<T&>: variant_size<T>
  81. {
  82. };
  83. template<class T> struct variant_size<T&&>: variant_size<T>
  84. {
  85. };
  86. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  87. template <class T> /*inline*/ constexpr std::size_t variant_size_v = variant_size<T>::value;
  88. #endif
  89. template <class... T> struct variant_size<variant<T...>>: mp11::mp_size<variant<T...>>
  90. {
  91. };
  92. // variant_alternative
  93. template<std::size_t I, class T> struct variant_alternative;
  94. template<std::size_t I, class T> using variant_alternative_t = typename variant_alternative<I, T>::type;
  95. #if BOOST_WORKAROUND(BOOST_GCC, < 40900)
  96. namespace detail
  97. {
  98. template<std::size_t I, class T, bool E> struct variant_alternative_impl
  99. {
  100. };
  101. template<std::size_t I, class... T> struct variant_alternative_impl<I, variant<T...>, true>
  102. {
  103. using type = mp11::mp_at_c<variant<T...>, I>;
  104. };
  105. 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> >
  106. {
  107. };
  108. 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> >
  109. {
  110. };
  111. 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> >
  112. {
  113. };
  114. 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> >
  115. {
  116. };
  117. 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 >
  118. {
  119. };
  120. 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 >
  121. {
  122. };
  123. 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 >
  124. {
  125. };
  126. 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> >
  127. {
  128. };
  129. 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 >
  130. {
  131. };
  132. 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 >
  133. {
  134. };
  135. 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 >
  136. {
  137. };
  138. } // namespace detail
  139. template<std::size_t I, class T> struct variant_alternative
  140. {
  141. };
  142. template<std::size_t I, class... T> struct variant_alternative<I, variant<T...>>: public detail::variant_alternative_impl<I, variant<T...>, (I < sizeof...(T))>
  143. {
  144. };
  145. 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))>
  146. {
  147. };
  148. 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))>
  149. {
  150. };
  151. 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))>
  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. #else
  179. namespace detail
  180. {
  181. #if defined( BOOST_MP11_VERSION ) && BOOST_MP11_VERSION >= 107000
  182. template<class I, class T, class Q> using var_alt_impl = mp11::mp_invoke_q<Q, variant_alternative_t<I::value, T>>;
  183. #else
  184. template<class I, class T, class Q> using var_alt_impl = mp11::mp_invoke<Q, variant_alternative_t<I::value, T>>;
  185. #endif
  186. } // namespace detail
  187. template<std::size_t I, class T> struct variant_alternative
  188. {
  189. };
  190. 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>>
  191. {
  192. };
  193. 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>>
  194. {
  195. };
  196. 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>>
  197. {
  198. };
  199. 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>>
  200. {
  201. };
  202. 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>>
  203. {
  204. };
  205. 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>>
  206. {
  207. };
  208. #endif
  209. // variant_npos
  210. constexpr std::size_t variant_npos = ~static_cast<std::size_t>( 0 );
  211. // holds_alternative
  212. template<class U, class... T> constexpr bool holds_alternative( variant<T...> const& v ) noexcept
  213. {
  214. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  215. return v.index() == mp11::mp_find<variant<T...>, U>::value;
  216. }
  217. // get (index)
  218. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
  219. {
  220. static_assert( I < sizeof...(T), "Index out of bounds" );
  221. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t<I>() );
  222. }
  223. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& get(variant<T...>&& v)
  224. {
  225. static_assert( I < sizeof...(T), "Index out of bounds" );
  226. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  227. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  228. #else
  229. if( v.index() != I ) detail::throw_bad_variant_access();
  230. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  231. #endif
  232. }
  233. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const& get(variant<T...> const& v)
  234. {
  235. static_assert( I < sizeof...(T), "Index out of bounds" );
  236. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t<I>() );
  237. }
  238. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const&& get(variant<T...> const&& v)
  239. {
  240. static_assert( I < sizeof...(T), "Index out of bounds" );
  241. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  242. return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  243. #else
  244. if( v.index() != I ) detail::throw_bad_variant_access();
  245. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  246. #endif
  247. }
  248. // detail::unsafe_get (for visit)
  249. namespace detail
  250. {
  251. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& unsafe_get(variant<T...>& v)
  252. {
  253. static_assert( I < sizeof...(T), "Index out of bounds" );
  254. return v._get_impl( mp11::mp_size_t<I>() );
  255. }
  256. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& unsafe_get(variant<T...>&& v)
  257. {
  258. static_assert( I < sizeof...(T), "Index out of bounds" );
  259. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  260. }
  261. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const& unsafe_get(variant<T...> const& v)
  262. {
  263. static_assert( I < sizeof...(T), "Index out of bounds" );
  264. return v._get_impl( mp11::mp_size_t<I>() );
  265. }
  266. template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>> const&& unsafe_get(variant<T...> const&& v)
  267. {
  268. static_assert( I < sizeof...(T), "Index out of bounds" );
  269. return std::move( v._get_impl( mp11::mp_size_t<I>() ) );
  270. }
  271. } // namespace detail
  272. // get (type)
  273. template<class U, class... T> constexpr U& get(variant<T...>& v)
  274. {
  275. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  276. using I = mp11::mp_find<variant<T...>, U>;
  277. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
  278. }
  279. template<class U, class... T> constexpr U&& get(variant<T...>&& v)
  280. {
  281. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  282. using I = mp11::mp_find<variant<T...>, U>;
  283. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  284. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
  285. #else
  286. if( v.index() != I::value ) detail::throw_bad_variant_access();
  287. return std::move( v._get_impl( I() ) );
  288. #endif
  289. }
  290. template<class U, class... T> constexpr U const& get(variant<T...> const& 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. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
  295. }
  296. template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
  297. {
  298. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  299. using I = mp11::mp_find<variant<T...>, U>;
  300. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
  301. return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
  302. #else
  303. if( v.index() != I::value ) detail::throw_bad_variant_access();
  304. return std::move( v._get_impl( I() ) );
  305. #endif
  306. }
  307. // get_if
  308. 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
  309. {
  310. static_assert( I < sizeof...(T), "Index out of bounds" );
  311. return v && v->index() == I? &v->_get_impl( mp11::mp_size_t<I>() ): 0;
  312. }
  313. 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
  314. {
  315. static_assert( I < sizeof...(T), "Index out of bounds" );
  316. return v && v->index() == I? &v->_get_impl( mp11::mp_size_t<I>() ): 0;
  317. }
  318. template<class U, class... T> constexpr typename std::add_pointer<U>::type get_if(variant<T...>* v) noexcept
  319. {
  320. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  321. using I = mp11::mp_find<variant<T...>, U>;
  322. return v && v->index() == I::value? &v->_get_impl( I() ): 0;
  323. }
  324. template<class U, class... T> constexpr typename std::add_pointer<U const>::type get_if(variant<T...> const * v) noexcept
  325. {
  326. static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
  327. using I = mp11::mp_find<variant<T...>, U>;
  328. return v && v->index() == I::value? &v->_get_impl( I() ): 0;
  329. }
  330. //
  331. namespace detail
  332. {
  333. // trivially_*
  334. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  335. template<class T> struct is_trivially_copy_constructible: mp11::mp_bool<std::is_copy_constructible<T>::value && std::has_trivial_copy_constructor<T>::value>
  336. {
  337. };
  338. template<class T> struct is_trivially_copy_assignable: mp11::mp_bool<std::is_copy_assignable<T>::value && std::has_trivial_copy_assign<T>::value>
  339. {
  340. };
  341. template<class T> struct is_trivially_move_constructible: mp11::mp_bool<std::is_move_constructible<T>::value && std::is_trivial<T>::value>
  342. {
  343. };
  344. template<class T> struct is_trivially_move_assignable: mp11::mp_bool<std::is_move_assignable<T>::value && std::is_trivial<T>::value>
  345. {
  346. };
  347. #else
  348. using std::is_trivially_copy_constructible;
  349. using std::is_trivially_copy_assignable;
  350. using std::is_trivially_move_constructible;
  351. using std::is_trivially_move_assignable;
  352. #endif
  353. // variant_storage
  354. template<class D, class... T> union variant_storage_impl;
  355. template<class... T> using variant_storage = variant_storage_impl<mp11::mp_all<std::is_trivially_destructible<T>...>, T...>;
  356. template<class D> union variant_storage_impl<D>
  357. {
  358. };
  359. // not all trivially destructible
  360. template<class T1, class... T> union variant_storage_impl<mp11::mp_false, T1, T...>
  361. {
  362. T1 first_;
  363. variant_storage<T...> rest_;
  364. template<class... A> constexpr explicit variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward<A>(a)... )
  365. {
  366. }
  367. template<std::size_t I, class... A> constexpr explicit variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-1>(), std::forward<A>(a)... )
  368. {
  369. }
  370. ~variant_storage_impl()
  371. {
  372. }
  373. template<class... A> void emplace( mp11::mp_size_t<0>, A&&... a )
  374. {
  375. ::new( &first_ ) T1( std::forward<A>(a)... );
  376. }
  377. template<std::size_t I, class... A> void emplace( mp11::mp_size_t<I>, A&&... a )
  378. {
  379. rest_.emplace( mp11::mp_size_t<I-1>(), std::forward<A>(a)... );
  380. }
  381. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; }
  382. constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; }
  383. 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>() ); }
  384. 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>() ); }
  385. };
  386. // all trivially destructible
  387. template<class T1, class... T> union variant_storage_impl<mp11::mp_true, T1, T...>
  388. {
  389. T1 first_;
  390. variant_storage<T...> rest_;
  391. template<class... A> constexpr explicit variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward<A>(a)... )
  392. {
  393. }
  394. template<std::size_t I, class... A> constexpr explicit variant_storage_impl( mp11::mp_size_t<I>, A&&... a ): rest_( mp11::mp_size_t<I-1>(), std::forward<A>(a)... )
  395. {
  396. }
  397. template<class... A> void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a )
  398. {
  399. ::new( &first_ ) T1( std::forward<A>(a)... );
  400. }
  401. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t<I>, A&&... a )
  402. {
  403. rest_.emplace( mp11::mp_size_t<I-1>(), std::forward<A>(a)... );
  404. }
  405. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t<I>, A&&... a )
  406. {
  407. *this = variant_storage_impl( mp11::mp_size_t<I>(), std::forward<A>(a)... );
  408. }
  409. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t<I>, A&&... a )
  410. {
  411. 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)... );
  412. }
  413. BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; }
  414. constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; }
  415. 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>() ); }
  416. 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>() ); }
  417. };
  418. // resolve_overload_*
  419. template<class... T> struct overload;
  420. template<> struct overload<>
  421. {
  422. void operator()() const;
  423. };
  424. template<class T1, class... T> struct overload<T1, T...>: overload<T...>
  425. {
  426. using overload<T...>::operator();
  427. mp11::mp_identity<T1> operator()(T1) const;
  428. };
  429. #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )
  430. template<class U, class... T> using resolve_overload_type_ = decltype( overload<T...>()(std::declval<U>()) );
  431. template<class U, class... T> struct resolve_overload_type_impl: mp11::mp_defer< resolve_overload_type_, U, T... >
  432. {
  433. };
  434. template<class U, class... T> using resolve_overload_type = typename resolve_overload_type_impl<U, T...>::type::type;
  435. #else
  436. template<class U, class... T> using resolve_overload_type = typename decltype( overload<T...>()(std::declval<U>()) )::type;
  437. #endif
  438. template<class U, class... T> using resolve_overload_index = mp11::mp_find<mp11::mp_list<T...>, resolve_overload_type<U, T...>>;
  439. // variant_base
  440. template<bool is_trivially_destructible, bool is_single_buffered, class... T> struct variant_base_impl;
  441. 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...>;
  442. struct none {};
  443. // trivially destructible, single buffered
  444. template<class... T> struct variant_base_impl<true, true, T...>
  445. {
  446. int ix_;
  447. variant_storage<none, T...> st1_;
  448. constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() )
  449. {
  450. }
  451. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... )
  452. {
  453. }
  454. // requires: ix_ == 0
  455. template<class I, class... A> void _replace( I, A&&... a )
  456. {
  457. ::new( &st1_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  458. ix_ = I::value + 1;
  459. }
  460. constexpr std::size_t index() const noexcept
  461. {
  462. return ix_ - 1;
  463. }
  464. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  465. {
  466. size_t const J = I+1;
  467. assert( ix_ == J );
  468. return st1_.get( mp11::mp_size_t<J>() );
  469. }
  470. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  471. {
  472. // size_t const J = I+1;
  473. // assert( ix_ == I+1 );
  474. return st1_.get( mp11::mp_size_t<I+1>() );
  475. }
  476. template<std::size_t J, class U, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, A&&... a )
  477. {
  478. static_assert( std::is_nothrow_constructible<U, A&&...>::value, "Logic error: U must be nothrow constructible from A&&..." );
  479. st1_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  480. ix_ = J;
  481. }
  482. template<std::size_t J, class U, class... A> BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, A&&... a )
  483. {
  484. static_assert( std::is_nothrow_move_constructible<U>::value, "Logic error: U must be nothrow move constructible" );
  485. U tmp( std::forward<A>(a)... );
  486. st1_.emplace( mp11::mp_size_t<J>(), std::move(tmp) );
  487. ix_ = J;
  488. }
  489. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( A&&... a )
  490. {
  491. std::size_t const J = I+1;
  492. using U = mp11::mp_at_c<variant<T...>, I>;
  493. this->emplace_impl<J, U>( std::is_nothrow_constructible<U, A&&...>(), std::forward<A>(a)... );
  494. }
  495. };
  496. // trivially destructible, double buffered
  497. template<class... T> struct variant_base_impl<true, false, T...>
  498. {
  499. int ix_;
  500. variant_storage<none, T...> st1_;
  501. variant_storage<none, T...> st2_;
  502. constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() )
  503. {
  504. }
  505. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), st2_( mp11::mp_size_t<0>() )
  506. {
  507. }
  508. // requires: ix_ == 0
  509. template<class I, class... A> void _replace( I, A&&... a )
  510. {
  511. ::new( &st1_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  512. ix_ = I::value + 1;
  513. }
  514. constexpr std::size_t index() const noexcept
  515. {
  516. return ix_ >= 0? ix_ - 1: -ix_ - 1;
  517. }
  518. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  519. {
  520. size_t const J = I+1;
  521. assert( ix_ == J || -ix_ == J );
  522. constexpr mp11::mp_size_t<J> j{};
  523. return ix_ >= 0? st1_.get( j ): st2_.get( j );
  524. }
  525. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  526. {
  527. // size_t const J = I+1;
  528. // assert( ix_ == J || -ix_ == J );
  529. // constexpr mp_size_t<J> j{};
  530. return ix_ >= 0? st1_.get( mp11::mp_size_t<I+1>() ): st2_.get( mp11::mp_size_t<I+1>() );
  531. }
  532. template<std::size_t I, class... A> BOOST_CXX14_CONSTEXPR void emplace( A&&... a )
  533. {
  534. size_t const J = I+1;
  535. if( ix_ >= 0 )
  536. {
  537. st2_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  538. ix_ = -static_cast<int>( J );
  539. }
  540. else
  541. {
  542. st1_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  543. ix_ = J;
  544. }
  545. }
  546. };
  547. // not trivially destructible, single buffered
  548. template<class... T> struct variant_base_impl<false, true, T...>
  549. {
  550. int ix_;
  551. variant_storage<none, T...> st1_;
  552. constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() )
  553. {
  554. }
  555. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... )
  556. {
  557. }
  558. // requires: ix_ == 0
  559. template<class I, class... A> void _replace( I, A&&... a )
  560. {
  561. ::new( &st1_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  562. ix_ = I::value + 1;
  563. }
  564. //[&]( auto I ){
  565. // using U = mp_at_c<mp_list<none, T...>, I>;
  566. // st1_.get( I ).~U();
  567. //}
  568. struct _destroy_L1
  569. {
  570. variant_base_impl * this_;
  571. template<class I> void operator()( I ) const noexcept
  572. {
  573. using U = mp11::mp_at<mp11::mp_list<none, T...>, I>;
  574. this_->st1_.get( I() ).~U();
  575. }
  576. };
  577. void _destroy() noexcept
  578. {
  579. if( ix_ > 0 )
  580. {
  581. mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } );
  582. }
  583. }
  584. ~variant_base_impl() noexcept
  585. {
  586. _destroy();
  587. }
  588. constexpr std::size_t index() const noexcept
  589. {
  590. return ix_ - 1;
  591. }
  592. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  593. {
  594. size_t const J = I+1;
  595. assert( ix_ == J );
  596. return st1_.get( mp11::mp_size_t<J>() );
  597. }
  598. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  599. {
  600. // size_t const J = I+1;
  601. // assert( ix_ == J );
  602. return st1_.get( mp11::mp_size_t<I+1>() );
  603. }
  604. template<std::size_t I, class... A> void emplace( A&&... a )
  605. {
  606. size_t const J = I+1;
  607. using U = mp11::mp_at_c<variant<T...>, I>;
  608. static_assert( std::is_nothrow_move_constructible<U>::value, "Logic error: U must be nothrow move constructible" );
  609. U tmp( std::forward<A>(a)... );
  610. _destroy();
  611. st1_.emplace( mp11::mp_size_t<J>(), std::move(tmp) );
  612. ix_ = J;
  613. }
  614. };
  615. // not trivially destructible, double buffered
  616. template<class... T> struct variant_base_impl<false, false, T...>
  617. {
  618. int ix_;
  619. variant_storage<none, T...> st1_;
  620. variant_storage<none, T...> st2_;
  621. constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() )
  622. {
  623. }
  624. template<class I, class... A> constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st1_( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... ), st2_( mp11::mp_size_t<0>() )
  625. {
  626. }
  627. // requires: ix_ == 0
  628. template<class I, class... A> void _replace( I, A&&... a )
  629. {
  630. ::new( &st1_ ) variant_storage<none, T...>( mp11::mp_size_t<I::value + 1>(), std::forward<A>(a)... );
  631. ix_ = I::value + 1;
  632. }
  633. //[&]( auto I ){
  634. // using U = mp_at_c<mp_list<none, T...>, I>;
  635. // st1_.get( I ).~U();
  636. //}
  637. struct _destroy_L1
  638. {
  639. variant_base_impl * this_;
  640. template<class I> void operator()( I ) const noexcept
  641. {
  642. using U = mp11::mp_at<mp11::mp_list<none, T...>, I>;
  643. this_->st1_.get( I() ).~U();
  644. }
  645. };
  646. struct _destroy_L2
  647. {
  648. variant_base_impl * this_;
  649. template<class I> void operator()( I ) const noexcept
  650. {
  651. using U = mp11::mp_at<mp11::mp_list<none, T...>, I>;
  652. this_->st2_.get( I() ).~U();
  653. }
  654. };
  655. void _destroy() noexcept
  656. {
  657. if( ix_ > 0 )
  658. {
  659. mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } );
  660. }
  661. else if( ix_ < 0 )
  662. {
  663. mp11::mp_with_index<1 + sizeof...(T)>( -ix_, _destroy_L2{ this } );
  664. }
  665. }
  666. ~variant_base_impl() noexcept
  667. {
  668. _destroy();
  669. }
  670. constexpr std::size_t index() const noexcept
  671. {
  672. return ix_ >= 0? ix_ - 1: -ix_ - 1;
  673. }
  674. template<std::size_t I> BOOST_CXX14_CONSTEXPR mp11::mp_at_c<variant<T...>, I>& _get_impl( mp11::mp_size_t<I> ) noexcept
  675. {
  676. size_t const J = I+1;
  677. assert( ix_ == J || -ix_ == J );
  678. constexpr mp11::mp_size_t<J> j{};
  679. return ix_ >= 0? st1_.get( j ): st2_.get( j );
  680. }
  681. template<std::size_t I> constexpr mp11::mp_at_c<variant<T...>, I> const& _get_impl( mp11::mp_size_t<I> ) const noexcept
  682. {
  683. // size_t const J = I+1;
  684. // assert( ix_ == J || -ix_ == J );
  685. // constexpr mp_size_t<J> j{};
  686. return ix_ >= 0? st1_.get( mp11::mp_size_t<I+1>() ): st2_.get( mp11::mp_size_t<I+1>() );
  687. }
  688. template<std::size_t I, class... A> void emplace( A&&... a )
  689. {
  690. size_t const J = I+1;
  691. if( ix_ >= 0 )
  692. {
  693. st2_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  694. _destroy();
  695. ix_ = -static_cast<int>( J );
  696. }
  697. else
  698. {
  699. st1_.emplace( mp11::mp_size_t<J>(), std::forward<A>(a)... );
  700. _destroy();
  701. ix_ = J;
  702. }
  703. }
  704. };
  705. } // namespace detail
  706. // in_place_type_t
  707. template<class T> struct in_place_type_t
  708. {
  709. };
  710. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  711. template<class T> constexpr in_place_type_t<T> in_place_type{};
  712. #endif
  713. namespace detail
  714. {
  715. template<class T> struct is_in_place_type: std::false_type {};
  716. template<class T> struct is_in_place_type<in_place_type_t<T>>: std::true_type {};
  717. } // namespace detail
  718. // in_place_index_t
  719. template<std::size_t I> struct in_place_index_t
  720. {
  721. };
  722. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  723. template<std::size_t I> constexpr in_place_index_t<I> in_place_index{};
  724. #endif
  725. namespace detail
  726. {
  727. template<class T> struct is_in_place_index: std::false_type {};
  728. template<std::size_t I> struct is_in_place_index<in_place_index_t<I>>: std::true_type {};
  729. } // namespace detail
  730. // is_nothrow_swappable
  731. namespace detail
  732. {
  733. namespace det2
  734. {
  735. using std::swap;
  736. template<class T> using is_swappable_impl = decltype(swap(std::declval<T&>(), std::declval<T&>()));
  737. #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  738. template<class T> struct is_nothrow_swappable_impl_
  739. {
  740. static constexpr bool value = noexcept(swap(std::declval<T&>(), std::declval<T&>()));
  741. };
  742. template<class T> using is_nothrow_swappable_impl = mp11::mp_bool< is_nothrow_swappable_impl_<T>::value >;
  743. #else
  744. template<class T> using is_nothrow_swappable_impl = typename std::enable_if<noexcept(swap(std::declval<T&>(), std::declval<T&>()))>::type;
  745. #endif
  746. } // namespace det2
  747. template<class T> struct is_swappable: mp11::mp_valid<det2::is_swappable_impl, T>
  748. {
  749. };
  750. #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  751. 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>
  752. {
  753. };
  754. #else
  755. template<class T> struct is_nothrow_swappable: mp11::mp_valid<det2::is_nothrow_swappable_impl, T>
  756. {
  757. };
  758. #endif
  759. } // namespace detail
  760. // variant
  761. template<class... T> class variant: private detail::variant_base<T...>
  762. {
  763. private:
  764. using variant_base = detail::variant_base<T...>;
  765. private:
  766. variant( variant const volatile& r ) = delete;
  767. variant& operator=( variant const volatile& r ) = delete;
  768. public:
  769. // constructors
  770. template<class E1 = void, class E2 = mp11::mp_if<std::is_default_constructible< mp11::mp_first<variant<T...>> >, E1>>
  771. constexpr variant()
  772. noexcept( std::is_nothrow_default_constructible< mp11::mp_first<variant<T...>> >::value )
  773. : variant_base( mp11::mp_size_t<0>() )
  774. {
  775. }
  776. template<class E1 = void,
  777. class E2 = mp11::mp_if<mp11::mp_all<detail::is_trivially_copy_constructible<T>...>, E1>
  778. >
  779. constexpr variant( variant const& r ) noexcept
  780. : variant_base( static_cast<variant_base const&>(r) )
  781. {
  782. }
  783. private:
  784. struct L1
  785. {
  786. variant_base * this_;
  787. variant const & r;
  788. template<class I> void operator()( I i ) const
  789. {
  790. this_->_replace( i, r._get_impl( i ) );
  791. }
  792. };
  793. public:
  794. template<class E1 = void,
  795. class E2 = mp11::mp_if<mp11::mp_not<mp11::mp_all<detail::is_trivially_copy_constructible<T>...>>, E1>,
  796. class E3 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<T>...>, E1>
  797. >
  798. variant( variant const& r )
  799. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<T>...>::value )
  800. {
  801. mp11::mp_with_index<sizeof...(T)>( r.index(), L1{ this, r } );
  802. }
  803. template<class E1 = void,
  804. class E2 = mp11::mp_if<mp11::mp_all<detail::is_trivially_move_constructible<T>...>, E1>
  805. >
  806. constexpr variant( variant && r ) noexcept
  807. : variant_base( static_cast<variant_base&&>(r) )
  808. {
  809. }
  810. private:
  811. struct L2
  812. {
  813. variant_base * this_;
  814. variant & r;
  815. template<class I> void operator()( I i ) const
  816. {
  817. this_->_replace( i, std::move( r._get_impl( i ) ) );
  818. }
  819. };
  820. public:
  821. template<class E1 = void,
  822. class E2 = mp11::mp_if<mp11::mp_not<mp11::mp_all<detail::is_trivially_move_constructible<T>...>>, E1>,
  823. class E3 = mp11::mp_if<mp11::mp_all<std::is_move_constructible<T>...>, E1>
  824. >
  825. variant( variant && r )
  826. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value )
  827. {
  828. mp11::mp_with_index<sizeof...(T)>( r.index(), L2{ this, r } );
  829. }
  830. template<class U,
  831. class Ud = typename std::decay<U>::type,
  832. 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,
  833. class V = detail::resolve_overload_type<U&&, T...>,
  834. class E2 = typename std::enable_if<std::is_constructible<V, U&&>::value>::type
  835. >
  836. constexpr variant( U&& u )
  837. noexcept( std::is_nothrow_constructible<V, U&&>::value )
  838. : variant_base( detail::resolve_overload_index<U&&, T...>(), std::forward<U>(u) )
  839. {
  840. }
  841. 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>
  842. constexpr explicit variant( in_place_type_t<U>, A&&... a ): variant_base( I(), std::forward<A>(a)... )
  843. {
  844. }
  845. 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>
  846. constexpr explicit variant( in_place_type_t<U>, std::initializer_list<V> il, A&&... a ): variant_base( I(), il, std::forward<A>(a)... )
  847. {
  848. }
  849. 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>
  850. constexpr explicit variant( in_place_index_t<I>, A&&... a ): variant_base( mp11::mp_size_t<I>(), std::forward<A>(a)... )
  851. {
  852. }
  853. 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>
  854. 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)... )
  855. {
  856. }
  857. // assignment
  858. template<class E1 = void,
  859. class E2 = mp11::mp_if<mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_copy_constructible<T>..., detail::is_trivially_copy_assignable<T>...>, E1>
  860. >
  861. BOOST_CXX14_CONSTEXPR variant& operator=( variant const & r ) noexcept
  862. {
  863. static_cast<variant_base&>( *this ) = static_cast<variant_base const&>( r );
  864. return *this;
  865. }
  866. private:
  867. struct L3
  868. {
  869. variant * this_;
  870. variant const & r;
  871. template<class I> void operator()( I i ) const
  872. {
  873. this_->variant_base::template emplace<I::value>( r._get_impl( i ) );
  874. }
  875. };
  876. public:
  877. template<class E1 = void,
  878. class E2 = mp11::mp_if<mp11::mp_not<mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_copy_constructible<T>..., detail::is_trivially_copy_assignable<T>...>>, E1>,
  879. class E3 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<T>..., std::is_copy_assignable<T>...>, E1>
  880. >
  881. BOOST_CXX14_CONSTEXPR variant& operator=( variant const & r )
  882. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<T>...>::value )
  883. {
  884. mp11::mp_with_index<sizeof...(T)>( r.index(), L3{ this, r } );
  885. return *this;
  886. }
  887. template<class E1 = void,
  888. class E2 = mp11::mp_if<mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_move_constructible<T>..., detail::is_trivially_move_assignable<T>...>, E1>
  889. >
  890. BOOST_CXX14_CONSTEXPR variant& operator=( variant && r ) noexcept
  891. {
  892. static_cast<variant_base&>( *this ) = static_cast<variant_base&&>( r );
  893. return *this;
  894. }
  895. private:
  896. struct L4
  897. {
  898. variant * this_;
  899. variant & r;
  900. template<class I> void operator()( I i ) const
  901. {
  902. this_->variant_base::template emplace<I::value>( std::move( r._get_impl( i ) ) );
  903. }
  904. };
  905. public:
  906. template<class E1 = void,
  907. class E2 = mp11::mp_if<mp11::mp_not<mp11::mp_all<std::is_trivially_destructible<T>..., detail::is_trivially_move_constructible<T>..., detail::is_trivially_move_assignable<T>...>>, E1>,
  908. class E3 = mp11::mp_if<mp11::mp_all<std::is_move_constructible<T>..., std::is_move_assignable<T>...>, E1>
  909. >
  910. variant& operator=( variant && r )
  911. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value )
  912. {
  913. mp11::mp_with_index<sizeof...(T)>( r.index(), L4{ this, r } );
  914. return *this;
  915. }
  916. template<class U,
  917. class E1 = typename std::enable_if<!std::is_same<typename std::decay<U>::type, variant>::value>::type,
  918. class V = detail::resolve_overload_type<U, T...>,
  919. class E2 = typename std::enable_if<std::is_assignable<V&, U&&>::value && std::is_constructible<V, U&&>::value>::type
  920. >
  921. BOOST_CXX14_CONSTEXPR variant& operator=( U&& u )
  922. noexcept( std::is_nothrow_constructible<V, U&&>::value )
  923. {
  924. std::size_t const I = detail::resolve_overload_index<U, T...>::value;
  925. this->template emplace<I>( std::forward<U>(u) );
  926. return *this;
  927. }
  928. // modifiers
  929. template<class U, class... A,
  930. class E = typename std::enable_if< mp11::mp_count<variant<T...>, U>::value == 1 && std::is_constructible<U, A&&...>::value >::type>
  931. BOOST_CXX14_CONSTEXPR U& emplace( A&&... a )
  932. {
  933. using I = mp11::mp_find<variant<T...>, U>;
  934. variant_base::template emplace<I::value>( std::forward<A>(a)... );
  935. return _get_impl( I() );
  936. }
  937. template<class U, class V, class... A,
  938. 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>
  939. BOOST_CXX14_CONSTEXPR U& emplace( std::initializer_list<V> il, A&&... a )
  940. {
  941. using I = mp11::mp_find<variant<T...>, U>;
  942. variant_base::template emplace<I::value>( il, std::forward<A>(a)... );
  943. return _get_impl( I() );
  944. }
  945. 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>
  946. BOOST_CXX14_CONSTEXPR variant_alternative_t<I, variant<T...>>& emplace( A&&... a )
  947. {
  948. variant_base::template emplace<I>( std::forward<A>(a)... );
  949. return _get_impl( mp11::mp_size_t<I>() );
  950. }
  951. 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>
  952. BOOST_CXX14_CONSTEXPR variant_alternative_t<I, variant<T...>>& emplace( std::initializer_list<V> il, A&&... a )
  953. {
  954. variant_base::template emplace<I>( il, std::forward<A>(a)... );
  955. return _get_impl( mp11::mp_size_t<I>() );
  956. }
  957. // value status
  958. constexpr bool valueless_by_exception() const noexcept
  959. {
  960. return false;
  961. }
  962. using variant_base::index;
  963. // swap
  964. private:
  965. struct L5
  966. {
  967. variant * this_;
  968. variant & r;
  969. template<class I> void operator()( I i ) const
  970. {
  971. using std::swap;
  972. swap( this_->_get_impl( i ), r._get_impl( i ) );
  973. }
  974. };
  975. public:
  976. void swap( variant& r ) noexcept( mp11::mp_all<std::is_nothrow_move_constructible<T>..., detail::is_nothrow_swappable<T>...>::value )
  977. {
  978. if( index() == r.index() )
  979. {
  980. mp11::mp_with_index<sizeof...(T)>( index(), L5{ this, r } );
  981. }
  982. else
  983. {
  984. variant tmp( std::move(*this) );
  985. *this = std::move( r );
  986. r = std::move( tmp );
  987. }
  988. }
  989. // private accessors
  990. using variant_base::_get_impl;
  991. // converting constructors (extension)
  992. private:
  993. template<class... U> struct L6
  994. {
  995. variant_base * this_;
  996. variant<U...> const & r;
  997. template<class I> void operator()( I i ) const
  998. {
  999. using J = mp11::mp_find<mp11::mp_list<T...>, mp11::mp_at<mp11::mp_list<U...>, I>>;
  1000. this_->_replace( J{}, r._get_impl( i ) );
  1001. }
  1002. };
  1003. public:
  1004. template<class... U,
  1005. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1006. variant( variant<U...> const& r )
  1007. noexcept( mp11::mp_all<std::is_nothrow_copy_constructible<U>...>::value )
  1008. {
  1009. mp11::mp_with_index<sizeof...(U)>( r.index(), L6<U...>{ this, r } );
  1010. }
  1011. private:
  1012. template<class... U> struct L7
  1013. {
  1014. variant_base * this_;
  1015. variant<U...> & r;
  1016. template<class I> void operator()( I i ) const
  1017. {
  1018. using J = mp11::mp_find<mp11::mp_list<T...>, mp11::mp_at<mp11::mp_list<U...>, I>>;
  1019. this_->_replace( J{}, std::move( r._get_impl( i ) ) );
  1020. }
  1021. };
  1022. public:
  1023. template<class... U,
  1024. class E2 = mp11::mp_if<mp11::mp_all<std::is_move_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1025. variant( variant<U...> && r )
  1026. noexcept( mp11::mp_all<std::is_nothrow_move_constructible<U>...>::value )
  1027. {
  1028. mp11::mp_with_index<sizeof...(U)>( r.index(), L7<U...>{ this, r } );
  1029. }
  1030. // subset (extension)
  1031. private:
  1032. 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 )
  1033. {
  1034. return variant<U...>( in_place_index_t<J>(), std::forward<V>(v) );
  1035. }
  1036. template<class... U, class V> static variant<U...> _subset_impl( mp11::mp_size_t<sizeof...(U)>, V && /*v*/ )
  1037. {
  1038. detail::throw_bad_variant_access();
  1039. }
  1040. private:
  1041. template<class... U> struct L8
  1042. {
  1043. variant * this_;
  1044. template<class I> variant<U...> operator()( I i ) const
  1045. {
  1046. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1047. return this_->_subset_impl<U...>( J{}, this_->_get_impl( i ) );
  1048. }
  1049. };
  1050. public:
  1051. template<class... U,
  1052. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1053. BOOST_CXX14_CONSTEXPR variant<U...> subset() &
  1054. {
  1055. return mp11::mp_with_index<sizeof...(T)>( index(), L8<U...>{ this } );
  1056. }
  1057. private:
  1058. template<class... U> struct L9
  1059. {
  1060. variant const * this_;
  1061. template<class I> variant<U...> operator()( I i ) const
  1062. {
  1063. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1064. return this_->_subset_impl<U...>( J{}, this_->_get_impl( i ) );
  1065. }
  1066. };
  1067. public:
  1068. template<class... U,
  1069. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1070. constexpr variant<U...> subset() const&
  1071. {
  1072. return mp11::mp_with_index<sizeof...(T)>( index(), L9<U...>{ this } );
  1073. }
  1074. private:
  1075. template<class... U> struct L10
  1076. {
  1077. variant * this_;
  1078. template<class I> variant<U...> operator()( I i ) const
  1079. {
  1080. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1081. return this_->_subset_impl<U...>( J{}, std::move( this_->_get_impl( i ) ) );
  1082. }
  1083. };
  1084. public:
  1085. template<class... U,
  1086. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1087. BOOST_CXX14_CONSTEXPR variant<U...> subset() &&
  1088. {
  1089. return mp11::mp_with_index<sizeof...(T)>( index(), L10<U...>{ this } );
  1090. }
  1091. #if !BOOST_WORKAROUND(BOOST_GCC, < 40900)
  1092. // g++ 4.8 doesn't handle const&& particularly well
  1093. private:
  1094. template<class... U> struct L11
  1095. {
  1096. variant const * this_;
  1097. template<class I> variant<U...> operator()( I i ) const
  1098. {
  1099. using J = mp11::mp_find<mp11::mp_list<U...>, mp11::mp_at<mp11::mp_list<T...>, I>>;
  1100. return this_->_subset_impl<U...>( J{}, std::move( this_->_get_impl( i ) ) );
  1101. }
  1102. };
  1103. public:
  1104. template<class... U,
  1105. class E2 = mp11::mp_if<mp11::mp_all<std::is_copy_constructible<U>..., mp11::mp_contains<mp11::mp_list<T...>, U>...>, void> >
  1106. constexpr variant<U...> subset() const&&
  1107. {
  1108. return mp11::mp_with_index<sizeof...(T)>( index(), L11<U...>{ this } );
  1109. }
  1110. #endif
  1111. };
  1112. // relational operators
  1113. namespace detail
  1114. {
  1115. template<class... T> struct eq_L
  1116. {
  1117. variant<T...> const & v;
  1118. variant<T...> const & w;
  1119. template<class I> constexpr bool operator()( I i ) const
  1120. {
  1121. return v._get_impl( i ) == w._get_impl( i );
  1122. }
  1123. };
  1124. } // namespace detail
  1125. template<class... T> constexpr bool operator==( variant<T...> const & v, variant<T...> const & w )
  1126. {
  1127. return v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::eq_L<T...>{ v, w } );
  1128. }
  1129. namespace detail
  1130. {
  1131. template<class... T> struct ne_L
  1132. {
  1133. variant<T...> const & v;
  1134. variant<T...> const & w;
  1135. template<class I> constexpr bool operator()( I i ) const
  1136. {
  1137. return v._get_impl( i ) != w._get_impl( i );
  1138. }
  1139. };
  1140. } // namespace detail
  1141. template<class... T> constexpr bool operator!=( variant<T...> const & v, variant<T...> const & w )
  1142. {
  1143. return v.index() != w.index() || mp11::mp_with_index<sizeof...(T)>( v.index(), detail::ne_L<T...>{ v, w } );
  1144. }
  1145. namespace detail
  1146. {
  1147. template<class... T> struct lt_L
  1148. {
  1149. variant<T...> const & v;
  1150. variant<T...> const & w;
  1151. template<class I> constexpr bool operator()( I i ) const
  1152. {
  1153. return v._get_impl( i ) < w._get_impl( i );
  1154. }
  1155. };
  1156. } // namespace detail
  1157. template<class... T> constexpr bool operator<( variant<T...> const & v, variant<T...> const & w )
  1158. {
  1159. return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::lt_L<T...>{ v, w } ) );
  1160. }
  1161. template<class... T> constexpr bool operator>( variant<T...> const & v, variant<T...> const & w )
  1162. {
  1163. return w < v;
  1164. }
  1165. namespace detail
  1166. {
  1167. template<class... T> struct le_L
  1168. {
  1169. variant<T...> const & v;
  1170. variant<T...> const & w;
  1171. template<class I> constexpr bool operator()( I i ) const
  1172. {
  1173. return v._get_impl( i ) <= w._get_impl( i );
  1174. }
  1175. };
  1176. } // namespace detail
  1177. template<class... T> constexpr bool operator<=( variant<T...> const & v, variant<T...> const & w )
  1178. {
  1179. return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::le_L<T...>{ v, w } ) );
  1180. }
  1181. template<class... T> constexpr bool operator>=( variant<T...> const & v, variant<T...> const & w )
  1182. {
  1183. return w <= v;
  1184. }
  1185. // visitation
  1186. namespace detail
  1187. {
  1188. template<class T> using remove_cv_ref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  1189. template<class T, class U> struct copy_cv_ref
  1190. {
  1191. using type = T;
  1192. };
  1193. template<class T, class U> struct copy_cv_ref<T, U const>
  1194. {
  1195. using type = T const;
  1196. };
  1197. template<class T, class U> struct copy_cv_ref<T, U volatile>
  1198. {
  1199. using type = T volatile;
  1200. };
  1201. template<class T, class U> struct copy_cv_ref<T, U const volatile>
  1202. {
  1203. using type = T const volatile;
  1204. };
  1205. template<class T, class U> struct copy_cv_ref<T, U&>
  1206. {
  1207. using type = typename copy_cv_ref<T, U>::type&;
  1208. };
  1209. template<class T, class U> struct copy_cv_ref<T, U&&>
  1210. {
  1211. using type = typename copy_cv_ref<T, U>::type&&;
  1212. };
  1213. template<class T, class U> using copy_cv_ref_t = typename copy_cv_ref<T, U>::type;
  1214. template<class F> struct Qret
  1215. {
  1216. template<class... T> using fn = decltype( std::declval<F>()( std::declval<T>()... ) );
  1217. };
  1218. template<class L> using front_if_same = mp11::mp_if<mp11::mp_apply<mp11::mp_same, L>, mp11::mp_front<L>>;
  1219. template<class V> using apply_cv_ref = mp11::mp_product<copy_cv_ref_t, remove_cv_ref_t<V>, mp11::mp_list<V>>;
  1220. template<class F, class... V> using Vret = front_if_same<mp11::mp_product_q<Qret<F>, apply_cv_ref<V>...>>;
  1221. } // namespace detail
  1222. template<class F> constexpr auto visit( F&& f ) -> decltype(std::forward<F>(f)())
  1223. {
  1224. return std::forward<F>(f)();
  1225. }
  1226. namespace detail
  1227. {
  1228. template<class F, class V1> struct visit_L1
  1229. {
  1230. F&& f;
  1231. V1&& v1;
  1232. template<class I> auto operator()( I ) const -> Vret<F, V1>
  1233. {
  1234. return std::forward<F>(f)( unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1235. }
  1236. };
  1237. } // namespace detail
  1238. template<class F, class V1> constexpr auto visit( F&& f, V1&& v1 ) -> detail::Vret<F, V1>
  1239. {
  1240. return mp11::mp_with_index<variant_size<V1>>( v1.index(), detail::visit_L1<F, V1>{ std::forward<F>(f), std::forward<V1>(v1) } );
  1241. }
  1242. #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) || BOOST_WORKAROUND( BOOST_MSVC, < 1920 )
  1243. namespace detail
  1244. {
  1245. template<class F, class A> struct bind_front_
  1246. {
  1247. F&& f;
  1248. A&& a;
  1249. template<class... T> auto operator()( T&&... t ) -> decltype( std::forward<F>(f)( std::forward<A>(a), std::forward<T>(t)... ) )
  1250. {
  1251. return std::forward<F>(f)( std::forward<A>(a), std::forward<T>(t)... );
  1252. }
  1253. };
  1254. template<class F, class A> bind_front_<F, A> bind_front( F&& f, A&& a )
  1255. {
  1256. return bind_front_<F, A>{ std::forward<F>(f), std::forward<A>(a) };
  1257. }
  1258. template<class F, class V1, class V2> struct visit_L2
  1259. {
  1260. F&& f;
  1261. V1&& v1;
  1262. V2&& v2;
  1263. template<class I> auto operator()( I ) const -> Vret<F, V1, V2>
  1264. {
  1265. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1266. return visit( f2, std::forward<V2>(v2) );
  1267. }
  1268. };
  1269. } // namespace detail
  1270. template<class F, class V1, class V2> constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> detail::Vret<F, V1, V2>
  1271. {
  1272. return mp11::mp_with_index<variant_size<V1>>( v1.index(), detail::visit_L2<F, V1, V2>{ std::forward<F>(f), std::forward<V1>(v1), std::forward<V2>(v2) } );
  1273. }
  1274. namespace detail
  1275. {
  1276. template<class F, class V1, class V2, class V3> struct visit_L3
  1277. {
  1278. F&& f;
  1279. V1&& v1;
  1280. V2&& v2;
  1281. V3&& v3;
  1282. template<class I> auto operator()( I ) const -> Vret<F, V1, V2, V3>
  1283. {
  1284. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1285. return visit( f2, std::forward<V2>(v2), std::forward<V3>(v3) );
  1286. }
  1287. };
  1288. } // namespace detail
  1289. template<class F, class V1, class V2, class V3> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> detail::Vret<F, V1, V2, V3>
  1290. {
  1291. return mp11::mp_with_index<variant_size<V1>>( v1.index(), detail::visit_L3<F, V1, V2, V3>{ std::forward<F>(f), std::forward<V1>(v1), std::forward<V2>(v2), std::forward<V3>(v3) } );
  1292. }
  1293. namespace detail
  1294. {
  1295. template<class F, class V1, class V2, class V3, class V4> struct visit_L4
  1296. {
  1297. F&& f;
  1298. V1&& v1;
  1299. V2&& v2;
  1300. V3&& v3;
  1301. V4&& v4;
  1302. template<class I> auto operator()( I ) const -> Vret<F, V1, V2, V3, V4>
  1303. {
  1304. auto f2 = bind_front( std::forward<F>(f), unsafe_get<I::value>( std::forward<V1>(v1) ) );
  1305. return visit( f2, std::forward<V2>(v2), std::forward<V3>(v3), std::forward<V4>(v4) );
  1306. }
  1307. };
  1308. } // namespace detail
  1309. template<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<F, V1, V2, V3, V4>
  1310. {
  1311. return mp11::mp_with_index<variant_size<V1>>( v1.index(), detail::visit_L4<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) } );
  1312. }
  1313. #else
  1314. template<class F, class V1, class V2, class... V> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V&&... v ) -> detail::Vret<F, V1, V2, V...>
  1315. {
  1316. return mp11::mp_with_index<variant_size<V1>>( v1.index(), [&]( auto I ){
  1317. auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( detail::unsafe_get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
  1318. return visit( f2, std::forward<V2>(v2), std::forward<V>(v)... );
  1319. });
  1320. }
  1321. #endif
  1322. // specialized algorithms
  1323. template<class... T,
  1324. class E = typename std::enable_if<mp11::mp_all<std::is_move_constructible<T>..., detail::is_swappable<T>...>::value>::type>
  1325. void swap( variant<T...> & v, variant<T...> & w )
  1326. noexcept( noexcept(v.swap(w)) )
  1327. {
  1328. v.swap( w );
  1329. }
  1330. } // namespace variant2
  1331. } // namespace boost
  1332. #if defined(_MSC_VER) && _MSC_VER < 1910
  1333. # pragma warning( pop )
  1334. #endif
  1335. #endif // #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED