variant.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Boost.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="index.html" title="The Boost C++ Libraries"><link rel="previous" href="signals.tests.html" title="Testsuite"><link rel="next" href="variant.tutorial.html" title="Tutorial"><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="variant.html#variant.intro" title="Introduction"><link rel="section" href="variant.tutorial.html" title="Tutorial"><link rel="section" href="variant.reference.html" title="Reference"><link rel="section" href="variant.design.html" title="Design Overview"><link rel="section" href="variant.misc.html" title="Miscellaneous Notes"><link rel="section" href="variant.refs.html" title="References"></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="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="variant"></a>Boost.Variant</h2></div><div><div class="author"><h3 class="author"><span class="firstname">Eric</span> <span class="surname">Friedman</span></h3></div></div><div><div class="author"><h3 class="author"><span class="firstname">Itay</span> <span class="surname">Maman</span></h3></div></div><div><p class="copyright">Copyright © 2002, 2003 Eric Friedman, Itay Maman</p></div><div><div class="legalnotice"><p>Permission to copy, use, sell and distribute this software
  74. is granted provided this copyright notice appears in all copies.
  75. Permission to modify the code and to distribute modified code is
  76. granted provided this copyright notice appears in all copies, and
  77. a notice that the code was modified is included with the copyright
  78. notice.</p><p> This software is provided "as is" without express or
  79. implied warranty, and with no claim as to its suitability for any
  80. purpose.</p></div></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="variant.html#variant.intro">Introduction</a></dt><dd><dl><dt><a href="variant.html#variant.abstract">Abstract</a></dt><dt><a href="variant.html#variant.motivation">Motivation</a></dt></dl></dd><dt><a href="variant.tutorial.html">Tutorial</a></dt><dd><dl><dt><a href="variant.tutorial.html#variant.tutorial.basic">Basic Usage</a></dt><dt><a href="variant.tutorial.html#variant.tutorial.advanced">Advanced Topics</a></dt></dl></dd><dt><a href="variant.reference.html">Reference</a></dt><dd><dl><dt><a href="variant.reference.html#variant.concepts">Concepts</a></dt><dt><a href="variant.reference.html#header.boost.variant.hpp">Header &lt;boost/variant.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.variant_fwd.hpp">Header &lt;boost/variant/variant_fwd.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.variant.hpp">Header &lt;boost/variant/variant.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.recursive_variant.hpp">Header &lt;boost/variant/recursive_variant.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.recursive_wrapper.hpp">Header &lt;boost/variant/recursive_wrapper.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.apply_visitor.hpp">Header &lt;boost/variant/apply_visitor.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.get.hpp">Header &lt;boost/variant/get.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.bad_visit.hpp">Header &lt;boost/variant/bad_visit.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.static_visitor.hpp">Header &lt;boost/variant/static_visitor.hpp&gt;</a></dt><dt><a href="variant.reference.html#header.boost.variant.visitor_ptr.hpp">Header &lt;boost/variant/visitor_ptr.hpp&gt;</a></dt></dl></dd><dt><a href="variant.design.html">Design Overview</a></dt><dd><dl><dt><a href="variant.design.html#variant.design.never-empty">"Never-Empty" Guarantee</a></dt></dl></dd><dt><a href="variant.misc.html">Miscellaneous Notes</a></dt><dd><dl><dt><a href="variant.misc.html#variant.versus-any">Boost.Variant vs. Boost.Any</a></dt><dt><a href="variant.misc.html#id2539312">Portability</a></dt><dt><a href="variant.misc.html#variant.troubleshooting">Troubleshooting</a></dt><dt><a href="variant.misc.html#variant.ack">Acknowledgments</a></dt></dl></dd><dt><a href="variant.refs.html">References</a></dt></dl></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="variant.intro"></a>Introduction</h3></div></div><div></div></div><div class="toc"><dl><dt><a href="variant.html#variant.abstract">Abstract</a></dt><dt><a href="variant.html#variant.motivation">Motivation</a></dt></dl></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="variant.abstract"></a>Abstract</h4></div></div><div></div></div><p>The <tt class="computeroutput">variant</tt> class template is a safe, generic, stack-based
  81. discriminated union container, offering a simple solution for manipulating an
  82. object from a heterogeneous set of types in a uniform manner. Whereas
  83. standard containers such as <tt class="computeroutput">std::vector</tt> may be thought of as
  84. "<span class="bold"><b>multi-value, single type</b></span>,"
  85. <tt class="computeroutput">variant</tt> is "<span class="bold"><b>multi-type,
  86. single value</b></span>."</p><p>Notable features of <tt class="computeroutput"><a href="class.boost.variant.html" title="Class template variant">boost::variant</a></tt>
  87. include:</p><div class="itemizedlist"><ul type="disc"><li>Full value semantics, including adherence to standard
  88. overload resolution rules for conversion operations.</li><li>Compile-time type-safe value visitation via
  89. <tt class="computeroutput"><a href="id2557051-bb.html" title="Function apply_visitor">boost::apply_visitor</a></tt>.</li><li>Run-time checked explicit value retrieval via
  90. <tt class="computeroutput"><a href="id2493238-bb.html" title="Function get">boost::get</a></tt>.</li><li>Support for recursive variant types via both
  91. <tt class="computeroutput"><a href="class.boost.make_recursive_variant.html" title="Class template make_recursive_variant">boost::make_recursive_variant</a></tt> and
  92. <tt class="computeroutput"><a href="class.boost.recursive_wrapper.html" title="Class template recursive_wrapper">boost::recursive_wrapper</a></tt>.</li><li>Efficient implementation -- stack-based when possible (see
  93. <a href="variant.design.html#variant.design.never-empty" title='"Never-Empty" Guarantee'>the section called &#8220;"Never-Empty" Guarantee&#8221;</a> for more details).</li></ul></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="variant.motivation"></a>Motivation</h4></div></div><div></div></div><div class="toc"><dl><dt><a href="variant.html#variant.motivation.problem">Problem</a></dt><dt><a href="variant.html#variant.motivation.solution">Solution: A Motivating Example</a></dt></dl></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="variant.motivation.problem"></a>Problem</h5></div></div><div></div></div><p>Many times, during the development of a C++ program, the
  94. programmer finds himself in need of manipulating several distinct
  95. types in a uniform manner. Indeed, C++ features direct language
  96. support for such types through its <tt class="computeroutput">union</tt>
  97. keyword:</p><pre class="programlisting">union { int i; double d; } u;
  98. u.d = 3.14;
  99. u.i = 3; // overwrites u.d (OK: u.d is a POD type)</pre><p>C++'s <tt class="computeroutput">union</tt> construct, however, is nearly
  100. useless in an object-oriented environment. The construct entered
  101. the language primarily as a means for preserving compatibility with
  102. C, which supports only POD (Plain Old Data) types, and so does not
  103. accept types exhibiting non-trivial construction or
  104. destruction:</p><pre class="programlisting">union {
  105. int i;
  106. std::string s; // illegal: std::string is not a POD type!
  107. } u;</pre><p>Clearly another approach is required. Typical solutions
  108. feature the dynamic-allocation of objects, which are subsequently
  109. manipulated through a common base type (often a virtual base class
  110. [<a href="variant.refs.html#variant.refs.hen01">Hen01</a>]
  111. or, more dangerously, a <tt class="computeroutput">void*</tt>). Objects of
  112. concrete type may be then retrieved by way of a polymorphic downcast
  113. construct (e.g., <tt class="computeroutput">dynamic_cast</tt>,
  114. <tt class="computeroutput"><a href="id2383300-bb.html" title="Function any_cast">boost::any_cast</a></tt>, etc.).</p><p>However, solutions of this sort are highly error-prone, due
  115. to the following:</p><div class="itemizedlist"><ul type="disc"><li><span class="emphasis"><em>Downcast errors cannot be detected at
  116. compile-time.</em></span> Thus, incorrect usage of downcast
  117. constructs will lead to bugs detectable only at run-time.</li><li><span class="emphasis"><em>Addition of new concrete types may be
  118. ignored.</em></span> If a new concrete type is added to the
  119. hierarchy, existing downcast code will continue to work as-is,
  120. wholly ignoring the new type. Consequently, the programmer must
  121. manually locate and modify code at numerous locations, which often
  122. results in run-time errors that are difficult to find.</li></ul></div><p>Furthermore, even when properly implemented, these solutions tend
  123. to incur a relatively significant abstraction penalty due to the use of
  124. the heap, virtual function calls, and polymorphic downcasts.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="variant.motivation.solution"></a>Solution: A Motivating Example</h5></div></div><div></div></div><p>The <tt class="computeroutput"><a href="class.boost.variant.html" title="Class template variant">boost::variant</a></tt> class template
  125. addresses these issues in a safe, straightforward, and efficient manner. The
  126. following example demonstrates how the class can be used:</p><pre class="programlisting">#include "boost/variant.hpp"
  127. #include &lt;iostream&gt;
  128. class my_visitor : public <a href="class.boost.static_visitor.html" title="Class template static_visitor">boost::static_visitor</a>&lt;int&gt;
  129. {
  130. public:
  131. int operator()(int i) const
  132. {
  133. return i;
  134. }
  135. int operator()(const std::string &amp; str) const
  136. {
  137. return str.length();
  138. }
  139. };
  140. int main()
  141. {
  142. <a href="class.boost.variant.html" title="Class template variant">boost::variant</a>&lt; int, std::string &gt; u("hello world");
  143. std::cout &lt;&lt; u; // output: hello world
  144. int result = <a href="id2557051-bb.html" title="Function apply_visitor">boost::apply_visitor</a>( my_visitor(), u );
  145. std::cout &lt;&lt; result; // output: 11 (i.e., length of "hello world")
  146. }
  147. </pre></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></small></td></tr></table><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="signals.tests.html"><img src="images/prev.png" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="variant.tutorial.html"><img src="images/next.png" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">Testsuite </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"> Tutorial</td></tr></table></div></body></html>
粤ICP备19079148号