variant.hpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  1. //-----------------------------------------------------------------------------
  2. // boost variant/variant.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003 Eric Friedman, Itay Maman
  7. // Copyright (c) 2012-2024 Antony Polukhin
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. // Thanks to Adam Romanek for providing patches for exception-disabled env.
  13. #ifndef BOOST_VARIANT_VARIANT_HPP
  14. #define BOOST_VARIANT_VARIANT_HPP
  15. #include <cstddef> // for std::size_t
  16. #include <new> // for placement new
  17. #include <boost/type_index.hpp>
  18. #include <boost/variant/detail/config.hpp>
  19. #include <boost/mpl/aux_/value_wknd.hpp>
  20. #include <boost/variant/variant_fwd.hpp>
  21. #include <boost/variant/detail/backup_holder.hpp>
  22. #include <boost/variant/detail/enable_recursive_fwd.hpp>
  23. #include <boost/variant/detail/forced_return.hpp>
  24. #include <boost/variant/detail/initializer.hpp>
  25. #include <boost/variant/detail/make_variant_list.hpp>
  26. #include <boost/variant/detail/over_sequence.hpp>
  27. #include <boost/variant/detail/visitation_impl.hpp>
  28. #include <boost/variant/detail/hash_variant.hpp>
  29. #include <boost/variant/detail/std_hash.hpp>
  30. #include <boost/variant/detail/move.hpp>
  31. #include <boost/detail/reference_content.hpp>
  32. #include <boost/blank.hpp>
  33. #include <boost/integer/common_factor_ct.hpp>
  34. #include <boost/static_assert.hpp>
  35. #include <boost/type_traits/aligned_storage.hpp>
  36. #include <boost/type_traits/alignment_of.hpp>
  37. #include <boost/type_traits/add_const.hpp>
  38. #include <boost/type_traits/has_nothrow_constructor.hpp>
  39. #include <boost/type_traits/has_nothrow_copy.hpp>
  40. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  41. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  42. #include <boost/type_traits/is_const.hpp>
  43. #include <boost/type_traits/is_same.hpp>
  44. #include <boost/type_traits/is_rvalue_reference.hpp>
  45. #include <boost/type_traits/is_constructible.hpp>
  46. #include <boost/type_traits/add_lvalue_reference.hpp>
  47. #include <boost/type_traits/declval.hpp>
  48. #include <boost/core/no_exceptions_support.hpp>
  49. #include <boost/core/enable_if.hpp>
  50. #include <boost/variant/recursive_wrapper_fwd.hpp>
  51. #include <boost/variant/static_visitor.hpp>
  52. #include <boost/mpl/assert.hpp>
  53. #include <boost/mpl/begin_end.hpp>
  54. #include <boost/mpl/bool.hpp>
  55. #include <boost/mpl/deref.hpp>
  56. #include <boost/mpl/empty.hpp>
  57. #include <boost/mpl/eval_if.hpp>
  58. #include <boost/mpl/find_if.hpp>
  59. #include <boost/mpl/fold.hpp>
  60. #include <boost/mpl/front.hpp>
  61. #include <boost/mpl/identity.hpp>
  62. #include <boost/mpl/if.hpp>
  63. #include <boost/mpl/insert_range.hpp>
  64. #include <boost/mpl/int.hpp>
  65. #include <boost/mpl/is_sequence.hpp>
  66. #include <boost/mpl/iterator_range.hpp>
  67. #include <boost/mpl/iter_fold_if.hpp>
  68. #include <boost/mpl/list.hpp>
  69. #include <boost/mpl/logical.hpp>
  70. #include <boost/mpl/max_element.hpp>
  71. #include <boost/mpl/next.hpp>
  72. #include <boost/mpl/not.hpp>
  73. #include <boost/mpl/pair.hpp>
  74. #include <boost/mpl/protect.hpp>
  75. #include <boost/mpl/push_front.hpp>
  76. #include <boost/mpl/same_as.hpp>
  77. #include <boost/mpl/size_t.hpp>
  78. #include <boost/mpl/sizeof.hpp>
  79. #include <boost/mpl/transform.hpp>
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // Implementation Macros:
  82. //
  83. // BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
  84. // Defined in boost/variant/detail/visitation_impl.hpp.
  85. //
  86. // BOOST_VARIANT_MINIMIZE_SIZE
  87. // When #defined, implementation employs all known means to minimize the
  88. // size of variant obje cts. However, often unsuccessful due to alignment
  89. // issues, and potentially harmful to runtime speed, so not enabled by
  90. // default. (TODO: Investigate further.)
  91. #if defined(BOOST_VARIANT_MINIMIZE_SIZE)
  92. # include <climits> // for SCHAR_MAX
  93. # include <boost/mpl/eval_if.hpp>
  94. # include <boost/mpl/equal_to.hpp>
  95. # include <boost/mpl/identity.hpp>
  96. # include <boost/mpl/int.hpp>
  97. # include <boost/mpl/if.hpp>
  98. # include <boost/mpl/less.hpp>
  99. # include <boost/mpl/long.hpp>
  100. # include <boost/mpl/O1_size.hpp>
  101. #endif
  102. namespace boost {
  103. namespace detail { namespace variant {
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // (detail) metafunction max_value
  106. //
  107. // Finds the maximum value of the unary metafunction F over Sequence.
  108. //
  109. template <typename Sequence, typename F>
  110. struct max_value
  111. {
  112. private: // helpers, for metafunction result (below)
  113. typedef typename mpl::transform1<Sequence, F>::type transformed_;
  114. typedef typename mpl::max_element<transformed_
  115. >::type max_it;
  116. public: // metafunction result
  117. typedef typename mpl::deref<max_it>::type
  118. type;
  119. };
  120. struct add_alignment
  121. {
  122. template <typename State, typename Item>
  123. struct apply
  124. : mpl::size_t<
  125. ::boost::integer::static_lcm<
  126. BOOST_MPL_AUX_VALUE_WKND(State)::value
  127. , ::boost::alignment_of<Item>::value
  128. >::value
  129. >
  130. {};
  131. };
  132. ///////////////////////////////////////////////////////////////////////////////
  133. // (detail) metafunction find_fallback_type
  134. //
  135. // Provides a fallback (i.e., nothrow default-constructible) type from the
  136. // specified sequence, or no_fallback_type if not found.
  137. //
  138. // This implementation is designed to prefer boost::blank over other potential
  139. // fallback types, regardless of its position in the specified sequence.
  140. //
  141. class no_fallback_type;
  142. struct find_fallback_type_pred
  143. {
  144. template <typename Iterator>
  145. struct apply
  146. {
  147. private:
  148. typedef typename mpl::deref<Iterator>::type t_;
  149. public:
  150. typedef mpl::not_< has_nothrow_constructor<t_> > type;
  151. };
  152. };
  153. template <typename Types>
  154. struct find_fallback_type
  155. {
  156. private: // helpers, for metafunction result (below)
  157. typedef typename mpl::end<Types>::type end_it;
  158. // [Find the first suitable fallback type...]
  159. typedef typename mpl::iter_fold_if<
  160. Types
  161. , mpl::int_<0>, mpl::protect< mpl::next<> >
  162. , mpl::protect< find_fallback_type_pred >
  163. >::type first_result_;
  164. typedef typename first_result_::first first_result_index;
  165. typedef typename first_result_::second first_result_it;
  166. // [...now search the rest of the sequence for boost::blank...]
  167. typedef typename mpl::iter_fold_if<
  168. mpl::iterator_range< first_result_it,end_it >
  169. , first_result_index, mpl::protect< mpl::next<> >
  170. , mpl::protect< mpl::not_same_as<boost::blank> >
  171. >::type second_result_;
  172. typedef typename second_result_::second second_result_it;
  173. public: // metafunction result
  174. // [...and return the results of the search:]
  175. typedef typename mpl::eval_if<
  176. is_same< second_result_it,end_it >
  177. , mpl::if_<
  178. is_same< first_result_it,end_it >
  179. , mpl::pair< no_fallback_type,no_fallback_type >
  180. , first_result_
  181. >
  182. , mpl::identity< second_result_ >
  183. >::type type;
  184. };
  185. ///////////////////////////////////////////////////////////////////////////////
  186. // (detail) metafunction is_variant_move_noexcept_constructible
  187. //
  188. // Returns true_type if all the types are nothrow move constructible.
  189. //
  190. template <class Types>
  191. struct is_variant_move_noexcept_constructible {
  192. typedef typename boost::mpl::find_if<
  193. Types, mpl::not_<boost::is_nothrow_move_constructible<boost::mpl::_1> >
  194. >::type iterator_t;
  195. typedef typename boost::mpl::end<Types>::type end_t;
  196. typedef typename boost::is_same<
  197. iterator_t, end_t
  198. >::type type;
  199. };
  200. ///////////////////////////////////////////////////////////////////////////////
  201. // (detail) metafunction is_variant_move_noexcept_assignable
  202. //
  203. // Returns true_type if all the types are nothrow move constructible.
  204. //
  205. template <class Types>
  206. struct is_variant_move_noexcept_assignable {
  207. typedef typename boost::mpl::find_if<
  208. Types, mpl::not_<boost::is_nothrow_move_assignable<boost::mpl::_1> >
  209. >::type iterator_t;
  210. typedef typename boost::mpl::end<Types>::type end_t;
  211. typedef typename boost::is_same<
  212. iterator_t, end_t
  213. >::type type;
  214. };
  215. ///////////////////////////////////////////////////////////////////////////////
  216. // (detail) metafunction is_variant_constructible_from
  217. //
  218. // Derives from true_type if at least one variant's type is constructible from T.
  219. //
  220. template <class T1, class T2>
  221. struct is_constructible_ext:
  222. boost::mpl::or_<
  223. boost::is_constructible<
  224. T1,
  225. T2
  226. >,
  227. boost::is_constructible<
  228. T1,
  229. typename boost::add_lvalue_reference<T2>::type
  230. >
  231. >
  232. {};
  233. template <class T, class Types>
  234. struct is_variant_constructible_from:
  235. boost::mpl::not_< boost::is_same<
  236. typename boost::mpl::find_if<
  237. Types,
  238. is_constructible_ext<boost::mpl::_1, T>
  239. >::type,
  240. typename boost::mpl::end<Types>::type
  241. > >
  242. {};
  243. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  244. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >:
  245. boost::is_same<
  246. typename boost::mpl::find_if<
  247. typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types,
  248. mpl::not_< is_variant_constructible_from< boost::mpl::_1, Types> >
  249. >::type,
  250. typename boost::mpl::end< typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types >::type
  251. >
  252. {};
  253. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  254. struct is_variant_constructible_from< const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
  255. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  256. {};
  257. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  258. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
  259. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  260. {};
  261. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  262. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>&& , Types >:
  263. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  264. {};
  265. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  266. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const && , Types >:
  267. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  268. {};
  269. ///////////////////////////////////////////////////////////////////////////////
  270. // (detail) metafunction make_storage
  271. //
  272. // Provides an aligned storage type capable of holding any of the types
  273. // specified in the given type-sequence.
  274. //
  275. template <typename Types, typename NeverUsesBackupFlag>
  276. struct make_storage
  277. {
  278. private: // helpers, for metafunction result (below)
  279. typedef typename mpl::eval_if<
  280. NeverUsesBackupFlag
  281. , mpl::identity< Types >
  282. , mpl::push_front<
  283. Types, backup_holder<void*>
  284. >
  285. >::type types;
  286. typedef typename max_value<
  287. types, mpl::sizeof_<mpl::_1>
  288. >::type max_size;
  289. #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0551))
  290. typedef typename mpl::fold<
  291. types
  292. , mpl::size_t<1>
  293. , add_alignment
  294. >::type max_alignment;
  295. #else // borland
  296. // temporary workaround -- use maximal alignment
  297. typedef mpl::size_t< -1 > max_alignment;
  298. #endif // borland workaround
  299. public: // metafunction result
  300. typedef ::boost::aligned_storage<
  301. BOOST_MPL_AUX_VALUE_WKND(max_size)::value
  302. , BOOST_MPL_AUX_VALUE_WKND(max_alignment)::value
  303. > type;
  304. };
  305. ///////////////////////////////////////////////////////////////////////////////
  306. // (detail) class destroyer
  307. //
  308. // Internal visitor that destroys the value it visits.
  309. //
  310. struct destroyer
  311. : public static_visitor<>
  312. {
  313. public: // visitor interfaces
  314. template <typename T>
  315. void internal_visit(T& operand, int) const BOOST_NOEXCEPT
  316. {
  317. operand.~T(); // must be noexcept
  318. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0551)) || \
  319. BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  320. (void)operand; // suppresses warnings
  321. #endif
  322. }
  323. };
  324. ///////////////////////////////////////////////////////////////////////////////
  325. // (detail) class template known_get
  326. //
  327. // Visitor that returns a reference to content of the specified type.
  328. //
  329. // Precondition: visited variant MUST contain logical content of type T.
  330. //
  331. template <typename T>
  332. class known_get
  333. : public static_visitor<T&>
  334. {
  335. public: // visitor interface
  336. T& operator()(T& operand) const BOOST_NOEXCEPT
  337. {
  338. return operand;
  339. }
  340. template <typename U>
  341. T& operator()(U&) const
  342. {
  343. // logical error to be here: see precondition above
  344. return ::boost::detail::variant::forced_return< T& >();
  345. }
  346. };
  347. ///////////////////////////////////////////////////////////////////////////////
  348. // (detail) class copy_into
  349. //
  350. // Internal visitor that copies the value it visits into the given buffer.
  351. //
  352. class copy_into
  353. : public static_visitor<>
  354. {
  355. private: // representation
  356. void* storage_;
  357. public: // structors
  358. explicit copy_into(void* storage) BOOST_NOEXCEPT
  359. : storage_(storage)
  360. {
  361. }
  362. public: // internal visitor interface
  363. template <typename T>
  364. void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  365. {
  366. new(storage_) T( operand.get() );
  367. }
  368. template <typename T>
  369. void internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  370. {
  371. new(storage_) T( operand.get() );
  372. }
  373. template <typename T>
  374. void internal_visit(const T& operand, int) const
  375. {
  376. new(storage_) T(operand);
  377. }
  378. };
  379. ///////////////////////////////////////////////////////////////////////////////
  380. // (detail) class move_into
  381. //
  382. // Internal visitor that moves the value it visits into the given buffer.
  383. //
  384. class move_into
  385. : public static_visitor<>
  386. {
  387. private: // representation
  388. void* storage_;
  389. public: // structors
  390. explicit move_into(void* storage) BOOST_NOEXCEPT
  391. : storage_(storage)
  392. {
  393. }
  394. public: // internal visitor interface
  395. template <typename T>
  396. void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  397. {
  398. new(storage_) T( ::boost::detail::variant::move(operand.get()) );
  399. }
  400. template <typename T>
  401. void internal_visit(T& operand, int) const BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(boost::declval<T>())))
  402. {
  403. new(storage_) T(::boost::detail::variant::move(operand));
  404. }
  405. };
  406. ///////////////////////////////////////////////////////////////////////////////
  407. // (detail) class assign_storage
  408. //
  409. // Internal visitor that assigns the given storage (which must be a
  410. // constructed value of the same type) to the value it visits.
  411. //
  412. struct assign_storage
  413. : public static_visitor<>
  414. {
  415. private: // representation
  416. const void* rhs_storage_;
  417. public: // structors
  418. explicit assign_storage(const void* rhs_storage) BOOST_NOEXCEPT
  419. : rhs_storage_(rhs_storage)
  420. {
  421. }
  422. public: // internal visitor interfaces
  423. template <typename T>
  424. void internal_visit(backup_holder<T>& lhs_content, long) const
  425. {
  426. lhs_content.get()
  427. = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
  428. }
  429. template <typename T>
  430. void internal_visit(const backup_holder<T>& lhs_content, long) const
  431. {
  432. lhs_content.get()
  433. = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
  434. }
  435. template <typename T>
  436. void internal_visit(T& lhs_content, int) const
  437. {
  438. // NOTE TO USER :
  439. // Compile error here indicates one of variant's bounded types does
  440. // not meet the requirements of the Assignable concept. Thus,
  441. // variant is not Assignable.
  442. //
  443. // Hint: Are any of the bounded types const-qualified or references?
  444. //
  445. lhs_content = *static_cast< const T* >(rhs_storage_);
  446. }
  447. };
  448. ///////////////////////////////////////////////////////////////////////////////
  449. // (detail) class move_storage
  450. //
  451. // Internal visitor that moves the given storage (which must be a
  452. // constructed value of the same type) to the value it visits.
  453. //
  454. struct move_storage
  455. : public static_visitor<>
  456. {
  457. private: // representation
  458. void* rhs_storage_;
  459. public: // structors
  460. explicit move_storage(void* rhs_storage) BOOST_NOEXCEPT
  461. : rhs_storage_(rhs_storage)
  462. {
  463. }
  464. public: // internal visitor interfaces
  465. template <typename T>
  466. void internal_visit(backup_holder<T>& lhs_content, long) const
  467. {
  468. lhs_content.get()
  469. = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
  470. }
  471. template <typename T>
  472. void internal_visit(const backup_holder<T>& lhs_content, long) const
  473. {
  474. lhs_content.get()
  475. = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
  476. }
  477. template <typename T>
  478. void internal_visit(T& lhs_content, int) const
  479. {
  480. // NOTE TO USER :
  481. // Compile error here indicates one of variant's bounded types does
  482. // not meet the requirements of the Assignable concept. Thus,
  483. // variant is not Assignable.
  484. //
  485. // Hint: Are any of the bounded types const-qualified or references?
  486. //
  487. lhs_content = ::boost::detail::variant::move(*static_cast<T* >(rhs_storage_));
  488. }
  489. };
  490. ///////////////////////////////////////////////////////////////////////////////
  491. // (detail) class direct_assigner
  492. //
  493. // Generic static visitor that: if and only if the visited value is of the
  494. // specified type, assigns the given value to the visited value and returns
  495. // true; else returns false.
  496. //
  497. template <typename T>
  498. class direct_assigner
  499. : public static_visitor<bool>
  500. {
  501. private: // representation
  502. const T& rhs_;
  503. public: // structors
  504. explicit direct_assigner(const T& rhs) BOOST_NOEXCEPT
  505. : rhs_(rhs)
  506. {
  507. }
  508. public: // visitor interface
  509. bool operator()(T& lhs)
  510. {
  511. lhs = rhs_;
  512. return true;
  513. }
  514. template <typename U>
  515. bool operator()(U&) BOOST_NOEXCEPT
  516. {
  517. return false;
  518. }
  519. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  520. private:
  521. // silence MSVC warning C4512: assignment operator could not be generated
  522. direct_assigner& operator= (direct_assigner const&);
  523. #endif
  524. };
  525. ///////////////////////////////////////////////////////////////////////////////
  526. // (detail) class direct_mover
  527. //
  528. // Generic static visitor that: if and only if the visited value is of the
  529. // specified type, move assigns the given value to the visited value and returns
  530. // true; else returns false.
  531. //
  532. template <typename T>
  533. class direct_mover
  534. : public static_visitor<bool>
  535. {
  536. private: // representation
  537. T& rhs_;
  538. public: // structors
  539. explicit direct_mover(T& rhs) BOOST_NOEXCEPT
  540. : rhs_(rhs)
  541. {
  542. }
  543. public: // visitor interface
  544. bool operator()(T& lhs)
  545. {
  546. lhs = ::boost::detail::variant::move(rhs_);
  547. return true;
  548. }
  549. template <typename U>
  550. bool operator()(U&) BOOST_NOEXCEPT
  551. {
  552. return false;
  553. }
  554. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  555. private:
  556. // silence MSVC warning C4512: assignment operator could not be generated
  557. direct_mover& operator= (direct_mover const&);
  558. #endif
  559. };
  560. ///////////////////////////////////////////////////////////////////////////////
  561. // (detail) class backup_assigner
  562. //
  563. // Internal visitor that "assigns" the given value to the visited value,
  564. // using backup to recover if the destroy-copy sequence fails.
  565. //
  566. // NOTE: This needs to be a friend of variant, as it needs access to
  567. // indicate_which, indicate_backup_which, etc.
  568. //
  569. template <typename Variant>
  570. class backup_assigner
  571. : public static_visitor<>
  572. {
  573. private: // representation
  574. Variant& lhs_;
  575. int rhs_which_;
  576. const void* rhs_content_;
  577. void (*copy_rhs_content_)(void*, const void*);
  578. public: // structors
  579. template<class RhsT>
  580. backup_assigner(Variant& lhs, int rhs_which, const RhsT& rhs_content)
  581. : lhs_(lhs)
  582. , rhs_which_(rhs_which)
  583. , rhs_content_(&rhs_content)
  584. , copy_rhs_content_(&construct_impl<RhsT>)
  585. {
  586. }
  587. private: // helpers, for visitor interface (below)
  588. template<class RhsT>
  589. static void construct_impl(void* addr, const void* obj)
  590. {
  591. new(addr) RhsT(*static_cast<const RhsT*>(obj));
  592. }
  593. template <typename LhsT>
  594. void backup_assign_impl(
  595. backup_holder<LhsT>& lhs_content
  596. , mpl::false_ // is_nothrow_move_constructible
  597. , long
  598. )
  599. {
  600. // Move lhs content to backup...
  601. backup_holder<LhsT> backup_lhs_content(0);
  602. backup_lhs_content.swap(lhs_content); // nothrow
  603. // ...destroy lhs content...
  604. lhs_content.~backup_holder<LhsT>(); // nothrow
  605. BOOST_TRY
  606. {
  607. // ...and attempt to copy rhs content into lhs storage:
  608. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  609. }
  610. BOOST_CATCH (...)
  611. {
  612. // In case of failure, copy backup pointer to lhs storage...
  613. new(lhs_.storage_.address())
  614. backup_holder<LhsT>( 0 ); // nothrow
  615. static_cast<backup_holder<LhsT>* >(lhs_.storage_.address())
  616. ->swap(backup_lhs_content); // nothrow
  617. // ...and rethrow:
  618. BOOST_RETHROW;
  619. }
  620. BOOST_CATCH_END
  621. // In case of success, indicate new content type:
  622. lhs_.indicate_which(rhs_which_); // nothrow
  623. }
  624. template <typename LhsT>
  625. void backup_assign_impl(
  626. LhsT& lhs_content
  627. , mpl::true_ // is_nothrow_move_constructible
  628. , int
  629. )
  630. {
  631. // Move lhs content to backup...
  632. LhsT backup_lhs_content(
  633. ::boost::detail::variant::move(lhs_content)
  634. ); // nothrow
  635. // ...destroy lhs content...
  636. lhs_content.~LhsT(); // nothrow
  637. BOOST_TRY
  638. {
  639. // ...and attempt to copy rhs content into lhs storage:
  640. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  641. }
  642. BOOST_CATCH (...)
  643. {
  644. // In case of failure, restore backup content to lhs storage...
  645. new(lhs_.storage_.address())
  646. LhsT(
  647. ::boost::detail::variant::move(backup_lhs_content)
  648. ); // nothrow
  649. // ...and rethrow:
  650. BOOST_RETHROW;
  651. }
  652. BOOST_CATCH_END
  653. // In case of success, indicate new content type:
  654. lhs_.indicate_which(rhs_which_); // nothrow
  655. }
  656. template <typename LhsT>
  657. void backup_assign_impl(
  658. LhsT& lhs_content
  659. , mpl::false_ // is_nothrow_move_constructible
  660. , int
  661. )
  662. {
  663. // Backup lhs content...
  664. LhsT* backup_lhs_ptr = new LhsT(lhs_content);
  665. // ...destroy lhs content...
  666. lhs_content.~LhsT(); // nothrow
  667. BOOST_TRY
  668. {
  669. // ...and attempt to copy rhs content into lhs storage:
  670. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  671. }
  672. BOOST_CATCH (...)
  673. {
  674. // In case of failure, copy backup pointer to lhs storage...
  675. new(lhs_.storage_.address())
  676. backup_holder<LhsT>( backup_lhs_ptr ); // nothrow
  677. // ...indicate now using backup...
  678. lhs_.indicate_backup_which( lhs_.which() ); // nothrow
  679. // ...and rethrow:
  680. BOOST_RETHROW;
  681. }
  682. BOOST_CATCH_END
  683. // In case of success, indicate new content type...
  684. lhs_.indicate_which(rhs_which_); // nothrow
  685. // ...and delete backup:
  686. delete backup_lhs_ptr; // nothrow
  687. }
  688. public: // visitor interface
  689. template <typename LhsT>
  690. void internal_visit(LhsT& lhs_content, int)
  691. {
  692. typedef typename is_nothrow_move_constructible<LhsT>::type
  693. nothrow_move;
  694. backup_assign_impl( lhs_content, nothrow_move(), 1L);
  695. }
  696. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  697. private:
  698. // silence MSVC warning C4512: assignment operator could not be generated
  699. backup_assigner& operator= (backup_assigner const&);
  700. #endif
  701. };
  702. ///////////////////////////////////////////////////////////////////////////////
  703. // (detail) class swap_with
  704. //
  705. // Visitor that swaps visited value with content of given variant.
  706. //
  707. // Precondition: Given variant MUST have same logical type as visited value.
  708. //
  709. template <typename Variant>
  710. struct swap_with
  711. : public static_visitor<>
  712. {
  713. private: // representation
  714. Variant& toswap_;
  715. public: // structors
  716. explicit swap_with(Variant& toswap) BOOST_NOEXCEPT
  717. : toswap_(toswap)
  718. {
  719. }
  720. public: // internal visitor interfaces
  721. template <typename T>
  722. void operator()(T& operand) const
  723. {
  724. // Since the precondition ensures types are same, get T...
  725. known_get<T> getter;
  726. T& other = toswap_.apply_visitor(getter);
  727. // ...and swap:
  728. ::boost::detail::variant::move_swap( operand, other );
  729. }
  730. private:
  731. swap_with& operator=(const swap_with&);
  732. };
  733. ///////////////////////////////////////////////////////////////////////////////
  734. // (detail) class reflect
  735. //
  736. // Generic static visitor that performs a typeid on the value it visits.
  737. //
  738. class reflect
  739. : public static_visitor<const boost::typeindex::type_info&>
  740. {
  741. public: // visitor interfaces
  742. template <typename T>
  743. const boost::typeindex::type_info& operator()(const T&) const BOOST_NOEXCEPT
  744. {
  745. return boost::typeindex::type_id<T>().type_info();
  746. }
  747. };
  748. ///////////////////////////////////////////////////////////////////////////////
  749. // (detail) class comparer
  750. //
  751. // Generic static visitor that compares the content of the given lhs variant
  752. // with the visited rhs content using Comp.
  753. //
  754. // Precondition: lhs.which() == rhs.which()
  755. //
  756. template <typename Variant, typename Comp>
  757. class comparer
  758. : public static_visitor<bool>
  759. {
  760. private: // representation
  761. const Variant& lhs_;
  762. public: // structors
  763. explicit comparer(const Variant& lhs) BOOST_NOEXCEPT
  764. : lhs_(lhs)
  765. {
  766. }
  767. public: // visitor interfaces
  768. template <typename T>
  769. bool operator()(T& rhs_content) const
  770. {
  771. // Since the precondition ensures lhs and rhs types are same, get T...
  772. known_get<T> getter;
  773. const T& lhs_content = lhs_.apply_visitor(getter);
  774. // ...and compare lhs and rhs contents:
  775. return Comp()(lhs_content, rhs_content);
  776. }
  777. private:
  778. comparer& operator=(const comparer&);
  779. };
  780. ///////////////////////////////////////////////////////////////////////////////
  781. // (detail) class equal_comp
  782. //
  783. // Generic function object compares lhs with rhs using operator==.
  784. //
  785. struct equal_comp
  786. {
  787. template <typename T>
  788. bool operator()(const T& lhs, const T& rhs) const
  789. {
  790. return lhs == rhs;
  791. }
  792. };
  793. ///////////////////////////////////////////////////////////////////////////////
  794. // (detail) class less_comp
  795. //
  796. // Generic function object compares lhs with rhs using operator<.
  797. //
  798. struct less_comp
  799. {
  800. template <typename T>
  801. bool operator()(const T& lhs, const T& rhs) const
  802. {
  803. return lhs < rhs;
  804. }
  805. };
  806. ///////////////////////////////////////////////////////////////////////////////
  807. // (detail) class template invoke_visitor
  808. //
  809. // Internal visitor that invokes the given visitor using:
  810. // * for wrappers (e.g., recursive_wrapper), the wrapper's held value.
  811. // * for all other values, the value itself.
  812. //
  813. template <typename Visitor, bool MoveSemantics>
  814. class invoke_visitor
  815. {
  816. private: // representation
  817. Visitor& visitor_;
  818. public: // visitor typedefs
  819. typedef typename Visitor::result_type
  820. result_type;
  821. public: // structors
  822. explicit invoke_visitor(Visitor& visitor) BOOST_NOEXCEPT
  823. : visitor_(visitor)
  824. {
  825. }
  826. public: // internal visitor interfaces
  827. //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
  828. template <typename T>
  829. typename enable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
  830. {
  831. return visitor_(std::move(operand));
  832. }
  833. //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
  834. template <typename T>
  835. typename disable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
  836. {
  837. return visitor_(operand);
  838. }
  839. public: // internal visitor interfaces, cont.
  840. template <typename T>
  841. result_type internal_visit(boost::recursive_wrapper<T>& operand, long)
  842. {
  843. return internal_visit( operand.get(), 1L );
  844. }
  845. template <typename T>
  846. result_type internal_visit(const boost::recursive_wrapper<T>& operand, long)
  847. {
  848. return internal_visit( operand.get(), 1L );
  849. }
  850. template <typename T>
  851. result_type internal_visit(boost::detail::reference_content<T>& operand, long)
  852. {
  853. return internal_visit( operand.get(), 1L );
  854. }
  855. template <typename T>
  856. result_type internal_visit(const boost::detail::reference_content<T>& operand, long)
  857. {
  858. return internal_visit( operand.get(), 1L );
  859. }
  860. template <typename T>
  861. result_type internal_visit(boost::detail::variant::backup_holder<T>& operand, long)
  862. {
  863. return internal_visit( operand.get(), 1L );
  864. }
  865. template <typename T>
  866. result_type internal_visit(const boost::detail::variant::backup_holder<T>& operand, long)
  867. {
  868. return internal_visit( operand.get(), 1L );
  869. }
  870. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  871. private:
  872. // silence MSVC warning C4512: assignment operator could not be generated
  873. invoke_visitor& operator= (invoke_visitor const&);
  874. #endif
  875. };
  876. }} // namespace detail::variant
  877. ///////////////////////////////////////////////////////////////////////////////
  878. // class template variant (concept inspired by Andrei Alexandrescu)
  879. //
  880. // See docs and boost/variant/variant_fwd.hpp for more information.
  881. //
  882. template <
  883. typename T0_
  884. , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)
  885. >
  886. class variant
  887. {
  888. private: // helpers, for typedefs (below)
  889. typedef variant wknd_self_t;
  890. struct is_recursive_
  891. : detail::variant::is_recursive_flag<T0_>
  892. {
  893. };
  894. typedef typename mpl::eval_if<
  895. is_recursive_
  896. , T0_
  897. , mpl::identity< T0_ >
  898. >::type unwrapped_T0_;
  899. struct is_sequence_based_
  900. : detail::variant::is_over_sequence<unwrapped_T0_>
  901. {
  902. };
  903. private: // helpers, for typedefs (below)
  904. typedef typename mpl::eval_if<
  905. is_sequence_based_
  906. , unwrapped_T0_ // over_sequence<...>::type
  907. , detail::variant::make_variant_list<
  908. unwrapped_T0_
  909. , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(T)
  910. >
  911. >::type specified_types;
  912. BOOST_STATIC_ASSERT((
  913. ::boost::mpl::not_< mpl::empty<specified_types> >::value
  914. ));
  915. public: // public typedefs
  916. typedef typename mpl::eval_if<
  917. is_recursive_
  918. , mpl::transform<
  919. specified_types
  920. , mpl::protect<
  921. detail::variant::quoted_enable_recursive<wknd_self_t>
  922. >
  923. >
  924. , mpl::identity< specified_types >
  925. >::type recursive_enabled_types; // used by is_variant_constructible_from<> trait
  926. typedef typename mpl::transform<
  927. recursive_enabled_types
  928. , unwrap_recursive<mpl::_1>
  929. >::type types;
  930. private: // internal typedefs
  931. typedef typename mpl::transform<
  932. recursive_enabled_types
  933. , mpl::protect< detail::make_reference_content<> >
  934. >::type internal_types;
  935. typedef typename mpl::front<
  936. internal_types
  937. >::type internal_T0;
  938. private: // helpers, for representation (below)
  939. typedef typename detail::variant::find_fallback_type<
  940. internal_types
  941. >::type fallback_type_result_;
  942. typedef typename fallback_type_result_::first
  943. fallback_type_index_;
  944. typedef typename fallback_type_result_::second
  945. fallback_type_;
  946. struct has_fallback_type_
  947. : mpl::not_<
  948. is_same< fallback_type_, detail::variant::no_fallback_type >
  949. >
  950. {
  951. };
  952. typedef has_fallback_type_
  953. never_uses_backup_flag;
  954. typedef typename detail::variant::make_storage<
  955. internal_types, never_uses_backup_flag
  956. >::type storage_t;
  957. typedef typename detail::variant::is_variant_move_noexcept_constructible<
  958. internal_types
  959. > variant_move_noexcept_constructible;
  960. typedef typename detail::variant::is_variant_move_noexcept_assignable<
  961. internal_types
  962. > variant_move_noexcept_assignable;
  963. private: // helpers, for representation (below)
  964. // which_ on:
  965. // * [0, size<internal_types>) indicates stack content
  966. // * [-size<internal_types>, 0) indicates pointer to heap backup
  967. // if which_ >= 0:
  968. // * then which() -> which_
  969. // * else which() -> -(which_ + 1)
  970. #if !defined(BOOST_VARIANT_MINIMIZE_SIZE)
  971. typedef int which_t;
  972. #else // defined(BOOST_VARIANT_MINIMIZE_SIZE)
  973. // [if O1_size available, then attempt which_t size optimization...]
  974. // [select signed char if fewer than SCHAR_MAX types, else signed int:]
  975. typedef typename mpl::eval_if<
  976. mpl::equal_to< mpl::O1_size<internal_types>, mpl::long_<-1> >
  977. , mpl::identity< int >
  978. , mpl::if_<
  979. mpl::less< mpl::O1_size<internal_types>, mpl::int_<SCHAR_MAX> >
  980. , signed char
  981. , int
  982. >
  983. >::type which_t;
  984. #endif // BOOST_VARIANT_MINIMIZE_SIZE switch
  985. // representation -- private when possible
  986. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  987. private:
  988. #else
  989. public:
  990. #endif
  991. which_t which_;
  992. storage_t storage_;
  993. void indicate_which(int which_arg) BOOST_NOEXCEPT
  994. {
  995. which_ = static_cast<which_t>( which_arg );
  996. }
  997. void indicate_backup_which(int which_arg) BOOST_NOEXCEPT
  998. {
  999. which_ = static_cast<which_t>( -(which_arg + 1) );
  1000. }
  1001. private: // helpers, for queries (below)
  1002. bool using_backup() const BOOST_NOEXCEPT
  1003. {
  1004. return which_ < 0;
  1005. }
  1006. public: // queries
  1007. int which() const BOOST_NOEXCEPT
  1008. {
  1009. // If using heap backup...
  1010. if (using_backup())
  1011. // ...then return adjusted which_:
  1012. return -(which_ + 1);
  1013. // Otherwise, return which_ directly:
  1014. return which_;
  1015. }
  1016. private: // helpers, for structors (below)
  1017. struct initializer
  1018. : BOOST_VARIANT_AUX_INITIALIZER_T(
  1019. recursive_enabled_types, recursive_enabled_T
  1020. )
  1021. {
  1022. };
  1023. void destroy_content() BOOST_NOEXCEPT
  1024. {
  1025. detail::variant::destroyer visitor;
  1026. this->internal_apply_visitor(visitor);
  1027. }
  1028. public: // structors
  1029. ~variant() BOOST_NOEXCEPT
  1030. {
  1031. destroy_content();
  1032. }
  1033. variant()
  1034. #if !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
  1035. BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor<internal_T0>::value)
  1036. #endif
  1037. {
  1038. #ifdef _MSC_VER
  1039. #pragma warning( push )
  1040. // behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
  1041. #pragma warning( disable : 4345 )
  1042. #endif
  1043. // NOTE TO USER :
  1044. // Compile error from here indicates that the first bound
  1045. // type is not default-constructible, and so variant cannot
  1046. // support its own default-construction.
  1047. //
  1048. new( storage_.address() ) internal_T0();
  1049. indicate_which(0); // zero is the index of the first bounded type
  1050. #ifdef _MSC_VER
  1051. #pragma warning( pop )
  1052. #endif
  1053. }
  1054. private: // helpers, for structors, cont. (below)
  1055. class convert_copy_into
  1056. : public static_visitor<int>
  1057. {
  1058. private: // representation
  1059. void* storage_;
  1060. public: // structors
  1061. explicit convert_copy_into(void* storage) BOOST_NOEXCEPT
  1062. : storage_(storage)
  1063. {
  1064. }
  1065. public: // internal visitor interfaces (below)
  1066. template <typename T>
  1067. int internal_visit(T& operand, int) const
  1068. {
  1069. // NOTE TO USER :
  1070. // Compile error here indicates one of the source variant's types
  1071. // cannot be unambiguously converted to the destination variant's
  1072. // types (or that no conversion exists).
  1073. //
  1074. return initializer::initialize(storage_, operand);
  1075. }
  1076. # if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0564))
  1077. template <typename T>
  1078. result_type internal_visit(const T& operand, int) const
  1079. {
  1080. return initializer::initialize(storage_, operand);
  1081. }
  1082. # endif
  1083. template <typename T>
  1084. int internal_visit(boost::detail::reference_content<T>& operand, long) const
  1085. {
  1086. return internal_visit( operand.get(), 1L );
  1087. }
  1088. template <typename T>
  1089. int internal_visit(const boost::detail::reference_content<T>& operand, long) const
  1090. {
  1091. return internal_visit( operand.get(), 1L );
  1092. }
  1093. template <typename T>
  1094. int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  1095. {
  1096. return internal_visit( operand.get(), 1L );
  1097. }
  1098. template <typename T>
  1099. int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  1100. {
  1101. return internal_visit( operand.get(), 1L );
  1102. }
  1103. template <typename T>
  1104. int internal_visit(boost::recursive_wrapper<T>& operand, long) const
  1105. {
  1106. return internal_visit( operand.get(), 1L );
  1107. }
  1108. template <typename T>
  1109. int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
  1110. {
  1111. return internal_visit( operand.get(), 1L );
  1112. }
  1113. };
  1114. friend class convert_copy_into;
  1115. class convert_move_into
  1116. : public static_visitor<int>
  1117. {
  1118. private: // representation
  1119. void* storage_;
  1120. public: // structors
  1121. explicit convert_move_into(void* storage) BOOST_NOEXCEPT
  1122. : storage_(storage)
  1123. {
  1124. }
  1125. public: // internal visitor interfaces (below)
  1126. template <typename T>
  1127. int internal_visit(T& operand, int) const
  1128. {
  1129. // NOTE TO USER :
  1130. // Compile error here indicates one of the source variant's types
  1131. // cannot be unambiguously converted to the destination variant's
  1132. // types (or that no conversion exists).
  1133. //
  1134. return initializer::initialize(storage_, detail::variant::move(operand) );
  1135. }
  1136. template <typename T>
  1137. int internal_visit(boost::detail::reference_content<T>& operand, long) const
  1138. {
  1139. return internal_visit( operand.get(), 1L );
  1140. }
  1141. template <typename T>
  1142. int internal_visit(const boost::detail::reference_content<T>& operand, long) const
  1143. {
  1144. return internal_visit( operand.get(), 1L );
  1145. }
  1146. template <typename T>
  1147. int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  1148. {
  1149. return internal_visit( operand.get(), 1L );
  1150. }
  1151. template <typename T>
  1152. int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  1153. {
  1154. return internal_visit( operand.get(), 1L );
  1155. }
  1156. template <typename T>
  1157. int internal_visit(boost::recursive_wrapper<T>& operand, long) const
  1158. {
  1159. return internal_visit( operand.get(), 1L );
  1160. }
  1161. template <typename T>
  1162. int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
  1163. {
  1164. return internal_visit( operand.get(), 1L );
  1165. }
  1166. };
  1167. friend class convert_move_into;
  1168. private: // helpers, for structors, below
  1169. template <typename T>
  1170. void convert_construct(
  1171. T& operand
  1172. , int
  1173. , mpl::false_ = mpl::false_() // is_foreign_variant
  1174. )
  1175. {
  1176. // NOTE TO USER :
  1177. // Compile error here indicates that the given type is not
  1178. // unambiguously convertible to one of the variant's types
  1179. // (or that no conversion exists).
  1180. //
  1181. indicate_which(
  1182. initializer::initialize(
  1183. storage_.address()
  1184. , operand
  1185. )
  1186. );
  1187. }
  1188. template <typename T>
  1189. typename boost::enable_if<boost::is_rvalue_reference<T&&> >::type convert_construct(
  1190. T&& operand
  1191. , int
  1192. , mpl::false_ = mpl::false_() // is_foreign_variant
  1193. )
  1194. {
  1195. // NOTE TO USER :
  1196. // Compile error here indicates that the given type is not
  1197. // unambiguously convertible to one of the variant's types
  1198. // (or that no conversion exists).
  1199. //
  1200. indicate_which(
  1201. initializer::initialize(
  1202. storage_.address()
  1203. , detail::variant::move(operand)
  1204. )
  1205. );
  1206. }
  1207. template <typename Variant>
  1208. void convert_construct(
  1209. Variant& operand
  1210. , long
  1211. , mpl::true_// is_foreign_variant
  1212. )
  1213. {
  1214. convert_copy_into visitor(storage_.address());
  1215. indicate_which(
  1216. operand.internal_apply_visitor(visitor)
  1217. );
  1218. }
  1219. template <typename Variant>
  1220. typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct(
  1221. Variant&& operand
  1222. , long
  1223. , mpl::true_// is_foreign_variant
  1224. )
  1225. {
  1226. convert_move_into visitor(storage_.address());
  1227. indicate_which(
  1228. operand.internal_apply_visitor(visitor)
  1229. );
  1230. }
  1231. template <typename Variant>
  1232. void convert_construct_variant(Variant& operand)
  1233. {
  1234. // [Determine if the given variant is itself a bounded type, or if its
  1235. // content needs to be converted (i.e., it is a 'foreign' variant):]
  1236. //
  1237. typedef typename mpl::find_if<
  1238. types
  1239. , is_same<
  1240. add_const<mpl::_1>
  1241. , const Variant
  1242. >
  1243. >::type found_it;
  1244. typedef typename mpl::end<types>::type not_found;
  1245. typedef typename is_same<
  1246. found_it, not_found
  1247. >::type is_foreign_variant;
  1248. // Convert construct from operand:
  1249. convert_construct(
  1250. operand, 1L
  1251. , is_foreign_variant()
  1252. );
  1253. }
  1254. template <typename Variant>
  1255. typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct_variant(Variant&& operand)
  1256. {
  1257. // [Determine if the given variant is itself a bounded type, or if its
  1258. // content needs to be converted (i.e., it is a 'foreign' variant):]
  1259. //
  1260. typedef typename mpl::find_if<
  1261. types
  1262. , is_same<
  1263. add_const<mpl::_1>
  1264. , const Variant
  1265. >
  1266. >::type found_it;
  1267. typedef typename mpl::end<types>::type not_found;
  1268. typedef typename is_same<
  1269. found_it, not_found
  1270. >::type is_foreign_variant;
  1271. // Convert move construct from operand:
  1272. convert_construct(
  1273. detail::variant::move(operand), 1L
  1274. , is_foreign_variant()
  1275. );
  1276. }
  1277. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1278. typename boost::enable_if<mpl::or_<
  1279. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1280. boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
  1281. > >::type convert_construct(
  1282. boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
  1283. , long
  1284. )
  1285. {
  1286. convert_construct_variant(operand);
  1287. }
  1288. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1289. typename boost::enable_if<mpl::or_<
  1290. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1291. boost::detail::variant::is_variant_constructible_from<const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
  1292. > >::type convert_construct(
  1293. const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
  1294. , long
  1295. )
  1296. {
  1297. convert_construct_variant(operand);
  1298. }
  1299. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1300. typename boost::enable_if<mpl::or_<
  1301. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1302. boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&&, internal_types>
  1303. > >::type convert_construct(
  1304. boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&& operand
  1305. , long
  1306. )
  1307. {
  1308. convert_construct_variant( detail::variant::move(operand) );
  1309. }
  1310. public: // structors, cont.
  1311. template <typename T>
  1312. variant(const T& operand,
  1313. typename boost::enable_if<mpl::or_<
  1314. mpl::and_<
  1315. mpl::not_< boost::is_same<T, variant> >,
  1316. boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
  1317. >,
  1318. boost::is_same<T, boost::recursive_variant_> >,
  1319. bool >::type = true)
  1320. {
  1321. convert_construct(operand, 1L);
  1322. }
  1323. template <typename T>
  1324. variant(
  1325. T& operand
  1326. , typename boost::enable_if<mpl::or_<
  1327. mpl::and_<
  1328. mpl::not_< is_const<T> >,
  1329. mpl::not_< boost::is_same<T, variant> >,
  1330. boost::detail::variant::is_variant_constructible_from<T&, internal_types>
  1331. >,
  1332. boost::is_same<T, boost::recursive_variant_> >,
  1333. bool >::type = true
  1334. )
  1335. {
  1336. convert_construct(operand, 1L);
  1337. }
  1338. template <class T>
  1339. variant(T&& operand,
  1340. typename boost::enable_if<mpl::or_<
  1341. mpl::and_<
  1342. boost::is_rvalue_reference<T&&>,
  1343. mpl::not_< boost::is_const<T> >,
  1344. mpl::not_< boost::is_same<T, variant> >,
  1345. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1346. >,
  1347. boost::is_same<T, boost::recursive_variant_> >,
  1348. bool >::type = true)
  1349. {
  1350. convert_construct( detail::variant::move(operand), 1L);
  1351. }
  1352. public: // structors, cont.
  1353. // [MSVC6 requires copy constructor appear after template constructors]
  1354. variant(const variant& operand)
  1355. {
  1356. // Copy the value of operand into *this...
  1357. detail::variant::copy_into visitor( storage_.address() );
  1358. operand.internal_apply_visitor(visitor);
  1359. // ...and activate the *this's primary storage on success:
  1360. indicate_which(operand.which());
  1361. }
  1362. variant(variant&& operand) BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value)
  1363. {
  1364. // Move the value of operand into *this...
  1365. detail::variant::move_into visitor( storage_.address() );
  1366. operand.internal_apply_visitor(visitor);
  1367. // ...and activate the *this's primary storage on success:
  1368. indicate_which(operand.which());
  1369. }
  1370. private: // helpers, for modifiers (below)
  1371. template <typename Variant>
  1372. friend class detail::variant::backup_assigner;
  1373. // class assigner
  1374. //
  1375. // Internal visitor that "assigns" the visited value to the given variant
  1376. // by appropriate destruction and copy-construction.
  1377. //
  1378. class assigner
  1379. : public static_visitor<>
  1380. {
  1381. protected: // representation
  1382. variant& lhs_;
  1383. const int rhs_which_;
  1384. public: // structors
  1385. assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1386. : lhs_(lhs)
  1387. , rhs_which_(rhs_which)
  1388. {
  1389. }
  1390. protected: // helpers, for internal visitor interface (below)
  1391. template <typename RhsT, typename B1, typename B2>
  1392. void assign_impl(
  1393. const RhsT& rhs_content
  1394. , mpl::true_ // has_nothrow_copy
  1395. , B1 // is_nothrow_move_constructible
  1396. , B2 // has_fallback_type
  1397. ) const BOOST_NOEXCEPT
  1398. {
  1399. // Destroy lhs's content...
  1400. lhs_.destroy_content(); // nothrow
  1401. // ...copy rhs content into lhs's storage...
  1402. new(lhs_.storage_.address())
  1403. RhsT( rhs_content ); // nothrow
  1404. // ...and indicate new content type:
  1405. lhs_.indicate_which(rhs_which_); // nothrow
  1406. }
  1407. template <typename RhsT, typename B>
  1408. void assign_impl(
  1409. const RhsT& rhs_content
  1410. , mpl::false_ // has_nothrow_copy
  1411. , mpl::true_ // is_nothrow_move_constructible
  1412. , B // has_fallback_type
  1413. ) const
  1414. {
  1415. // Attempt to make a temporary copy (so as to move it below)...
  1416. RhsT temp(rhs_content);
  1417. // ...and upon success destroy lhs's content...
  1418. lhs_.destroy_content(); // nothrow
  1419. // ...move the temporary copy into lhs's storage...
  1420. new(lhs_.storage_.address())
  1421. RhsT( detail::variant::move(temp) ); // nothrow
  1422. // ...and indicate new content type:
  1423. lhs_.indicate_which(rhs_which_); // nothrow
  1424. }
  1425. void construct_fallback() const BOOST_NOEXCEPT {
  1426. // In case of failure, default-construct fallback type in lhs's storage...
  1427. new (lhs_.storage_.address())
  1428. fallback_type_; // nothrow
  1429. // ...indicate construction of fallback type...
  1430. lhs_.indicate_which(
  1431. BOOST_MPL_AUX_VALUE_WKND(fallback_type_index_)::value
  1432. ); // nothrow
  1433. }
  1434. template <typename RhsT>
  1435. void assign_impl(
  1436. const RhsT& rhs_content
  1437. , mpl::false_ // has_nothrow_copy
  1438. , mpl::false_ // is_nothrow_move_constructible
  1439. , mpl::true_ // has_fallback_type
  1440. ) const
  1441. {
  1442. // Destroy lhs's content...
  1443. lhs_.destroy_content(); // nothrow
  1444. BOOST_TRY
  1445. {
  1446. // ...and attempt to copy rhs's content into lhs's storage:
  1447. new(lhs_.storage_.address())
  1448. RhsT( rhs_content );
  1449. }
  1450. BOOST_CATCH (...)
  1451. {
  1452. construct_fallback();
  1453. // ...and rethrow:
  1454. BOOST_RETHROW;
  1455. }
  1456. BOOST_CATCH_END
  1457. // In the event of success, indicate new content type:
  1458. lhs_.indicate_which(rhs_which_); // nothrow
  1459. }
  1460. template <typename RhsT>
  1461. void assign_impl(
  1462. const RhsT& rhs_content
  1463. , mpl::false_ // has_nothrow_copy
  1464. , mpl::false_ // is_nothrow_move_constructible
  1465. , mpl::false_ // has_fallback_type
  1466. ) const
  1467. {
  1468. detail::variant::backup_assigner<wknd_self_t>
  1469. visitor(lhs_, rhs_which_, rhs_content);
  1470. lhs_.internal_apply_visitor(visitor);
  1471. }
  1472. public: // internal visitor interfaces
  1473. template <typename RhsT>
  1474. void internal_visit(const RhsT& rhs_content, int) const
  1475. {
  1476. typedef typename has_nothrow_copy<RhsT>::type
  1477. nothrow_copy;
  1478. typedef typename mpl::or_< // reduces compile-time
  1479. nothrow_copy
  1480. , is_nothrow_move_constructible<RhsT>
  1481. >::type nothrow_move_constructor;
  1482. assign_impl(
  1483. rhs_content
  1484. , nothrow_copy()
  1485. , nothrow_move_constructor()
  1486. , has_fallback_type_()
  1487. );
  1488. }
  1489. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1490. private:
  1491. // silence MSVC warning C4512: assignment operator could not be generated
  1492. assigner& operator= (assigner const&);
  1493. #endif
  1494. };
  1495. friend class assigner;
  1496. // class move_assigner
  1497. //
  1498. // Internal visitor that "move assigns" the visited value to the given variant
  1499. // by appropriate destruction and move-construction.
  1500. //
  1501. class move_assigner
  1502. : public assigner
  1503. {
  1504. public: // structors
  1505. move_assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1506. : assigner(lhs, rhs_which)
  1507. {
  1508. }
  1509. private: // helpers, for internal visitor interface (below)
  1510. template <typename RhsT, typename B2>
  1511. void assign_impl(
  1512. RhsT& rhs_content
  1513. , mpl::true_ // has_nothrow_copy
  1514. , mpl::false_ // is_nothrow_move_constructible
  1515. , B2 // has_fallback_type
  1516. ) const BOOST_NOEXCEPT
  1517. {
  1518. assigner::assign_impl(rhs_content, mpl::true_(), mpl::false_(), B2());
  1519. }
  1520. template <typename RhsT, typename B, typename B2>
  1521. void assign_impl(
  1522. RhsT& rhs_content
  1523. , B // has_nothrow_copy
  1524. , mpl::true_ // is_nothrow_move_constructible
  1525. , B2 // has_fallback_type
  1526. ) const BOOST_NOEXCEPT
  1527. {
  1528. // ...destroy lhs's content...
  1529. assigner::lhs_.destroy_content(); // nothrow
  1530. // ...move the rhs_content into lhs's storage...
  1531. new(assigner::lhs_.storage_.address())
  1532. RhsT( detail::variant::move(rhs_content) ); // nothrow
  1533. // ...and indicate new content type:
  1534. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1535. }
  1536. template <typename RhsT>
  1537. void assign_impl(
  1538. RhsT& rhs_content
  1539. , mpl::false_ // has_nothrow_copy
  1540. , mpl::false_ // is_nothrow_move_constructible
  1541. , mpl::true_ // has_fallback_type
  1542. ) const
  1543. {
  1544. // Destroy lhs's content...
  1545. assigner::lhs_.destroy_content(); // nothrow
  1546. BOOST_TRY
  1547. {
  1548. // ...and attempt to copy rhs's content into lhs's storage:
  1549. new(assigner::lhs_.storage_.address())
  1550. RhsT( detail::variant::move(rhs_content) );
  1551. }
  1552. BOOST_CATCH (...)
  1553. {
  1554. assigner::construct_fallback();
  1555. // ...and rethrow:
  1556. BOOST_RETHROW;
  1557. }
  1558. BOOST_CATCH_END
  1559. // In the event of success, indicate new content type:
  1560. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1561. }
  1562. template <typename RhsT>
  1563. void assign_impl(
  1564. RhsT& rhs_content
  1565. , mpl::false_ // has_nothrow_copy
  1566. , mpl::false_ // is_nothrow_move_constructible
  1567. , mpl::false_ // has_fallback_type
  1568. ) const
  1569. {
  1570. assigner::assign_impl(rhs_content, mpl::false_(), mpl::false_(), mpl::false_());
  1571. }
  1572. public: // internal visitor interfaces
  1573. template <typename RhsT>
  1574. void internal_visit(RhsT& rhs_content, int) const
  1575. {
  1576. typedef typename is_nothrow_move_constructible<RhsT>::type
  1577. nothrow_move_constructor;
  1578. typedef typename mpl::or_< // reduces compile-time
  1579. nothrow_move_constructor
  1580. , has_nothrow_copy<RhsT>
  1581. >::type nothrow_copy;
  1582. assign_impl(
  1583. rhs_content
  1584. , nothrow_copy()
  1585. , nothrow_move_constructor()
  1586. , has_fallback_type_()
  1587. );
  1588. }
  1589. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1590. private:
  1591. // silence MSVC warning C4512: assignment operator could not be generated
  1592. move_assigner& operator= (move_assigner const&);
  1593. #endif
  1594. };
  1595. friend class move_assigner;
  1596. void variant_assign(const variant& rhs)
  1597. {
  1598. // If the contained types are EXACTLY the same...
  1599. if (which_ == rhs.which_)
  1600. {
  1601. // ...then assign rhs's storage to lhs's content:
  1602. detail::variant::assign_storage visitor(rhs.storage_.address());
  1603. this->internal_apply_visitor(visitor);
  1604. }
  1605. else
  1606. {
  1607. // Otherwise, perform general (copy-based) variant assignment:
  1608. assigner visitor(*this, rhs.which());
  1609. rhs.internal_apply_visitor(visitor);
  1610. }
  1611. }
  1612. void variant_assign(variant&& rhs)
  1613. {
  1614. // If the contained types are EXACTLY the same...
  1615. if (which_ == rhs.which_)
  1616. {
  1617. // ...then move rhs's storage to lhs's content:
  1618. detail::variant::move_storage visitor(rhs.storage_.address());
  1619. this->internal_apply_visitor(visitor);
  1620. }
  1621. else
  1622. {
  1623. // Otherwise, perform general (move-based) variant assignment:
  1624. move_assigner visitor(*this, rhs.which());
  1625. rhs.internal_apply_visitor(visitor);
  1626. }
  1627. }
  1628. private: // helpers, for modifiers (below)
  1629. template <typename T>
  1630. void assign(const T& rhs)
  1631. {
  1632. // If direct T-to-T assignment is not possible...
  1633. detail::variant::direct_assigner<T> direct_assign(rhs);
  1634. if (this->apply_visitor(direct_assign) == false)
  1635. {
  1636. // ...then convert rhs to variant and assign:
  1637. //
  1638. // While potentially inefficient, the following construction of a
  1639. // variant allows T as any type convertible to one of the bounded
  1640. // types without excessive code redundancy.
  1641. //
  1642. variant temp(rhs);
  1643. variant_assign( detail::variant::move(temp) );
  1644. }
  1645. }
  1646. template <typename T>
  1647. void move_assign(T&& rhs)
  1648. {
  1649. // If direct T-to-T move assignment is not possible...
  1650. detail::variant::direct_mover<T> direct_move(rhs);
  1651. if (this->apply_visitor(direct_move) == false)
  1652. {
  1653. // ...then convert rhs to variant and assign:
  1654. //
  1655. // While potentially inefficient, the following construction of a
  1656. // variant allows T as any type convertible to one of the bounded
  1657. // types without excessive code redundancy.
  1658. //
  1659. variant temp( detail::variant::move(rhs) );
  1660. variant_assign( detail::variant::move(temp) );
  1661. }
  1662. }
  1663. public: // modifiers
  1664. #if !BOOST_WORKAROUND(BOOST_CLANG_VERSION, BOOST_TESTED_AT(150000)) || BOOST_CXX_VERSION <= 202002L
  1665. template <class T>
  1666. typename boost::enable_if<
  1667. boost::mpl::and_<
  1668. boost::is_rvalue_reference<T&&>,
  1669. mpl::not_< boost::is_const<T> >,
  1670. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1671. >,
  1672. variant&
  1673. >::type operator=(T&& rhs)
  1674. {
  1675. move_assign( detail::variant::move(rhs) );
  1676. return *this;
  1677. }
  1678. #endif
  1679. template <typename T>
  1680. typename boost::enable_if<
  1681. mpl::or_<
  1682. boost::is_same<T, variant>,
  1683. boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
  1684. >,
  1685. variant&
  1686. >::type operator=(const T& rhs)
  1687. {
  1688. assign(rhs);
  1689. return *this;
  1690. }
  1691. // [MSVC6 requires copy assign appear after templated operator=]
  1692. variant& operator=(const variant& rhs)
  1693. {
  1694. variant_assign(rhs);
  1695. return *this;
  1696. }
  1697. variant& operator=(variant&& rhs)
  1698. #if !defined(__GNUC__) || (__GNUC__ != 4) || (__GNUC_MINOR__ > 6) || defined(__clang__)
  1699. BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value && variant_move_noexcept_assignable::type::value)
  1700. #endif
  1701. {
  1702. variant_assign( detail::variant::move(rhs) );
  1703. return *this;
  1704. }
  1705. void swap(variant& rhs)
  1706. {
  1707. // If the contained types are the same...
  1708. if (which() == rhs.which())
  1709. {
  1710. // ...then swap the values directly:
  1711. detail::variant::swap_with<variant> visitor(rhs);
  1712. this->apply_visitor(visitor);
  1713. }
  1714. else
  1715. {
  1716. // ...otherwise, perform general variant swap:
  1717. variant tmp( detail::variant::move(rhs) );
  1718. rhs = detail::variant::move(*this);
  1719. *this = detail::variant::move(tmp);
  1720. }
  1721. }
  1722. public: // queries
  1723. //
  1724. // NOTE: member which() defined above.
  1725. //
  1726. bool empty() const BOOST_NOEXCEPT
  1727. {
  1728. return false;
  1729. }
  1730. const boost::typeindex::type_info& type() const
  1731. {
  1732. detail::variant::reflect visitor;
  1733. return this->apply_visitor(visitor);
  1734. }
  1735. public: // prevent comparison with foreign types
  1736. template <typename U>
  1737. void operator==(const U&) const
  1738. {
  1739. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1740. }
  1741. template <typename U>
  1742. void operator<(const U&) const
  1743. {
  1744. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1745. }
  1746. template <typename U>
  1747. void operator!=(const U&) const
  1748. {
  1749. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1750. }
  1751. template <typename U>
  1752. void operator>(const U&) const
  1753. {
  1754. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1755. }
  1756. template <typename U>
  1757. void operator<=(const U&) const
  1758. {
  1759. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1760. }
  1761. template <typename U>
  1762. void operator>=(const U&) const
  1763. {
  1764. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1765. }
  1766. public: // comparison operators
  1767. // [MSVC6 requires these operators appear after template operators]
  1768. bool operator==(const variant& rhs) const
  1769. {
  1770. if (this->which() != rhs.which())
  1771. return false;
  1772. detail::variant::comparer<
  1773. variant, detail::variant::equal_comp
  1774. > visitor(*this);
  1775. return rhs.apply_visitor(visitor);
  1776. }
  1777. bool operator<(const variant& rhs) const
  1778. {
  1779. //
  1780. // Dirk Schreib suggested this collating order.
  1781. //
  1782. if (this->which() != rhs.which())
  1783. return this->which() < rhs.which();
  1784. detail::variant::comparer<
  1785. variant, detail::variant::less_comp
  1786. > visitor(*this);
  1787. return rhs.apply_visitor(visitor);
  1788. }
  1789. ///////////////////////////////////////////////////////////////////////////////
  1790. // comparison operators != > <= >=
  1791. inline bool operator!=(const variant& rhs) const
  1792. {
  1793. return !(*this == rhs);
  1794. }
  1795. inline bool operator>(const variant& rhs) const
  1796. {
  1797. return rhs < *this;
  1798. }
  1799. inline bool operator<=(const variant& rhs) const
  1800. {
  1801. return !(*this > rhs);
  1802. }
  1803. inline bool operator>=(const variant& rhs) const
  1804. {
  1805. return !(*this < rhs);
  1806. }
  1807. // helpers, for visitation support (below) -- private when possible
  1808. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1809. template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
  1810. friend class variant;
  1811. private:
  1812. #else// defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1813. public:
  1814. #endif// !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1815. template <typename Visitor, typename VoidPtrCV>
  1816. BOOST_FORCEINLINE static typename Visitor::result_type
  1817. internal_apply_visitor_impl(
  1818. int internal_which
  1819. , int logical_which
  1820. , Visitor& visitor
  1821. , VoidPtrCV storage
  1822. )
  1823. {
  1824. typedef mpl::int_<0> first_which;
  1825. typedef typename mpl::begin<internal_types>::type first_it;
  1826. typedef typename mpl::end<internal_types>::type last_it;
  1827. typedef detail::variant::visitation_impl_step<
  1828. first_it, last_it
  1829. > first_step;
  1830. return detail::variant::visitation_impl(
  1831. internal_which, logical_which
  1832. , visitor, storage, mpl::false_()
  1833. , never_uses_backup_flag()
  1834. , static_cast<first_which*>(0), static_cast<first_step*>(0)
  1835. );
  1836. }
  1837. template <typename Visitor>
  1838. BOOST_FORCEINLINE typename Visitor::result_type
  1839. internal_apply_visitor(Visitor& visitor)
  1840. {
  1841. return internal_apply_visitor_impl(
  1842. which_, which(), visitor, storage_.address()
  1843. );
  1844. }
  1845. template <typename Visitor>
  1846. BOOST_FORCEINLINE typename Visitor::result_type
  1847. internal_apply_visitor(Visitor& visitor) const
  1848. {
  1849. return internal_apply_visitor_impl(
  1850. which_, which(), visitor, storage_.address()
  1851. );
  1852. }
  1853. public: // visitation support
  1854. template <typename Visitor>
  1855. typename Visitor::result_type
  1856. apply_visitor(Visitor& visitor) &&
  1857. {
  1858. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1859. return this->internal_apply_visitor(invoker);
  1860. }
  1861. template <typename Visitor>
  1862. typename Visitor::result_type
  1863. apply_visitor(Visitor& visitor) const&&
  1864. {
  1865. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1866. return this->internal_apply_visitor(invoker);
  1867. }
  1868. template <typename Visitor>
  1869. typename Visitor::result_type
  1870. apply_visitor(Visitor& visitor) &
  1871. {
  1872. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1873. return this->internal_apply_visitor(invoker);
  1874. }
  1875. template <typename Visitor>
  1876. typename Visitor::result_type
  1877. apply_visitor(Visitor& visitor) const &
  1878. {
  1879. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1880. return this->internal_apply_visitor(invoker);
  1881. }
  1882. }; // class variant
  1883. ///////////////////////////////////////////////////////////////////////////////
  1884. // metafunction make_variant_over
  1885. //
  1886. // See docs and boost/variant/variant_fwd.hpp for more information.
  1887. //
  1888. template <typename Types>
  1889. struct make_variant_over
  1890. {
  1891. private: // precondition assertions
  1892. BOOST_STATIC_ASSERT(( ::boost::mpl::is_sequence<Types>::value ));
  1893. typedef typename boost::mpl::insert_range<
  1894. boost::mpl::list<>
  1895. , boost::mpl::end< boost::mpl::list<> >::type
  1896. , Types
  1897. >::type copied_sequence_t;
  1898. public: // metafunction result
  1899. typedef variant<
  1900. detail::variant::over_sequence<copied_sequence_t>
  1901. > type;
  1902. };
  1903. ///////////////////////////////////////////////////////////////////////////////
  1904. // function template swap
  1905. //
  1906. // Swaps two variants of the same type (i.e., identical specification).
  1907. //
  1908. template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
  1909. inline void swap(
  1910. variant< BOOST_VARIANT_ENUM_PARAMS(T) >& lhs
  1911. , variant< BOOST_VARIANT_ENUM_PARAMS(T) >& rhs
  1912. )
  1913. {
  1914. lhs.swap(rhs);
  1915. }
  1916. } // namespace boost
  1917. // implementation additions
  1918. #if !defined(BOOST_NO_IOSTREAM)
  1919. #include <boost/variant/detail/variant_io.hpp>
  1920. #endif // BOOST_NO_IOSTREAM
  1921. #endif // BOOST_VARIANT_VARIANT_HPP