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 4. Boost.Any</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="align/history.html" title="History"> 11<link rel="next" href="any/s02.html" title="Examples"> 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="align/history.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="any/s02.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="any"></a>Chapter 4. Boost.Any</h2></div> 30<div><div class="author"><h3 class="author"> 31<span class="firstname">Kevlin</span> <span class="surname">Henney</span> 32</h3></div></div> 33<div><p class="copyright">Copyright © 2001 Kevlin Henney</p></div> 34<div><p class="copyright">Copyright © 2013-2020 Antony Polukhin</p></div> 35<div><div class="legalnotice"> 36<a name="id-1.3.5.1.4"></a><p>Distributed under the Boost Software License, Version 1.0. 37 (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 38 <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 39 </p> 40</div></div> 41</div></div> 42<div class="toc"> 43<p><b>Table of Contents</b></p> 44<dl class="toc"> 45<dt><span class="section"><a href="any.html#id-1.3.5.3">Introduction</a></span></dt> 46<dt><span class="section"><a href="any/s02.html">Examples</a></span></dt> 47<dt><span class="section"><a href="any/reference.html">Reference</a></span></dt> 48<dd><dl> 49<dt><span class="section"><a href="any/reference.html#any.ValueType"><span class="emphasis"><em>ValueType</em></span> requirements</a></span></dt> 50<dt><span class="section"><a href="any/reference.html#header.boost.any_hpp">Header <boost/any.hpp></a></span></dt> 51</dl></dd> 52<dt><span class="section"><a href="any/s04.html">Acknowledgements</a></span></dt> 53</dl> 54</div> 55<div class="section"> 56<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 57<a name="id-1.3.5.3"></a>Introduction</h2></div></div></div> 58<p>There are times when a generic (in the sense of 59 <span class="emphasis"><em>general</em></span> as opposed to 60 <span class="emphasis"><em>template-based programming</em></span>) type is needed: 61 variables that are truly variable, accommodating values of many 62 other more specific types rather than C++'s normal strict and 63 static types. We can distinguish three basic kinds of generic 64 type:</p> 65<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 66<li class="listitem"><p>Converting types that can hold one of a number of 67 possible value types, e.g. <code class="computeroutput">int</code> and 68 <code class="computeroutput">string</code>, and freely convert between them, for 69 instance interpreting <code class="computeroutput">5</code> as <code class="computeroutput">"5"</code> or 70 vice-versa. Such types are common in scripting and other 71 interpreted 72 languages. 73 <code class="computeroutput">boost::lexical_cast</code> 74 supports such conversion functionality.</p></li> 75<li class="listitem"><p> 76 Discriminated types that contain values of different types but 77 do not attempt conversion between them, i.e. <code class="computeroutput">5</code> is 78 held strictly as an <code class="computeroutput">int</code> and is not implicitly 79 convertible either to <code class="computeroutput">"5"</code> or to 80 <code class="computeroutput">5.0</code>. Their indifference to interpretation but 81 awareness of type effectively makes them safe, generic 82 containers of single values, with no scope for surprises from 83 ambiguous conversions.</p></li> 84<li class="listitem"><p> 85 Indiscriminate types that can refer to anything but are 86 oblivious to the actual underlying type, entrusting all forms 87 of access and interpretation to the programmer. This niche is 88 dominated by <code class="computeroutput">void *</code>, which offers plenty of scope 89 for surprising, undefined behavior.</p></li> 90</ul></div> 91<p>The <code class="computeroutput"><a class="link" href="boost/any.html" title="Class any">boost::any</a></code> class 92 (based on the class of the same name described in <a href="http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf" target="_top">"Valued 93 Conversions"</a> by Kevlin Henney, <span class="emphasis"><em>C++ 94 Report</em></span> 12(7), July/August 2000) is a variant value type 95 based on the second category. It supports copying of any value 96 type and safe checked extraction of that value strictly against 97 its type. A similar design, offering more appropriate operators, 98 can be used for a generalized function adaptor, 99 <code class="computeroutput">any_function</code>, a generalized iterator adaptor, 100 <code class="computeroutput">any_iterator</code>, and other object types that need 101 uniform runtime treatment but support only compile-time template 102 parameter conformance.</p> 103</div> 104</div> 105<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 106<td align="left"></td> 107<td align="right"><div class="copyright-footer"></div></td> 108</tr></table> 109<hr> 110<div class="spirit-nav"> 111<a accesskey="p" href="align/history.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="any/s02.html"><img src="../../doc/src/images/next.png" alt="Next"></a> 112</div> 113</body> 114</html> 115