class.boost.variant.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Class template variant</title><link rel="stylesheet" href="reference.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.61.3"><link rel="home" href="index.html" title="The Boost C++ Libraries"><link rel="up" href="variant.reference.html#header.boost.variant.variant.hpp" title="Header &lt;boost/variant/variant.hpp&gt;"><link rel="previous" href="BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT.html" title="Macro BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT"><link rel="next" href="id2518553-bb.html" title="Function template swap"><link rel="chapter" href="libraries.html" title="The Boost C++ Libraries"><link rel="chapter" href="any.html" title="Boost.Any"><link rel="refentry" href="class.boost.bad_any_cast.html" title="Class bad_any_cast"><link rel="refentry" href="class.boost.any.html" title="Class any"><link rel="refentry" href="id2383300-bb.html" title="Function any_cast"><link rel="chapter" href="array.html" title="Boost.Array"><link rel="refentry" href="class.boost.array.html" title="Class template array"><link rel="chapter" href="function.html" title="Boost.Function"><link rel="refentry" href="class.boost.bad_function_call.html" title="Class bad_function_call"><link rel="refentry" href="class.boost.function_base.html" title="Class function_base"><link rel="refentry" href="class.boost.functionN.html" title="Class template functionN"><link rel="refentry" href="class.boost.function.html" title="Class template function"><link rel="chapter" href="ref.html" title="Boost.Ref"><link rel="refentry" href="class.boost.reference_wrapper.html" title="Class template reference_wrapper"><link rel="refentry" href="class.boost.is_reference_wrapper.html" title="Class template is_reference_wrapper"><link rel="refentry" href="class.boost.unwrap_reference.html" title="Class template unwrap_reference"><link rel="chapter" href="signals.html" title="Boost.Signals"><link rel="refentry" href="class.boost.signalN.html" title="Class template signalN"><link rel="refentry" href="class.boost.signal.html" title="Class template signal"><link rel="refentry" href="class.boost.slot.html" title="Class template slot"><link rel="refentry" href="class.boost.signals.trackable.html" title="Class trackable"><link rel="refentry" href="class.boost.signals.connection.html" title="Class connection"><link rel="refentry" href="class.boost.signals.scoped_connection.html" title="Class scoped_connection"><link rel="refentry" href="id2532119-bb.html" title="Function template visit_each"><link rel="refentry" href="class.boost.last_value.html" title="Class template last_value"><link rel="refentry" href="id2354683-bb.html" title="Class last_value&lt;void&gt;"><link rel="chapter" href="variant.html" title="Boost.Variant"><link rel="refentry" href="BOOST_VARIANT_LIMIT_TYPES.html" title="Macro BOOST_VARIANT_LIMIT_TYPES"><link rel="refentry" href="BOOST_VARIANT_ENUM_PARAMS.html" title="Macro BOOST_VARIANT_ENUM_PARAMS"><link rel="refentry" href="BOOST_VARIANT_ENUM_SHIFTED_PARAMS.html" title="Macro BOOST_VARIANT_ENUM_SHIFTED_PARAMS"><link rel="refentry" href="BOOST_VARIANT_NO_REFERENCE_SUPPORT.html" title="Macro BOOST_VARIANT_NO_REFERENCE_SUPPORT"><link rel="refentry" href="BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT.html" title="Macro BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT"><link rel="refentry" href="BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT.html" title="Macro BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT"><link rel="refentry" href="class.boost.variant.html" title="Class template variant"><link rel="refentry" href="id2518553-bb.html" title="Function template swap"><link rel="refentry" href="id2524211-bb.html" title="Function template operator&lt;&lt;"><link rel="refentry" href="class.boost.make_variant_over.html" title="Class template make_variant_over"><link rel="refentry" href="class.boost.make_recursive_variant.html" title="Class template make_recursive_variant"><link rel="refentry" href="class.boost.make_recursive_variant_over.html" title="Class template make_recursive_variant_over"><link rel="refentry" href="class.boost.recursive_wrapper.html" title="Class template recursive_wrapper"><link rel="refentry" href="class.boost.is_recursive_wrapper.html" title="Class template is_recursive_wrapper"><link rel="refentry" href="class.boost.unwrap_recursive_wrapper.html" title="Class template unwrap_recursive_wrapper"><link rel="refentry" href="class.boost.apply_visitor_delayed_t.html" title="Class template apply_visitor_delayed_t"><link rel="refentry" href="id2557051-bb.html" title="Function apply_visitor"><link rel="refentry" href="class.boost.bad_get.html" title="Class bad_get"><link rel="refentry" href="id2493238-bb.html" title="Function get"><link rel="refentry" href="class.boost.bad_visit.html" title="Class bad_visit"><link rel="refentry" href="class.boost.static_visitor.html" title="Class template static_visitor"><link rel="refentry" href="class.boost.visitor_ptr_t.html" title="Class template visitor_ptr_t"><link rel="refentry" href="id2544445-bb.html" title="Function template visitor_ptr"><link rel="chapter" href="boostbook.html" title="The BoostBook Documentation Format"><link rel="refentry" href="boostbook.dtd.class-specialization.html" title="
  2. BoostBook element class-specialization"><link rel="refentry" href="boostbook.dtd.link-test.html" title="
  3. BoostBook element link-test"><link rel="refentry" href="boostbook.dtd.link-fail-test.html" title="
  4. BoostBook element link-fail-test"><link rel="refentry" href="boostbook.dtd.typedef.html" title="
  5. BoostBook element typedef"><link rel="refentry" href="boostbook.dtd.static-constant.html" title="
  6. BoostBook element static-constant"><link rel="refentry" href="boostbook.dtd.code.html" title="
  7. BoostBook element code"><link rel="refentry" href="boostbook.dtd.destructor.html" title="
  8. BoostBook element destructor"><link rel="refentry" href="boostbook.dtd.template-type-parameter.html" title="
  9. BoostBook element template-type-parameter"><link rel="refentry" href="boostbook.dtd.description.html" title="
  10. BoostBook element description"><link rel="refentry" href="boostbook.dtd.librarylist.html" title="
  11. BoostBook element librarylist"><link rel="refentry" href="boostbook.dtd.library-reference.html" title="
  12. BoostBook element library-reference"><link rel="refentry" href="boostbook.dtd.boostbook.html" title="
  13. BoostBook element boostbook"><link rel="refentry" href="boostbook.dtd.union.html" title="
  14. BoostBook element union"><link rel="refentry" href="boostbook.dtd.inherit.html" title="
  15. BoostBook element inherit"><link rel="refentry" href="boostbook.dtd.template-varargs.html" title="
  16. BoostBook element template-varargs"><link rel="refentry" href="boostbook.dtd.source.html" title="
  17. BoostBook element source"><link rel="refentry" href="boostbook.dtd.function.html" title="
  18. BoostBook element function"><link rel="refentry" href="boostbook.dtd.postconditions.html" title="
  19. BoostBook element postconditions"><link rel="refentry" href="boostbook.dtd.compile-test.html" title="
  20. BoostBook element compile-test"><link rel="refentry" href="boostbook.dtd.method.html" title="
  21. BoostBook element method"><link rel="refentry" href="boostbook.dtd.snippet.html" title="
  22. BoostBook element snippet"><link rel="refentry" href="boostbook.dtd.constructor.html" title="
  23. BoostBook element constructor"><link rel="refentry" href="boostbook.dtd.namespace.html" title="
  24. BoostBook element namespace"><link rel="refentry" href="boostbook.dtd.if-fails.html" title="
  25. BoostBook element if-fails"><link rel="refentry" href="boostbook.dtd.free-function-group.html" title="
  26. BoostBook element free-function-group"><link rel="refentry" href="boostbook.dtd.functionname.html" title="
  27. BoostBook element functionname"><link rel="refentry" href="boostbook.dtd.librarycategory.html" title="
  28. BoostBook element librarycategory"><link rel="refentry" href="boostbook.dtd.notes.html" title="
  29. BoostBook element notes"><link rel="refentry" href="boostbook.dtd.data-member.html" title="
  30. BoostBook element data-member"><link rel="refentry" href="boostbook.dtd.specialization.html" title="
  31. BoostBook element specialization"><link rel="refentry" href="boostbook.dtd.union-specialization.html" title="
  32. BoostBook element union-specialization"><link rel="refentry" href="boostbook.dtd.throws.html" title="
  33. BoostBook element throws"><link rel="refentry" href="boostbook.dtd.template-arg.html" title="
  34. BoostBook element template-arg"><link rel="refentry" href="boostbook.dtd.method-group.html" title="
  35. BoostBook element method-group"><link rel="refentry" href="boostbook.dtd.requirement.html" title="
  36. BoostBook element requirement"><link rel="refentry" href="boostbook.dtd.precondition.html" title="
  37. BoostBook element precondition"><link rel="refentry" href="boostbook.dtd.paramtype.html" title="
  38. BoostBook element paramtype"><link rel="refentry" href="boostbook.dtd.using-class.html" title="
  39. BoostBook element using-class"><link rel="refentry" href="boostbook.dtd.run-test.html" title="
  40. BoostBook element run-test"><link rel="refentry" href="boostbook.dtd.librarypurpose.html" title="
  41. BoostBook element librarypurpose"><link rel="refentry" href="boostbook.dtd.copy-assignment.html" title="
  42. BoostBook element copy-assignment"><link rel="refentry" href="boostbook.dtd.run-fail-test.html" title="
  43. BoostBook element run-fail-test"><link rel="refentry" href="boostbook.dtd.template.html" title="
  44. BoostBook element template"><link rel="refentry" href="boostbook.dtd.compile-fail-test.html" title="
  45. BoostBook element compile-fail-test"><link rel="refentry" href="boostbook.dtd.returns.html" title="
  46. BoostBook element returns"><link rel="refentry" href="boostbook.dtd.default.html" title="
  47. BoostBook element default"><link rel="refentry" href="boostbook.dtd.parameter.html" title="
  48. BoostBook element parameter"><link rel="refentry" href="boostbook.dtd.signature.html" title="
  49. BoostBook element signature"><link rel="refentry" href="boostbook.dtd.overloaded-function.html" title="
  50. BoostBook element overloaded-function"><link rel="refentry" href="boostbook.dtd.class.html" title="
  51. BoostBook element class"><link rel="refentry" href="boostbook.dtd.librarycategorydef.html" title="
  52. BoostBook element librarycategorydef"><link rel="refentry" href="boostbook.dtd.type.html" title="
  53. BoostBook element type"><link rel="refentry" href="boostbook.dtd.enumvalue.html" title="
  54. BoostBook element enumvalue"><link rel="refentry" href="boostbook.dtd.overloaded-method.html" title="
  55. BoostBook element overloaded-method"><link rel="refentry" href="boostbook.dtd.programlisting.html" title="
  56. BoostBook element programlisting"><link rel="refentry" href="boostbook.dtd.complexity.html" title="
  57. BoostBook element complexity"><link rel="refentry" href="boostbook.dtd.purpose.html" title="
  58. BoostBook element purpose"><link rel="refentry" href="boostbook.dtd.template-nontype-parameter.html" title="
  59. BoostBook element template-nontype-parameter"><link rel="refentry" href="boostbook.dtd.library.html" title="
  60. BoostBook element library"><link rel="refentry" href="boostbook.dtd.librarycategorylist.html" title="
  61. BoostBook element librarycategorylist"><link rel="refentry" href="boostbook.dtd.using-namespace.html" title="
  62. BoostBook element using-namespace"><link rel="refentry" href="boostbook.dtd.struct-specialization.html" title="
  63. BoostBook element struct-specialization"><link rel="refentry" href="boostbook.dtd.struct.html" title="
  64. BoostBook element struct"><link rel="refentry" href="boostbook.dtd.lib.html" title="
  65. BoostBook element lib"><link rel="refentry" href="boostbook.dtd.enum.html" title="
  66. BoostBook element enum"><link rel="refentry" href="boostbook.dtd.requires.html" title="
  67. BoostBook element requires"><link rel="refentry" href="boostbook.dtd.effects.html" title="
  68. BoostBook element effects"><link rel="refentry" href="boostbook.dtd.libraryname.html" title="
  69. BoostBook element libraryname"><link rel="refentry" href="boostbook.dtd.libraryinfo.html" title="
  70. BoostBook element libraryinfo"><link rel="refentry" href="boostbook.dtd.testsuite.html" title="
  71. BoostBook element testsuite"><link rel="refentry" href="boostbook.dtd.header.html" title="
  72. BoostBook element header"><link rel="refentry" href="boostbook.dtd.rationale.html" title="
  73. BoostBook element rationale"><link rel="refentry" href="Assignable.html" title="Concept Assignable"><link rel="refentry" href="InputIterator.html" title="Concept InputIterator"><link rel="refentry" href="OutputIterator.html" title="Concept OutputIterator"><link rel="refentry" href="ForwardIterator.html" title="Concept ForwardIterator"><link rel="refentry" href="BidirectionalIterator.html" title="Concept BidirectionalIterator"><link rel="refentry" href="RandomAccessIterator.html" title="Concept RandomAccessIterator"><link rel="refentry" href="DefaultConstructible.html" title="Concept DefaultConstructible"><link rel="refentry" href="CopyConstructible.html" title="Concept CopyConstructible"><link rel="refentry" href="EqualityComparable.html" title="Concept EqualityComparable"><link rel="refentry" href="LessThanComparable.html" title="Concept LessThanComparable"><link rel="refentry" href="SignedInteger.html" title="Concept SignedInteger"><link rel="section" href="class.boost.variant.html#id2531529" title="Description"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" border="1" cellpadding="2" bgcolor="#007F7F"><tr><td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td><td><a href="../../index.htm"><font color="#FFFFFF" size="4" face="Arial">Home</font></a></td><td><a href="libraries.html"><font color="#FFFFFF" size="4" face="Arial">Libraries</font></a></td><td><a href="../../people/people.htm"><font color="#FFFFFF" size="4" face="Arial">People</font></a></td><td><a href="../../more/faq.htm"><font color="#FFFFFF" size="4" face="Arial">FAQ</font></a></td><td><a href="../../more/index.htm"><font color="#FFFFFF" size="4" face="Arial">More</font></a></td></tr></table><div class="refentry" lang="en"><a name="class.boost.variant"></a><div class="titlepage"><div></div><div></div></div><div class="refnamediv"><h2><span class="refentrytitle">Class template variant</span></h2><p>boost::variant &#8212; Safe, generic, stack-based discriminated union container.</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T1, <span class="bold"><b>typename</b></span> T2 = <span class="emphasis"><em>unspecified</em></span>, ...,
  74.          <span class="bold"><b>typename</b></span> TN = <span class="emphasis"><em>unspecified</em></span>&gt;
  75. <span class="bold"><b>class</b></span> variant {
  76. <span class="bold"><b>public</b></span>:
  77.   <span class="emphasis"><em>// types</em></span>
  78.   <span class="bold"><b>typedef</b></span> <span class="emphasis"><em>unspecified</em></span> types;
  79.   <span class="emphasis"><em>// <a href="class.boost.variant.html#class.boost.variantconstruct-copy-destruct">construct/copy/destruct</a></em></span>
  80.   <a href="class.boost.variant.html#id2410771-bb">variant</a>();
  81.   <a href="class.boost.variant.html#id2458124-bb">variant</a>(<span class="bold"><b>const</b></span> variant &amp;);
  82.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <a href="class.boost.variant.html#id2375383-bb">variant</a>(T &amp;);
  83.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <a href="class.boost.variant.html#id2485087-bb">variant</a>(<span class="bold"><b>const</b></span> T &amp;);
  84.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U1, <span class="bold"><b>typename</b></span> U2, ..., <span class="bold"><b>typename</b></span> UN&gt;
  85.     <a href="class.boost.variant.html#id2516325-bb">variant</a>(variant&lt;U1, U2, ..., UN&gt; &amp;);
  86.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U1, <span class="bold"><b>typename</b></span> U2, ..., <span class="bold"><b>typename</b></span> UN&gt;
  87.     <a href="class.boost.variant.html#id2355967-bb">variant</a>(<span class="bold"><b>const</b></span> variant&lt;U1, U2, ..., UN&gt; &amp;);
  88.   <a href="class.boost.variant.html#id2489745-bb">~variant</a>();
  89.   <span class="emphasis"><em>// <a href="class.boost.variant.html#id2549864-bb">modifiers</a></em></span>
  90.   <span class="type"><span class="bold"><b>void</b></span></span> <a href="class.boost.variant.html#id2514140-bb">swap</a>(variant &amp;);
  91.   <span class="type">variant &amp;</span> <a href="class.boost.variant.html#id2509331-bb"><span class="bold"><b>operator</b></span>=</a>(<span class="bold"><b>const</b></span> variant &amp;);
  92.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <span class="type">variant &amp;</span> <a href="class.boost.variant.html#id2489594-bb"><span class="bold"><b>operator</b></span>=</a>(<span class="bold"><b>const</b></span> T &amp;);
  93.   <span class="emphasis"><em>// <a href="class.boost.variant.html#id2370110-bb">queries</a></em></span>
  94.   <span class="type"><span class="bold"><b>int</b></span></span> <a href="class.boost.variant.html#id2466395-bb">which</a>() <span class="bold"><b>const</b></span>;
  95.   <span class="type"><span class="bold"><b>bool</b></span></span> <a href="class.boost.variant.html#id2494720-bb">empty</a>() <span class="bold"><b>const</b></span>;
  96.   <span class="type"><span class="bold"><b>const</b></span> std::type_info &amp;</span> <a href="class.boost.variant.html#id2508948-bb">type</a>() <span class="bold"><b>const</b></span>;
  97.   <span class="emphasis"><em>// <a href="class.boost.variant.html#id2450166-bb">relational</a></em></span>
  98.   <span class="type"><span class="bold"><b>bool</b></span></span> <a href="class.boost.variant.html#id2492818-bb"><span class="bold"><b>operator</b></span>==</a>(<span class="bold"><b>const</b></span> variant &amp;) <span class="bold"><b>const</b></span>;
  99.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U&gt; <span class="type"><span class="bold"><b>void</b></span></span> <a href="class.boost.variant.html#id2495748-bb"><span class="bold"><b>operator</b></span>==</a>(<span class="bold"><b>const</b></span> U &amp;) <span class="bold"><b>const</b></span>;
  100.   <span class="type"><span class="bold"><b>bool</b></span></span> <a href="class.boost.variant.html#id2372301-bb"><span class="bold"><b>operator</b></span>&lt;</a>(<span class="bold"><b>const</b></span> variant &amp;) <span class="bold"><b>const</b></span>;
  101.   <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U&gt; <span class="type"><span class="bold"><b>void</b></span></span> <a href="class.boost.variant.html#id2433666-bb"><span class="bold"><b>operator</b></span>&lt;</a>(<span class="bold"><b>const</b></span> U &amp;) <span class="bold"><b>const</b></span>;
  102. };</pre></div><div class="refsect1" lang="en"><h2>Description</h2><p>The <tt class="computeroutput">variant</tt> class template (inspired by Andrei
  103. Alexandrescu's class of the same name
  104. [<a href="variant.refs.html#variant.refs.ale01a">Ale01A</a>]) is an efficient,
  105. <a href="variant.tutorial.html#variant.tutorial.recursive" title="Recursive variant types">recursive-capable</a>,
  106. bounded discriminated union value type capable of containing any value
  107. type (either POD or non-POD). It supports construction from any type
  108. convertible to one of its bounded types or from a source
  109. <tt class="computeroutput">variant</tt> whose bounded types are each convertible to one
  110. of the destination <tt class="computeroutput">variant</tt>'s bounded types. As well,
  111. through <tt class="computeroutput"><a href="id2557051-bb.html" title="Function apply_visitor">apply_visitor</a></tt>,
  112. <tt class="computeroutput">variant</tt> supports compile-time checked, type-safe
  113. visitation; and through <tt class="computeroutput"><a href="id2493238-bb.html" title="Function get">get</a></tt>,
  114. <tt class="computeroutput">variant</tt> supports run-time checked, type-safe value
  115. retrieval.</p><p><span class="bold"><b>Notes</b></span>:</p><div class="itemizedlist"><ul type="disc"><li>The bounded types of the <tt class="computeroutput">variant</tt> are exposed
  116. via the nested typedef <tt class="computeroutput">types</tt>, which is an
  117. <a href="../../libs/mpl/index.html" target="_top">MPL</a>-compatible Sequence containing the
  118. set of types that must be handled by any
  119. <a href="variant.reference.html#variant.concepts.static-visitor" title="StaticVisitor">visitor</a> to
  120. the <tt class="computeroutput">variant</tt>.</li><li>All members of <tt class="computeroutput">variant</tt> satisfy at least the
  121. basic guarantee of exception-safety. That is, all operations on
  122. a <tt class="computeroutput">variant</tt> remain defined even after previous
  123. operations have failed.</li><li>Each type specified as a template argument to
  124. <tt class="computeroutput">variant</tt> must meet the requirements of the
  125. <span class="emphasis"><em><a href="variant.reference.html#variant.concepts.bounded-type" title="BoundedType">BoundedType</a></em></span>
  126. concept.</li><li>Each type specified as a template argument to
  127. <tt class="computeroutput">variant</tt> must be distinct after removal of qualifiers.
  128. Thus, for instance, both <tt class="computeroutput">variant&lt;int, int&gt;</tt> and
  129. <tt class="computeroutput">variant&lt;int, const int&gt;</tt> have undefined
  130. behavior.</li><li>Conforming implementations of <tt class="computeroutput">variant</tt> must
  131. allow at least ten types as template arguments. The exact number
  132. of allowed arguments is exposed by the preprocessor macro
  133. <tt class="computeroutput"><a href="BOOST_VARIANT_LIMIT_TYPES.html" title="Macro BOOST_VARIANT_LIMIT_TYPES">BOOST_VARIANT_LIMIT_TYPES</a></tt>.
  134. (See <tt class="computeroutput"><a href="class.boost.make_variant_over.html" title="Class template make_variant_over">make_variant_over</a></tt> for a
  135. means to specify the bounded types of a <tt class="computeroutput">variant</tt> by
  136. the elements of an <a href="../../libs/mpl/index.html" target="_top">MPL</a> or compatible
  137. Sequence, thus overcoming this limitation.)</li></ul></div><div class="refsect2" lang="en"><h3><a name="class.boost.variantconstruct-copy-destruct"></a><tt class="computeroutput">variant</tt> construct/copy/destruct</h3><div class="orderedlist"><ol type="1"><li><pre class="literallayout"><a name="id2410771-bb"></a>variant();</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  138. The first bounded type of the <tt class="computeroutput">variant</tt> (i.e.,
  139. <tt class="computeroutput">T1</tt>) must fulfill the requirements of the
  140. <span class="emphasis"><em>DefaultConstructible</em></span> [20.1.4]
  141. concept.<br><b><span class="term">Postconditions</span></b>:
  142. Content of <tt class="computeroutput">*this</tt> is the default value of the
  143. first bounded type (i.e, <tt class="computeroutput">T1</tt>).<br><b><span class="term">Throws</span></b>:
  144. May fail with any exceptions arising from the default
  145. constructor of <tt class="computeroutput">T1</tt>.</p></li><li><pre class="literallayout"><a name="id2458124-bb"></a>variant(<span class="bold"><b>const</b></span> variant &amp; other);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Postconditions</span></b>:
  146. Content of <tt class="computeroutput">*this</tt> is a copy of the content of
  147. <tt class="computeroutput">other</tt>.<br><b><span class="term">Throws</span></b>:
  148. May fail with any exceptions arising from the
  149. copy constructor of <tt class="computeroutput">other</tt>'s contained type.</p></li><li><pre class="literallayout"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <a name="id2375383-bb"></a>variant(T &amp; operand);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  150. <tt class="computeroutput">T</tt> must be unambiguously convertible to one of
  151. the bounded types (i.e., <tt class="computeroutput">T1</tt>, <tt class="computeroutput">T2</tt>,
  152. etc.).<br><b><span class="term">Postconditions</span></b>:
  153. Content of <tt class="computeroutput">*this</tt> is the best conversion of
  154. <tt class="computeroutput">operand</tt> to one of the bounded types, as determined
  155. by standard overload resolution rules.<br><b><span class="term">Throws</span></b>:
  156. May fail with any exceptions arising from the conversion of
  157. <tt class="computeroutput">operand</tt> to one of the bounded types.</p></li><li><pre class="literallayout"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <a name="id2485087-bb"></a>variant(<span class="bold"><b>const</b></span> T &amp; operand);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Notes</span></b>:
  158. Same semantics as previous constructor, but allows
  159. construction from temporaries.</p></li><li><pre class="literallayout"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U1, <span class="bold"><b>typename</b></span> U2, ..., <span class="bold"><b>typename</b></span> UN&gt;
  160.   <a name="id2516325-bb"></a>variant(variant&lt;U1, U2, ..., UN&gt; &amp; operand);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  161. <span class="emphasis"><em>Every</em></span> one of <tt class="computeroutput">U1</tt>,
  162. <tt class="computeroutput">U2</tt>, ..., <tt class="computeroutput">UN</tt> must have an unambiguous
  163. conversion to one of the bounded types (i.e., <tt class="computeroutput">T1</tt>,
  164. <tt class="computeroutput">T2</tt>, ..., <tt class="computeroutput">TN</tt>).<br><b><span class="term">Postconditions</span></b>:
  165. If <tt class="computeroutput">variant&lt;U1, U2, ..., UN&gt;</tt> is itself
  166. one of the bounded types, then content of <tt class="computeroutput">*this</tt> is a
  167. copy of <tt class="computeroutput">operand</tt>. Otherwise, content of
  168. <tt class="computeroutput">*this</tt> is the best conversion of the content of
  169. <tt class="computeroutput">operand</tt> to one of the bounded types, as determined
  170. by standard overload resolution rules.<br><b><span class="term">Throws</span></b>:
  171. If <tt class="computeroutput">variant&lt;U1, U2, ..., UN&gt;</tt> is itself
  172. one of the bounded types, then may fail with any exceptions arising
  173. from the copy constructor of
  174. <tt class="computeroutput">variant&lt;U1, U2, ..., UN&gt;</tt>. Otherwise, may fail
  175. with any exceptions arising from the conversion of the content of
  176. <tt class="computeroutput">operand</tt> to one of the bounded types.</p></li><li><pre class="literallayout"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U1, <span class="bold"><b>typename</b></span> U2, ..., <span class="bold"><b>typename</b></span> UN&gt;
  177.   <a name="id2355967-bb"></a>variant(<span class="bold"><b>const</b></span> variant&lt;U1, U2, ..., UN&gt; &amp; operand);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Notes</span></b>:
  178. Same semantics as previous constructor, but allows
  179. construction from temporaries.</p></li><li><pre class="literallayout"><a name="id2489745-bb"></a>~variant();</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Effects</span></b>:
  180. Destroys the content of <tt class="computeroutput">*this</tt>.<br><b><span class="term">Throws</span></b>:
  181. Will not throw.</p></li></ol></div></div><div class="refsect2" lang="en"><h3><a name="id2549864-bb"></a><tt class="computeroutput">variant</tt> modifiers</h3><div class="orderedlist"><ol type="1"><li><pre class="literallayout"><span class="type"><span class="bold"><b>void</b></span></span> <a name="id2514140-bb"></a>swap(variant &amp; other);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  182. Every bounded type must fulfill the requirements of the
  183. <a href="Assignable.html" title="Concept Assignable">Assignable</a>
  184. concept.<br><b><span class="term">Effects</span></b>:
  185. Interchanges the content of <tt class="computeroutput">*this</tt> and
  186. <tt class="computeroutput">other</tt>.<br><b><span class="term">Throws</span></b>:
  187. If the contained type of <tt class="computeroutput">other</tt> is the same as
  188. the contained type of <tt class="computeroutput">*this</tt>, then may fail with any
  189. exceptions arising from the <tt class="computeroutput">swap</tt> of the contents of
  190. <tt class="computeroutput">*this</tt> and <tt class="computeroutput">other</tt>. Otherwise, may fail
  191. with any exceptions arising from either of the copy constructors
  192. of the contained types. Also, in the event of insufficient
  193. memory, may fail with <tt class="computeroutput">std::bad_alloc</tt>
  194. (<a href="variant.design.html#variant.design.never-empty.problem" title="The Implementation Problem">why?</a>).</p></li><li><pre class="literallayout"><span class="type">variant &amp;</span> <a name="id2509331-bb"></a><span class="bold"><b>operator</b></span>=(<span class="bold"><b>const</b></span> variant &amp; rhs);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  195. Every bounded type must fulfill the requirements of the
  196. <a href="Assignable.html" title="Concept Assignable">Assignable</a>
  197. concept.<br><b><span class="term">Effects</span></b>:
  198. If the contained type of <tt class="computeroutput">rhs</tt> is the same as
  199. the contained type of <tt class="computeroutput">*this</tt>, then assigns the
  200. content of <tt class="computeroutput">rhs</tt> into the content of
  201. <tt class="computeroutput">*this</tt>. Otherwise, makes the content of
  202. <tt class="computeroutput">*this</tt> a copy of the content of <tt class="computeroutput">rhs</tt>,
  203. destroying the previous content of <tt class="computeroutput">*this</tt>.<br><b><span class="term">Throws</span></b>:
  204. If the contained type of <tt class="computeroutput">rhs</tt> is the same as
  205. the contained type of <tt class="computeroutput">*this</tt>, then may fail with any
  206. exceptions arising from the assignment of the content of
  207. <tt class="computeroutput">rhs</tt> into the content <tt class="computeroutput">*this</tt>. Otherwise,
  208. may fail with any exceptions arising from the copy constructor
  209. of the contained type of <tt class="computeroutput">rhs</tt>. Also, in the event of
  210. insufficient memory, may fail with <tt class="computeroutput">std::bad_alloc</tt>
  211. (<a href="variant.design.html#variant.design.never-empty.problem" title="The Implementation Problem">why?</a>).</p></li><li><pre class="literallayout"><span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> T&gt; <span class="type">variant &amp;</span> <a name="id2489594-bb"></a><span class="bold"><b>operator</b></span>=(<span class="bold"><b>const</b></span> T &amp; rhs);</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Requires</span></b>:
  212. <div class="itemizedlist"><ul type="disc"><li><tt class="computeroutput">T</tt> must be unambiguously convertible to
  213. one of the bounded types (i.e., <tt class="computeroutput">T1</tt>,
  214. <tt class="computeroutput">T2</tt>, etc.).</li><li>Every bounded type must fulfill the requirements of the
  215. <a href="Assignable.html" title="Concept Assignable">Assignable</a>
  216. concept.</li></ul></div><br><b><span class="term">Effects</span></b>:
  217. If the contained type of <tt class="computeroutput">*this</tt> is
  218. <tt class="computeroutput">T</tt>, then assigns <tt class="computeroutput">rhs</tt> into the content
  219. of <tt class="computeroutput">*this</tt>. Otherwise, makes the content of
  220. <tt class="computeroutput">*this</tt> the best conversion of <tt class="computeroutput">rhs</tt> to
  221. one of the bounded types, as determined by standard overload
  222. resolution rules, destroying the previous content of
  223. <tt class="computeroutput">*this</tt>.<br><b><span class="term">Throws</span></b>:
  224. If the contained type of <tt class="computeroutput">*this</tt> is
  225. <tt class="computeroutput">T</tt>, then may fail with any exceptions arising from
  226. the assignment of <tt class="computeroutput">rhs</tt> into the content
  227. <tt class="computeroutput">*this</tt>. Otherwise, may fail with any exceptions
  228. arising from the conversion of <tt class="computeroutput">rhs</tt> to one of the
  229. bounded types. Also, in the event of insufficient memory, may
  230. fail with <tt class="computeroutput">std::bad_alloc</tt>
  231. (<a href="variant.design.html#variant.design.never-empty.problem" title="The Implementation Problem">why?</a>).</p></li></ol></div></div><div class="refsect2" lang="en"><h3><a name="id2370110-bb"></a><tt class="computeroutput">variant</tt> queries</h3><div class="orderedlist"><ol type="1"><li><pre class="literallayout"><span class="type"><span class="bold"><b>int</b></span></span> <a name="id2466395-bb"></a>which() <span class="bold"><b>const</b></span>;</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Returns</span></b>:
  232. The zero-based index into the set of bounded types
  233. of the contained type of <tt class="computeroutput">*this</tt>. (For instance, if
  234. called on a <tt class="computeroutput">variant&lt;int, std::string&gt;</tt> object
  235. containing a <tt class="computeroutput">std::string</tt>, <tt class="computeroutput">which()</tt>
  236. would return <tt class="computeroutput">1</tt>.)<br><b><span class="term">Throws</span></b>:
  237. Will not throw.</p></li><li><pre class="literallayout"><span class="type"><span class="bold"><b>bool</b></span></span> <a name="id2494720-bb"></a>empty() <span class="bold"><b>const</b></span>;</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Returns</span></b>:
  238. <tt class="computeroutput">false</tt>: <tt class="computeroutput">variant</tt> always contains
  239. exactly one of its bounded types. (See
  240. <a href="variant.design.html#variant.design.never-empty" title='"Never-Empty" Guarantee'>the section called &#8220;"Never-Empty" Guarantee&#8221;</a>
  241. for more information.)<br><b><span class="term">Rationale</span></b>:
  242. Facilitates generic compatibility with
  243. <a href="class.boost.any.html" title="Class any">boost::any</a>.<br><b><span class="term">Throws</span></b>:
  244. Will not throw.</p></li><li><pre class="literallayout"><span class="type"><span class="bold"><b>const</b></span> std::type_info &amp;</span> <a name="id2508948-bb"></a>type() <span class="bold"><b>const</b></span>;</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Returns</span></b>:
  245. <tt class="computeroutput">typeid(x)</tt>, where <tt class="computeroutput">x</tt> is the the
  246. content of <tt class="computeroutput">*this</tt>.<br><b><span class="term">Throws</span></b>:
  247. Will not throw.</p></li></ol></div></div><div class="refsect2" lang="en"><h3><a name="id2450166-bb"></a><tt class="computeroutput">variant</tt> relational</h3><div class="orderedlist"><ol type="1"><li><p><a name="id2370129-bb"></a></p><pre class="literallayout"><span class="type"><span class="bold"><b>bool</b></span></span> <a name="id2492818-bb"></a><span class="bold"><b>operator</b></span>==(<span class="bold"><b>const</b></span> variant &amp; rhs) <span class="bold"><b>const</b></span>;
  248. <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U&gt; <span class="type"><span class="bold"><b>void</b></span></span> <a name="id2495748-bb"></a><span class="bold"><b>operator</b></span>==(<span class="bold"><b>const</b></span> U &amp; ) <span class="bold"><b>const</b></span>;</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Notes</span></b>:
  249. The overload returning <tt class="computeroutput">void</tt> exists only to
  250. prohibit implicit conversion of the operator's right-hand side
  251. to <tt class="computeroutput">variant</tt>; thus, its use will (purposefully)
  252. result in a compile-time error.<br><b><span class="term">Requires</span></b>:
  253. Every bounded type of the <tt class="computeroutput">variant</tt> must
  254. fulfill the requirements of the
  255. <a href="EqualityComparable.html" title="Concept EqualityComparable">EqualityComparable</a>
  256. concept.<br><b><span class="term">Returns</span></b>:
  257. <tt class="computeroutput">true</tt> iff <tt class="computeroutput">which() == rhs.which()</tt><span class="emphasis"><em>and</em></span><tt class="computeroutput">content_this == content_rhs</tt>, where
  258. <tt class="computeroutput">content_this</tt> is the content of <tt class="computeroutput">*this</tt>
  259. and <tt class="computeroutput">content_rhs</tt> is the content of
  260. <tt class="computeroutput">rhs</tt>.<br><b><span class="term">Throws</span></b>:
  261. If <tt class="computeroutput">which() == rhs.which()</tt> then may fail with
  262. any exceptions arising from <tt class="computeroutput">operator==(T,T)</tt>, where
  263. <tt class="computeroutput">T</tt> is the contained type of
  264. <tt class="computeroutput">*this</tt>.</p></li><li><p><a name="id2440328-bb"></a></p><pre class="literallayout"><span class="type"><span class="bold"><b>bool</b></span></span> <a name="id2372301-bb"></a><span class="bold"><b>operator</b></span>&lt;(<span class="bold"><b>const</b></span> variant &amp; rhs) <span class="bold"><b>const</b></span>;
  265. <span class="bold"><b>template</b></span>&lt;<span class="bold"><b>typename</b></span> U&gt; <span class="type"><span class="bold"><b>void</b></span></span> <a name="id2433666-bb"></a><span class="bold"><b>operator</b></span>&lt;(<span class="bold"><b>const</b></span> U &amp; ) <span class="bold"><b>const</b></span>;</pre><p xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"><b><span class="term">Notes</span></b>:
  266. The overload returning <tt class="computeroutput">void</tt> exists only to
  267. prohibit implicit conversion of the operator's right-hand side
  268. to <tt class="computeroutput">variant</tt>; thus, its use will (purposefully)
  269. result in a compile-time error.<br><b><span class="term">Requires</span></b>:
  270. Every bounded type of the <tt class="computeroutput">variant</tt> must
  271. fulfill the requirements of the
  272. <a href="LessThanComparable.html" title="Concept LessThanComparable">LessThanComparable</a>
  273. concept.<br><b><span class="term">Returns</span></b>:
  274. If <tt class="computeroutput">which() == rhs.which()</tt> then:
  275. <tt class="computeroutput">content_this &lt; content_rhs</tt>, where
  276. <tt class="computeroutput">content_this</tt> is the content of <tt class="computeroutput">*this</tt>
  277. and <tt class="computeroutput">content_rhs</tt> is the content of <tt class="computeroutput">rhs</tt>.
  278. Otherwise: <tt class="computeroutput">which() &lt; rhs.which()</tt>.<br><b><span class="term">Throws</span></b>:
  279. If <tt class="computeroutput">which() == rhs.which()</tt> then may fail with
  280. any exceptions arising from <tt class="computeroutput">operator&lt;(T,T)</tt>,
  281. where <tt class="computeroutput">T</tt> is the contained type of
  282. <tt class="computeroutput">*this</tt>.</p></li></ol></div></div></div></div><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr><td align="left"><small>Last revised: , at GMT</small></td><td align="right"><small>Copyright © 2002, 2003 Eric Friedman, Itay Maman</small></td></tr></table><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT.html"><img src="images/prev.png" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="variant.reference.html#header.boost.variant.variant.hpp"><img src="images/up.png" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="id2518553-bb.html"><img src="images/next.png" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">Macro BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a></td><td width="40%" align="right" valign="top"> Function template swap</td></tr></table></div></body></html>
粤ICP备19079148号