Imported Upstream version 1.72.0
[platform/upstream/boost.git] / doc / html / boost_typeerasure.html
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=US-ASCII">
5 <title>Chapter&#160;40.&#160;Boost.TypeErasure</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&#160;I.&#160;The Boost C++ Libraries (BoostBook Subset)">
10 <link rel="prev" href="thread/compliance.html" title="Conformance and Extension">
11 <link rel="next" href="boost_typeerasure/reading.html" title="How to read this documentation">
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="thread/compliance.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="boost_typeerasure/reading.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="boost_typeerasure"></a>Chapter&#160;40.&#160;Boost.TypeErasure</h2></div>
30 <div><div class="author"><h3 class="author">
31 <span class="firstname">Steven</span> <span class="surname">Watanabe</span>
32 </h3></div></div>
33 <div><p class="copyright">Copyright &#169; 2011-2013 Steven Watanabe</p></div>
34 <div><div class="legalnotice">
35 <a name="boost_typeerasure.legal"></a><p>
36         Distributed under the Boost Software License, Version 1.0. (See accompanying
37         file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
38       </p>
39 </div></div>
40 </div></div>
41 <div class="toc">
42 <p><b>Table of Contents</b></p>
43 <dl class="toc">
44 <dt><span class="section"><a href="boost_typeerasure.html#boost_typeerasure.introduction">Introduction</a></span></dt>
45 <dt><span class="section"><a href="boost_typeerasure/reading.html">How to read this documentation</a></span></dt>
46 <dt><span class="section"><a href="boost_typeerasure/basic.html">Basic Usage</a></span></dt>
47 <dt><span class="section"><a href="boost_typeerasure/composing_concepts.html">Composing Concepts</a></span></dt>
48 <dt><span class="section"><a href="boost_typeerasure/multi.html">Functions with Multiple Arguments</a></span></dt>
49 <dt><span class="section"><a href="boost_typeerasure/concept.html">Concepts in Depth</a></span></dt>
50 <dd><dl>
51 <dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.custom">Defining Custom Concepts</a></span></dt>
52 <dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload">Overloading</a></span></dt>
53 <dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.concept_map">Concept Maps</a></span></dt>
54 <dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload0">Associated Types</a></span></dt>
55 </dl></dd>
56 <dt><span class="section"><a href="boost_typeerasure/any.html">Using Any</a></span></dt>
57 <dd><dl>
58 <dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.construction">Construction</a></span></dt>
59 <dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.conversions">Conversions</a></span></dt>
60 <dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.references">References</a></span></dt>
61 <dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.limit">Syntax Limitations</a></span></dt>
62 </dl></dd>
63 <dt><span class="section"><a href="boost_typeerasure/examples.html">Examples</a></span></dt>
64 <dd><dl>
65 <dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.print_sequence">A polymorphic
66       range formatter</a></span></dt>
67 <dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.printf">A type-safe printf</a></span></dt>
68 <dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.multifunction">Boost.Function
69       with multiple signatures</a></span></dt>
70 </dl></dd>
71 <dt><span class="section"><a href="boost_typeerasure/conceptdef.html">Concept Definitions</a></span></dt>
72 <dt><span class="section"><a href="boost_typeerasure/predef.html">Predefined Concepts</a></span></dt>
73 <dt><span class="section"><a href="boost_typeerasure/reference.html">Reference</a></span></dt>
74 <dd><dl>
75 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.any_hpp">Header &lt;boost/type_erasure/any.hpp&gt;</a></span></dt>
76 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.any_cast_hpp">Header &lt;boost/type_erasure/any_cast.hpp&gt;</a></span></dt>
77 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.binding_hpp">Header &lt;boost/type_erasure/binding.hpp&gt;</a></span></dt>
78 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.binding_of_hpp">Header &lt;boost/type_erasure/binding_of.hpp&gt;</a></span></dt>
79 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.builtin_hpp">Header &lt;boost/type_erasure/builtin.hpp&gt;</a></span></dt>
80 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.call_hpp">Header &lt;boost/type_erasure/call.hpp&gt;</a></span></dt>
81 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.callable_hpp">Header &lt;boost/type_erasure/callable.hpp&gt;</a></span></dt>
82 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.check_match_hpp">Header &lt;boost/type_erasure/check_match.hpp&gt;</a></span></dt>
83 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.concept_interface_hpp">Header &lt;boost/type_erasure/concept_interface.hpp&gt;</a></span></dt>
84 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.concept_of_hpp">Header &lt;boost/type_erasure/concept_of.hpp&gt;</a></span></dt>
85 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.config_hpp">Header &lt;boost/type_erasure/config.hpp&gt;</a></span></dt>
86 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.constructible_hpp">Header &lt;boost/type_erasure/constructible.hpp&gt;</a></span></dt>
87 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.deduced_hpp">Header &lt;boost/type_erasure/deduced.hpp&gt;</a></span></dt>
88 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.derived_hpp">Header &lt;boost/type_erasure/derived.hpp&gt;</a></span></dt>
89 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.dynamic_any_cast_hpp">Header &lt;boost/type_erasure/dynamic_any_cast.hpp&gt;</a></span></dt>
90 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.dynamic_binding_hpp">Header &lt;boost/type_erasure/dynamic_binding.hpp&gt;</a></span></dt>
91 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.exception_hpp">Header &lt;boost/type_erasure/exception.hpp&gt;</a></span></dt>
92 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.free_hpp">Header &lt;boost/type_erasure/free.hpp&gt;</a></span></dt>
93 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_empty_hpp">Header &lt;boost/type_erasure/is_empty.hpp&gt;</a></span></dt>
94 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_placeholder_hpp">Header &lt;boost/type_erasure/is_placeholder.hpp&gt;</a></span></dt>
95 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_subconcept_hpp">Header &lt;boost/type_erasure/is_subconcept.hpp&gt;</a></span></dt>
96 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.iterator_hpp">Header &lt;boost/type_erasure/iterator.hpp&gt;</a></span></dt>
97 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.member_hpp">Header &lt;boost/type_erasure/member.hpp&gt;</a></span></dt>
98 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.operators_hpp">Header &lt;boost/type_erasure/operators.hpp&gt;</a></span></dt>
99 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.param_hpp">Header &lt;boost/type_erasure/param.hpp&gt;</a></span></dt>
100 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.placeholder_hpp">Header &lt;boost/type_erasure/placeholder.hpp&gt;</a></span></dt>
101 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.placeholder_of_hpp">Header &lt;boost/type_erasure/placeholder_of.hpp&gt;</a></span></dt>
102 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.rebind_any_hpp">Header &lt;boost/type_erasure/rebind_any.hpp&gt;</a></span></dt>
103 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.register_binding_hpp">Header &lt;boost/type_erasure/register_binding.hpp&gt;</a></span></dt>
104 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.relaxed_hpp">Header &lt;boost/type_erasure/relaxed.hpp&gt;</a></span></dt>
105 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.require_match_hpp">Header &lt;boost/type_erasure/require_match.hpp&gt;</a></span></dt>
106 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.same_type_hpp">Header &lt;boost/type_erasure/same_type.hpp&gt;</a></span></dt>
107 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.static_binding_hpp">Header &lt;boost/type_erasure/static_binding.hpp&gt;</a></span></dt>
108 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.tuple_hpp">Header &lt;boost/type_erasure/tuple.hpp&gt;</a></span></dt>
109 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.typeid_of_hpp">Header &lt;boost/type_erasure/typeid_of.hpp&gt;</a></span></dt>
110 </dl></dd>
111 <dt><span class="section"><a href="boost_typeerasure/rationale.html">Rationale</a></span></dt>
112 <dd><dl>
113 <dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_">Why
114       do I have to specify the presence of a destructor explicitly?</a></span></dt>
115 <dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.why_non_member_functions_">Why
116       non-member functions?</a></span></dt>
117 <dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.placeholder">Why are the
118       placeholders called <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code> and not <code class="computeroutput"><span class="identifier">_1</span></code>
119       <code class="computeroutput"><span class="identifier">_2</span></code></a></span></dt>
120 <dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.ref">Why not use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code>
121       for references?</a></span></dt>
122 </dl></dd>
123 <dt><span class="section"><a href="boost_typeerasure/future.html">Future Work</a></span></dt>
124 <dt><span class="section"><a href="boost_typeerasure/acknowledgements.html">Acknowledgements</a></span></dt>
125 <dt><span class="section"><a href="boost_typeerasure/related.html">Related Work</a></span></dt>
126 </dl>
127 </div>
128 <div class="section">
129 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
130 <a name="boost_typeerasure.introduction"></a><a class="link" href="boost_typeerasure.html#boost_typeerasure.introduction" title="Introduction">Introduction</a>
131 </h2></div></div></div>
132 <p>
133       The Boost.TypeErasure library provides runtime polymorphism in C++ that is
134       more flexible than that provided by the core language.
135     </p>
136 <p>
137       C++ has two distinct kinds of polymorphism, virtual functions and templates,
138       each of which has its own advantages and disadvantages.
139     </p>
140 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
141 <li class="listitem">
142           Virtual functions are not resolved until runtime, while templates are always
143           resolved at compile time. If your types can vary at runtime (for example,
144           if they depend on user input), then static polymorphism with templates
145           doesn't help much.
146         </li>
147 <li class="listitem">
148           Virtual functions can be used with separate compilation. The body of a
149           template has to be available in every translation unit in which it is used,
150           slowing down compiles and increasing rebuilds.
151         </li>
152 <li class="listitem">
153           Virtual functions automatically make the requirements on the arguments
154           explicit. Templates are only checked when they're instantiated, requiring
155           extra work in testing, assertions, and documentation.
156         </li>
157 <li class="listitem">
158           The compiler creates a new copy of each function template every time it
159           is instantiated. This allows better optimization, because the compiler
160           knows everything statically, but it also causes a significant increase
161           of binary sizes.
162         </li>
163 <li class="listitem">
164           Templates support Value semantics. Objects that "behave like an int"
165           and are not shared are easier to reason about. To use virtual functions,
166           on the other hand, you have to use (smart) pointers or references.
167         </li>
168 <li class="listitem">
169           Template libraries can allow third-party types to be adapted non-intrusively
170           for seamless interoperability. With virtual functions, you have to create
171           a wrapper that inherits from the base class.
172         </li>
173 <li class="listitem">
174           Templates can handle constraints involving multiple types. For example,
175           std::for_each takes an iterator range and a function that can be called
176           on the elements of the range. Virtual functions aren't really able to express
177           such constraints.
178         </li>
179 </ul></div>
180 <p>
181       The Boost.TypeErasure library combines the superior abstraction capabilities
182       of templates, with the runtime flexibility of virtual functions.
183     </p>
184 <p>
185       Boost includes several special cases of this kind of polymorphism:
186     </p>
187 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
188 <li class="listitem">
189           <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span></code> for CopyConstructible types.
190         </li>
191 <li class="listitem">
192           <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span></code> for objects that can be called
193           like functions.
194         </li>
195 <li class="listitem">
196           Boost.Range provides <code class="computeroutput"><span class="identifier">any_iterator</span></code>.
197         </li>
198 </ul></div>
199 <p>
200       Boost.TypeErasure generalizes this to support arbitrary requirements and provides
201       a <a class="link" href="boost_typeerasure/predef.html" title="Predefined Concepts">predefined set of common concepts</a>
202     </p>
203 </div>
204 </div>
205 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
206 <td align="left"><p><small>Last revised: December 10, 2019 at 00:26:26 GMT</small></p></td>
207 <td align="right"><div class="copyright-footer"></div></td>
208 </tr></table>
209 <hr>
210 <div class="spirit-nav">
211 <a accesskey="p" href="thread/compliance.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="boost_typeerasure/reading.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
212 </div>
213 </body>
214 </html>