summaryrefslogtreecommitdiff
path: root/doc/html/boost_typeerasure.html
blob: 08060ce5b00cf4d0ac54072832d6c0d02a4c2835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter&#160;40.&#160;Boost.TypeErasure</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part&#160;I.&#160;The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="thread/compliance.html" title="Conformance and Extension">
<link rel="next" href="boost_typeerasure/reading.html" title="How to read this documentation">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="boost_typeerasure"></a>Chapter&#160;40.&#160;Boost.TypeErasure</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Steven</span> <span class="surname">Watanabe</span>
</h3></div></div>
<div><p class="copyright">Copyright &#169; 2011-2013 Steven Watanabe</p></div>
<div><div class="legalnotice">
<a name="boost_typeerasure.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="boost_typeerasure.html#boost_typeerasure.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reading.html">How to read this documentation</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/basic.html">Basic Usage</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/composing_concepts.html">Composing Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/multi.html">Functions with Multiple Arguments</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html">Concepts in Depth</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.custom">Defining Custom Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload">Overloading</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.concept_map">Concept Maps</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload0">Associated Types</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/any.html">Using Any</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.construction">Construction</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.conversions">Conversions</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.references">References</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.limit">Syntax Limitations</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/examples.html">Examples</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.print_sequence">A polymorphic
      range formatter</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.printf">A type-safe printf</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.multifunction">Boost.Function
      with multiple signatures</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/conceptdef.html">Concept Definitions</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/predef.html">Predefined Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html">Reference</a></span></dt>
<dd><dl>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/rationale.html">Rationale</a></span></dt>
<dd><dl>
<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
      do I have to specify the presence of a destructor explicitly?</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.why_non_member_functions_">Why
      non-member functions?</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.placeholder">Why are the
      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>
      <code class="computeroutput"><span class="identifier">_2</span></code></a></span></dt>
<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>
      for references?</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/future.html">Future Work</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/acknowledgements.html">Acknowledgements</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/related.html">Related Work</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeerasure.introduction"></a><a class="link" href="boost_typeerasure.html#boost_typeerasure.introduction" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      The Boost.TypeErasure library provides runtime polymorphism in C++ that is
      more flexible than that provided by the core language.
    </p>
<p>
      C++ has two distinct kinds of polymorphism, virtual functions and templates,
      each of which has its own advantages and disadvantages.
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Virtual functions are not resolved until runtime, while templates are always
          resolved at compile time. If your types can vary at runtime (for example,
          if they depend on user input), then static polymorphism with templates
          doesn't help much.
        </li>
<li class="listitem">
          Virtual functions can be used with separate compilation. The body of a
          template has to be available in every translation unit in which it is used,
          slowing down compiles and increasing rebuilds.
        </li>
<li class="listitem">
          Virtual functions automatically make the requirements on the arguments
          explicit. Templates are only checked when they're instantiated, requiring
          extra work in testing, assertions, and documentation.
        </li>
<li class="listitem">
          The compiler creates a new copy of each function template every time it
          is instantiated. This allows better optimization, because the compiler
          knows everything statically, but it also causes a significant increase
          of binary sizes.
        </li>
<li class="listitem">
          Templates support Value semantics. Objects that "behave like an int"
          and are not shared are easier to reason about. To use virtual functions,
          on the other hand, you have to use (smart) pointers or references.
        </li>
<li class="listitem">
          Template libraries can allow third-party types to be adapted non-intrusively
          for seamless interoperability. With virtual functions, you have to create
          a wrapper that inherits from the base class.
        </li>
<li class="listitem">
          Templates can handle constraints involving multiple types. For example,
          std::for_each takes an iterator range and a function that can be called
          on the elements of the range. Virtual functions aren't really able to express
          such constraints.
        </li>
</ul></div>
<p>
      The Boost.TypeErasure library combines the superior abstraction capabilities
      of templates, with the runtime flexibility of virtual functions.
    </p>
<p>
      Boost includes several special cases of this kind of polymorphism:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span></code> for CopyConstructible types.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span></code> for objects that can be called
          like functions.
        </li>
<li class="listitem">
          Boost.Range provides <code class="computeroutput"><span class="identifier">any_iterator</span></code>.
        </li>
</ul></div>
<p>
      Boost.TypeErasure generalizes this to support arbitrary requirements and provides
      a <a class="link" href="boost_typeerasure/predef.html" title="Predefined Concepts">predefined set of common concepts</a>
    </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: April 11, 2018 at 14:08:02 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>