| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title>Chapter 1. Boost.Any</title> |
|---|
| 5 | <link rel="stylesheet" href="boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
|---|
| 7 | <link rel="start" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
|---|
| 8 | <link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)"> |
|---|
| 9 | <link rel="prev" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)"> |
|---|
| 10 | <link rel="next" href="any/s02.html" title="Examples"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="libraries.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="any/s02.html"><img src="images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="chapter" lang="en"> |
|---|
| 26 | <div class="titlepage"><div> |
|---|
| 27 | <div><h2 class="title"> |
|---|
| 28 | <a name="any"></a>Chapter 1. Boost.Any</h2></div> |
|---|
| 29 | <div><div class="author"><h3 class="author"> |
|---|
| 30 | <span class="firstname">Kevlin</span> <span class="surname">Henney</span> |
|---|
| 31 | </h3></div></div> |
|---|
| 32 | <div><p class="copyright">Copyright © 2001 Kevlin Henney</p></div> |
|---|
| 33 | </div></div> |
|---|
| 34 | <div class="toc"> |
|---|
| 35 | <p><b>Table of Contents</b></p> |
|---|
| 36 | <dl> |
|---|
| 37 | <dt><span class="section"><a href="any.html#id936676">Introduction</a></span></dt> |
|---|
| 38 | <dt><span class="section"><a href="any/s02.html">Examples</a></span></dt> |
|---|
| 39 | <dt><span class="section"><a href="any/reference.html">Reference</a></span></dt> |
|---|
| 40 | <dd><dl> |
|---|
| 41 | <dt><span class="section"><a href="any/reference.html#any.ValueType"><span class="emphasis"><em>ValueType</em></span> requirements</a></span></dt> |
|---|
| 42 | <dt><span class="section"><a href="any/reference.html#header.boost.any.hpp">Header <boost/any.hpp></a></span></dt> |
|---|
| 43 | </dl></dd> |
|---|
| 44 | <dt><span class="section"><a href="any/s04.html">Acknowledgements</a></span></dt> |
|---|
| 45 | </dl> |
|---|
| 46 | </div> |
|---|
| 47 | <div class="section" lang="en"> |
|---|
| 48 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|---|
| 49 | <a name="id936676"></a>Introduction</h2></div></div></div> |
|---|
| 50 | <p>There are times when a generic (in the sense of |
|---|
| 51 | <span class="emphasis"><em>general</em></span> as opposed to |
|---|
| 52 | <span class="emphasis"><em>template-based programming</em></span>) type is needed: |
|---|
| 53 | variables that are truly variable, accommodating values of many |
|---|
| 54 | other more specific types rather than C++'s normal strict and |
|---|
| 55 | static types. We can distinguish three basic kinds of generic |
|---|
| 56 | type:</p> |
|---|
| 57 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 58 | <li><p>Converting types that can hold one of a number of |
|---|
| 59 | possible value types, e.g. <code class="computeroutput">int</code> and |
|---|
| 60 | <code class="computeroutput">string</code>, and freely convert between them, for |
|---|
| 61 | instance interpreting <code class="computeroutput">5</code> as <code class="computeroutput">"5"</code> or |
|---|
| 62 | vice-versa. Such types are common in scripting and other |
|---|
| 63 | interpreted |
|---|
| 64 | languages. |
|---|
| 65 | <code class="computeroutput">boost::lexical_cast</code> |
|---|
| 66 | supports such conversion functionality.</p></li> |
|---|
| 67 | <li><p> |
|---|
| 68 | Discriminated types that contain values of different types but |
|---|
| 69 | do not attempt conversion between them, i.e. <code class="computeroutput">5</code> is |
|---|
| 70 | held strictly as an <code class="computeroutput">int</code> and is not implicitly |
|---|
| 71 | convertible either to <code class="computeroutput">"5"</code> or to |
|---|
| 72 | <code class="computeroutput">5.0</code>. Their indifference to interpretation but |
|---|
| 73 | awareness of type effectively makes them safe, generic |
|---|
| 74 | containers of single values, with no scope for surprises from |
|---|
| 75 | ambiguous conversions.</p></li> |
|---|
| 76 | <li><p> |
|---|
| 77 | Indiscriminate types that can refer to anything but are |
|---|
| 78 | oblivious to the actual underlying type, entrusting all forms |
|---|
| 79 | of access and interpretation to the programmer. This niche is |
|---|
| 80 | dominated by <code class="computeroutput">void *</code>, which offers plenty of scope |
|---|
| 81 | for surprising, undefined behavior.</p></li> |
|---|
| 82 | </ul></div> |
|---|
| 83 | <p>The <code class="computeroutput"><a href="boost/any.html" title="Class any">boost::any</a></code> class |
|---|
| 84 | (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 |
|---|
| 85 | Conversions"</a> by Kevlin Henney, <span class="emphasis"><em>C++ |
|---|
| 86 | Report</em></span> 12(7), July/August 2000) is a variant value type |
|---|
| 87 | based on the second category. It supports copying of any value |
|---|
| 88 | type and safe checked extraction of that value strictly against |
|---|
| 89 | its type. A similar design, offering more appropriate operators, |
|---|
| 90 | can be used for a generalized function adaptor, |
|---|
| 91 | <code class="computeroutput">any_function</code>, a generalized iterator adaptor, |
|---|
| 92 | <code class="computeroutput">any_iterator</code>, and other object types that need |
|---|
| 93 | uniform runtime treatment but support only compile-time template |
|---|
| 94 | parameter conformance.</p> |
|---|
| 95 | </div> |
|---|
| 96 | </div> |
|---|
| 97 | <table width="100%"><tr> |
|---|
| 98 | <td align="left"><small><p>Last revised: November 04, 2005 at 09:34:36 GMT</p></small></td> |
|---|
| 99 | <td align="right"><small></small></td> |
|---|
| 100 | </tr></table> |
|---|
| 101 | <hr> |
|---|
| 102 | <div class="spirit-nav"> |
|---|
| 103 | <a accesskey="p" href="libraries.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="any/s02.html"><img src="images/next.png" alt="Next"></a> |
|---|
| 104 | </div> |
|---|
| 105 | </body> |
|---|
| 106 | </html> |
|---|