diff options
Diffstat (limited to 'doc/html/variant.html')
-rw-r--r-- | doc/html/variant.html | 226 |
1 files changed, 218 insertions, 8 deletions
diff --git a/doc/html/variant.html b/doc/html/variant.html index 193a0cec8e..197db47fcb 100644 --- a/doc/html/variant.html +++ b/doc/html/variant.html @@ -1,10 +1,220 @@ -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> - <head> - <meta http-equiv="refresh" content="0; URL=http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/variant.html"> - </head> - <body> - Automatic redirection failed, please go to - <a href="http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/variant.html">http://www.boost.org/regression-logs/cs-win32_metacomm/doc/html/variant.html</a> - </body> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<title>Chapter 15. Boost.Variant</title> +<link rel="stylesheet" href="boostbook.css" type="text/css"> +<meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> +<link rel="start" href="index.html" title="The Boost C++ Libraries"> +<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries"> +<link rel="prev" href="boost_typetraits/credits.html" title=" Credits"> +<link rel="next" href="variant/tutorial.html" title="Tutorial"> +</head> +<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> +<table cellpadding="2" width="100%"> +<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../boost.png"></td> +<td align="center"><a href="../../index.htm">Home</a></td> +<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td> +<td align="center"><a href="../../people/people.htm">People</a></td> +<td align="center"><a href="../../more/faq.htm">FAQ</a></td> +<td align="center"><a href="../../more/index.htm">More</a></td> +</table> +<hr> +<div class="spirit-nav"> +<a accesskey="p" href="boost_typetraits/credits.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="variant/tutorial.html"><img src="images/next.png" alt="Next"></a> +</div> +<div class="chapter" lang="en"> +<div class="titlepage"><div> +<div><h2 class="title"> +<a name="variant"></a>Chapter 15. 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"> +<a name="id1642545"></a><p>Permission to copy, use, sell and distribute this software + is granted provided this copyright notice appears in all copies. + Permission to modify the code and to distribute modified code is + granted provided this copyright notice appears in all copies, and + a notice that the code was modified is included with the copyright + notice.</p> +<p> This software is provided "as is" without express or + implied warranty, and with no claim as to its suitability for any + purpose.</p> +</div></div> +</div></div> +<div class="toc"> +<p><b>Table of Contents</b></p> +<dl> +<dt><span class="section"><a href="variant.html#variant.intro">Introduction</a></span></dt> +<dd><dl> +<dt><span class="section"><a href="variant.html#variant.abstract">Abstract</a></span></dt> +<dt><span class="section"><a href="variant.html#variant.motivation">Motivation</a></span></dt> +</dl></dd> +<dt><span class="section"><a href="variant/tutorial.html">Tutorial</a></span></dt> +<dd><dl> +<dt><span class="section"><a href="variant/tutorial.html#variant.tutorial.basic">Basic Usage</a></span></dt> +<dt><span class="section"><a href="variant/tutorial.html#variant.tutorial.advanced">Advanced Topics</a></span></dt> +</dl></dd> +<dt><span class="section"><a href="variant/reference.html">Reference</a></span></dt> +<dd><dl> +<dt><span class="section"><a href="variant/reference.html#variant.concepts">Concepts</a></span></dt> +<dt><span class="section"><a href="variant/reference.html#header.boost.variant.hpp">Header <boost/variant.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1283515">Header <boost/variant/variant_fwd.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1258086">Header <boost/variant/variant.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1362374">Header <boost/variant/recursive_variant.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1311990">Header <boost/variant/recursive_wrapper.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id872290">Header <boost/variant/apply_visitor.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id894482">Header <boost/variant/get.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1561653">Header <boost/variant/bad_visit.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1216732">Header <boost/variant/static_visitor.hpp></a></span></dt> +<dt><span class="section"><a href="variant/reference.html#id1040401">Header <boost/variant/visitor_ptr.hpp></a></span></dt> +</dl></dd> +<dt><span class="section"><a href="variant/design.html">Design Overview</a></span></dt> +<dd><dl><dt><span class="section"><a href="variant/design.html#variant.design.never-empty">"Never-Empty" Guarantee</a></span></dt></dl></dd> +<dt><span class="section"><a href="variant/misc.html">Miscellaneous Notes</a></span></dt> +<dd><dl> +<dt><span class="section"><a href="variant/misc.html#variant.versus-any">Boost.Variant vs. Boost.Any</a></span></dt> +<dt><span class="section"><a href="variant/misc.html#id1671669">Portability</a></span></dt> +<dt><span class="section"><a href="variant/misc.html#variant.troubleshooting">Troubleshooting</a></span></dt> +<dt><span class="section"><a href="variant/misc.html#variant.ack">Acknowledgments</a></span></dt> +</dl></dd> +<dt><span class="section"><a href="variant/refs.html">References</a></span></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 class="toc"><dl> +<dt><span class="section"><a href="variant.html#variant.abstract">Abstract</a></span></dt> +<dt><span class="section"><a href="variant.html#variant.motivation">Motivation</a></span></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> +<p>The <code class="computeroutput">variant</code> class template is a safe, generic, stack-based +discriminated union container, offering a simple solution for manipulating an +object from a heterogeneous set of types in a uniform manner. Whereas +standard containers such as <code class="computeroutput">std::vector</code> may be thought of as +"<span class="bold"><strong>multi-value, single type</strong></span>," +<code class="computeroutput">variant</code> is "<span class="bold"><strong>multi-type, +single value</strong></span>."</p> +<p>Notable features of <code class="computeroutput"><a href="boost/variant.html" title="Class template variant">boost::variant</a></code> +include:</p> +<div class="itemizedlist"><ul type="disc"> +<li>Full value semantics, including adherence to standard + overload resolution rules for conversion operations.</li> +<li>Compile-time type-safe value visitation via + <code class="computeroutput"><a href="apply_visitor.html" title="Function apply_visitor">boost::apply_visitor</a></code>.</li> +<li>Run-time checked explicit value retrieval via + <code class="computeroutput"><a href="get.html" title="Function get">boost::get</a></code>.</li> +<li>Support for recursive variant types via both + <code class="computeroutput"><a href="make_recursive_variant.html" title="Class template make_recursive_variant">boost::make_recursive_variant</a></code> and + <code class="computeroutput"><a href="recursive_wrapper.html" title="Class template recursive_wrapper">boost::recursive_wrapper</a></code>.</li> +<li>Efficient implementation -- stack-based when possible (see + <a href="variant/design.html#variant.design.never-empty" title='"Never-Empty" Guarantee'>the section called “"Never-Empty" Guarantee”</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 class="toc"><dl> +<dt><span class="section"><a href="variant.html#variant.motivation.problem">Problem</a></span></dt> +<dt><span class="section"><a href="variant.html#variant.motivation.solution">Solution: A Motivating Example</a></span></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> +<p>Many times, during the development of a C++ program, the +programmer finds himself in need of manipulating several distinct +types in a uniform manner. Indeed, C++ features direct language +support for such types through its <code class="computeroutput">union</code> +keyword:</p> +<pre class="programlisting">union { int i; double d; } u; +u.d = 3.14; +u.i = 3; // overwrites u.d (OK: u.d is a POD type)</pre> +<p>C++'s <code class="computeroutput">union</code> construct, however, is nearly +useless in an object-oriented environment. The construct entered +the language primarily as a means for preserving compatibility with +C, which supports only POD (Plain Old Data) types, and so does not +accept types exhibiting non-trivial construction or +destruction:</p> +<pre class="programlisting">union { + int i; + std::string s; // illegal: std::string is not a POD type! +} u;</pre> +<p>Clearly another approach is required. Typical solutions +feature the dynamic-allocation of objects, which are subsequently +manipulated through a common base type (often a virtual base class + [<a href="variant/refs.html#variant.refs.hen01">Hen01</a>] +or, more dangerously, a <code class="computeroutput">void*</code>). Objects of +concrete type may be then retrieved by way of a polymorphic downcast +construct (e.g., <code class="computeroutput">dynamic_cast</code>, +<code class="computeroutput"><a href="any_cast.html" title="Function any_cast">boost::any_cast</a></code>, etc.).</p> +<p>However, solutions of this sort are highly error-prone, due +to the following:</p> +<div class="itemizedlist"><ul type="disc"> +<li> +<span class="emphasis"><em>Downcast errors cannot be detected at + compile-time.</em></span> Thus, incorrect usage of downcast + constructs will lead to bugs detectable only at run-time.</li> +<li> +<span class="emphasis"><em>Addition of new concrete types may be + ignored.</em></span> If a new concrete type is added to the + hierarchy, existing downcast code will continue to work as-is, + wholly ignoring the new type. Consequently, the programmer must + manually locate and modify code at numerous locations, which often + results in run-time errors that are difficult to find.</li> +</ul></div> +<p>Furthermore, even when properly implemented, these solutions tend +to incur a relatively significant abstraction penalty due to the use of +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> +<p>The <code class="computeroutput"><a href="boost/variant.html" title="Class template variant">boost::variant</a></code> class template +addresses these issues in a safe, straightforward, and efficient manner. The +following example demonstrates how the class can be used:</p> +<pre class="programlisting">#include "boost/variant.hpp" +#include <iostream> + +class my_visitor : public <code class="computeroutput"><a href="static_visitor.html" title="Class template static_visitor">boost::static_visitor</a></code><int> +{ +public: + int operator()(int i) const + { + return i; + } + + int operator()(const <code class="computeroutput">std::string</code> & str) const + { + return str.length(); + } +}; + +int main() +{ + <code class="computeroutput"><a href="boost/variant.html" title="Class template variant">boost::variant</a></code>< int, std::string > u("hello world"); + std::cout << u; // output: hello world + + int result = <code class="computeroutput"><a href="apply_visitor.html" title="Function apply_visitor">boost::apply_visitor</a></code>( my_visitor(), u ); + std::cout << result; // output: 11 (i.e., length of "hello world") +} +</pre> +</div> +</div> +</div> +</div> +<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> +<td align="left"></td> +<td align="right"><small></small></td> +</tr></table> +<hr> +<div class="spirit-nav"> +<a accesskey="p" href="boost_typetraits/credits.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="variant/tutorial.html"><img src="images/next.png" alt="Next"></a> +</div> +</body> </html> |