1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Chapter 45. Boost.Variant</title> 6<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)"> 10<link rel="prev" href="unordered/bibliography.html" title="Bibliography"> 11<link rel="next" href="variant/tutorial.html" title="Tutorial"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td> 16<td align="center"><a href="../../index.html">Home</a></td> 17<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="unordered/bibliography.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="variant/tutorial.html"><img src="../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="chapter"> 27<div class="titlepage"><div> 28<div><h2 class="title"> 29<a name="variant"></a>Chapter 45. Boost.Variant</h2></div> 30<div><div class="author"><h3 class="author"> 31<span class="firstname">Eric</span> <span class="surname">Friedman</span> 32</h3></div></div> 33<div><div class="author"><h3 class="author"> 34<span class="firstname">Itay</span> <span class="surname">Maman</span> 35</h3></div></div> 36<div><p class="copyright">Copyright © 2002, 2003 Eric Friedman, Itay Maman</p></div> 37<div><p class="copyright">Copyright © 2014-2020 Antony Polukhin</p></div> 38<div><div class="legalnotice"> 39<a name="id-1.3.46.1.5"></a><p>Distributed under the Boost Software License, Version 1.0. 40 (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 41 <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 42 </p> 43</div></div> 44</div></div> 45<div class="toc"> 46<p><b>Table of Contents</b></p> 47<dl class="toc"> 48<dt><span class="section"><a href="variant.html#variant.intro">Introduction</a></span></dt> 49<dd><dl> 50<dt><span class="section"><a href="variant.html#variant.abstract">Abstract</a></span></dt> 51<dt><span class="section"><a href="variant.html#variant.motivation">Motivation</a></span></dt> 52</dl></dd> 53<dt><span class="section"><a href="variant/tutorial.html">Tutorial</a></span></dt> 54<dd><dl> 55<dt><span class="section"><a href="variant/tutorial.html#variant.tutorial.basic">Basic Usage</a></span></dt> 56<dt><span class="section"><a href="variant/tutorial.html#variant.tutorial.advanced">Advanced Topics</a></span></dt> 57</dl></dd> 58<dt><span class="section"><a href="variant/reference.html">Reference</a></span></dt> 59<dd><dl> 60<dt><span class="section"><a href="variant/reference.html#variant.concepts">Concepts</a></span></dt> 61<dt><span class="section"><a href="variant/reference.html#header.boost.variant_hpp">Header <boost/variant.hpp></a></span></dt> 62<dt><span class="section"><a href="variant/reference.html#header.boost.variant.variant_fwd_hpp">Header <boost/variant/variant_fwd.hpp></a></span></dt> 63<dt><span class="section"><a href="variant/reference.html#header.boost.variant.variant_hpp">Header <boost/variant/variant.hpp></a></span></dt> 64<dt><span class="section"><a href="variant/reference.html#header.boost.variant.recursive_variant_hpp">Header <boost/variant/recursive_variant.hpp></a></span></dt> 65<dt><span class="section"><a href="variant/reference.html#header.boost.variant.recursive_wrapper_hpp">Header <boost/variant/recursive_wrapper.hpp></a></span></dt> 66<dt><span class="section"><a href="variant/reference.html#header.boost.variant.apply_visitor_hpp">Header <boost/variant/apply_visitor.hpp></a></span></dt> 67<dt><span class="section"><a href="variant/reference.html#header.boost.variant.multivisitors_hpp">Header <boost/variant/multivisitors.hpp></a></span></dt> 68<dt><span class="section"><a href="variant/reference.html#header.boost.variant.get_hpp">Header <boost/variant/get.hpp></a></span></dt> 69<dt><span class="section"><a href="variant/reference.html#header.boost.variant.polymorphic_get_hpp">Header <boost/variant/polymorphic_get.hpp></a></span></dt> 70<dt><span class="section"><a href="variant/reference.html#header.boost.variant.bad_visit_hpp">Header <boost/variant/bad_visit.hpp></a></span></dt> 71<dt><span class="section"><a href="variant/reference.html#header.boost.variant.static_visitor_hpp">Header <boost/variant/static_visitor.hpp></a></span></dt> 72<dt><span class="section"><a href="variant/reference.html#header.boost.variant.visitor_ptr_hpp">Header <boost/variant/visitor_ptr.hpp></a></span></dt> 73</dl></dd> 74<dt><span class="section"><a href="variant/design.html">Design Overview</a></span></dt> 75<dd><dl><dt><span class="section"><a href="variant/design.html#variant.design.never-empty">"Never-Empty" Guarantee</a></span></dt></dl></dd> 76<dt><span class="section"><a href="variant/misc.html">Miscellaneous Notes</a></span></dt> 77<dd><dl> 78<dt><span class="section"><a href="variant/misc.html#variant.versus-any">Boost.Variant vs. Boost.Any</a></span></dt> 79<dt><span class="section"><a href="variant/misc.html#id-1.3.46.7.3">Portability</a></span></dt> 80<dt><span class="section"><a href="variant/misc.html#variant.troubleshooting">Troubleshooting</a></span></dt> 81<dt><span class="section"><a href="variant/misc.html#variant.ack">Acknowledgments</a></span></dt> 82</dl></dd> 83<dt><span class="section"><a href="variant/refs.html">References</a></span></dt> 84</dl> 85</div> 86<div class="section"> 87<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 88<a name="variant.intro"></a>Introduction</h2></div></div></div> 89<div class="toc"><dl class="toc"> 90<dt><span class="section"><a href="variant.html#variant.abstract">Abstract</a></span></dt> 91<dt><span class="section"><a href="variant.html#variant.motivation">Motivation</a></span></dt> 92</dl></div> 93<div class="section"> 94<div class="titlepage"><div><div><h3 class="title"> 95<a name="variant.abstract"></a>Abstract</h3></div></div></div> 96<p>The <code class="computeroutput">variant</code> class template is a safe, generic, stack-based 97discriminated union container, offering a simple solution for manipulating an 98object from a heterogeneous set of types in a uniform manner. Whereas 99standard containers such as <code class="computeroutput">std::vector</code> may be thought of as 100"<span class="bold"><strong>multi-value, single type</strong></span>," 101<code class="computeroutput">variant</code> is "<span class="bold"><strong>multi-type, 102single value</strong></span>."</p> 103<p>Notable features of <code class="computeroutput"><a class="link" href="boost/variant.html" title="Class template variant">boost::variant</a></code> 104include:</p> 105<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 106<li class="listitem">Full value semantics, including adherence to standard 107 overload resolution rules for conversion operations.</li> 108<li class="listitem">Compile-time type-safe value visitation via 109 <code class="computeroutput"><a class="link" href="boost/apply_visitor.html" title="Function apply_visitor">boost::apply_visitor</a></code>.</li> 110<li class="listitem">Run-time checked explicit value retrieval via 111 <code class="computeroutput"><a class="link" href="boost/get.html" title="Function get">boost::get</a></code>.</li> 112<li class="listitem">Support for recursive variant types via both 113 <code class="computeroutput"><a class="link" href="boost/make_recursive_variant.html" title="Class template make_recursive_variant">boost::make_recursive_variant</a></code> and 114 <code class="computeroutput"><a class="link" href="boost/recursive_wrapper.html" title="Class template recursive_wrapper">boost::recursive_wrapper</a></code>.</li> 115<li class="listitem">Efficient implementation -- stack-based when possible (see 116 <a class="xref" href="variant/design.html#variant.design.never-empty" title='"Never-Empty" Guarantee'>the section called “"Never-Empty" Guarantee”</a> for more details).</li> 117</ul></div> 118</div> 119<div class="section"> 120<div class="titlepage"><div><div><h3 class="title"> 121<a name="variant.motivation"></a>Motivation</h3></div></div></div> 122<div class="toc"><dl class="toc"> 123<dt><span class="section"><a href="variant.html#variant.motivation.problem">Problem</a></span></dt> 124<dt><span class="section"><a href="variant.html#variant.motivation.solution">Solution: A Motivating Example</a></span></dt> 125</dl></div> 126<div class="section"> 127<div class="titlepage"><div><div><h4 class="title"> 128<a name="variant.motivation.problem"></a>Problem</h4></div></div></div> 129<p>Many times, during the development of a C++ program, the 130programmer finds himself in need of manipulating several distinct 131types in a uniform manner. Indeed, C++ features direct language 132support for such types through its <code class="computeroutput">union</code> 133keyword:</p> 134<pre class="programlisting">union { int i; double d; } u; 135u.d = 3.14; 136u.i = 3; // overwrites u.d (OK: u.d is a POD type)</pre> 137<p>C++'s <code class="computeroutput">union</code> construct, however, is nearly 138useless in an object-oriented environment. The construct entered 139the language primarily as a means for preserving compatibility with 140C, which supports only POD (Plain Old Data) types, and so does not 141accept types exhibiting non-trivial construction or 142destruction:</p> 143<pre class="programlisting">union { 144 int i; 145 std::string s; // illegal: std::string is not a POD type! 146} u;</pre> 147<p>Clearly another approach is required. Typical solutions 148feature the dynamic-allocation of objects, which are subsequently 149manipulated through a common base type (often a virtual base class 150 [<a class="link" href="variant/refs.html#variant.refs.hen01">Hen01</a>] 151or, more dangerously, a <code class="computeroutput">void*</code>). Objects of 152concrete type may be then retrieved by way of a polymorphic downcast 153construct (e.g., <code class="computeroutput">dynamic_cast</code>, 154<code class="computeroutput"><a class="link" href="boost/any_cast.html" title="Function any_cast">boost::any_cast</a></code>, etc.).</p> 155<p>However, solutions of this sort are highly error-prone, due 156to the following:</p> 157<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 158<li class="listitem"> 159<span class="emphasis"><em>Downcast errors cannot be detected at 160 compile-time.</em></span> Thus, incorrect usage of downcast 161 constructs will lead to bugs detectable only at run-time.</li> 162<li class="listitem"> 163<span class="emphasis"><em>Addition of new concrete types may be 164 ignored.</em></span> If a new concrete type is added to the 165 hierarchy, existing downcast code will continue to work as-is, 166 wholly ignoring the new type. Consequently, the programmer must 167 manually locate and modify code at numerous locations, which often 168 results in run-time errors that are difficult to find.</li> 169</ul></div> 170<p>Furthermore, even when properly implemented, these solutions tend 171to incur a relatively significant abstraction penalty due to the use of 172the heap, virtual function calls, and polymorphic downcasts.</p> 173</div> 174<div class="section"> 175<div class="titlepage"><div><div><h4 class="title"> 176<a name="variant.motivation.solution"></a>Solution: A Motivating Example</h4></div></div></div> 177<p>The <code class="computeroutput"><a class="link" href="boost/variant.html" title="Class template variant">boost::variant</a></code> class template 178addresses these issues in a safe, straightforward, and efficient manner. The 179following example demonstrates how the class can be used:</p> 180<pre class="programlisting">#include "boost/variant.hpp" 181#include <iostream> 182 183class my_visitor : public <code class="computeroutput"><a class="link" href="boost/static_visitor.html" title="Class template static_visitor">boost::static_visitor</a></code><int> 184{ 185public: 186 int operator()(int i) const 187 { 188 return i; 189 } 190 191 int operator()(const <code class="computeroutput">std::string</code> & str) const 192 { 193 return str.length(); 194 } 195}; 196 197int main() 198{ 199 <code class="computeroutput"><a class="link" href="boost/variant.html" title="Class template variant">boost::variant</a></code>< int, std::string > u("hello world"); 200 std::cout << u; // output: hello world 201 202 int result = <code class="computeroutput"><a class="link" href="boost/apply_visitor.html" title="Function apply_visitor">boost::apply_visitor</a></code>( my_visitor(), u ); 203 std::cout << result; // output: 11 (i.e., length of "hello world") 204} 205</pre> 206</div> 207</div> 208</div> 209</div> 210<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 211<td align="left"></td> 212<td align="right"><div class="copyright-footer"></div></td> 213</tr></table> 214<hr> 215<div class="spirit-nav"> 216<a accesskey="p" href="unordered/bibliography.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="variant/tutorial.html"><img src="../../doc/src/images/next.png" alt="Next"></a> 217</div> 218</body> 219</html> 220