variant.hpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  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-2025 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