1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5 <title>Chapter 40. 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 I. 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">
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>
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>
27 <div class="titlepage"><div>
28 <div><h2 class="title">
29 <a name="boost_typeerasure"></a>Chapter 40. Boost.TypeErasure</h2></div>
30 <div><div class="author"><h3 class="author">
31 <span class="firstname">Steven</span> <span class="surname">Watanabe</span>
33 <div><p class="copyright">Copyright © 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>)
42 <p><b>Table of Contents</b></p>
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>
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>
56 <dt><span class="section"><a href="boost_typeerasure/any.html">Using Any</a></span></dt>
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>
63 <dt><span class="section"><a href="boost_typeerasure/examples.html">Examples</a></span></dt>
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>
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>
75 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.any_hpp">Header <boost/type_erasure/any.hpp></a></span></dt>
76 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.any_cast_hpp">Header <boost/type_erasure/any_cast.hpp></a></span></dt>
77 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.binding_hpp">Header <boost/type_erasure/binding.hpp></a></span></dt>
78 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.binding_of_hpp">Header <boost/type_erasure/binding_of.hpp></a></span></dt>
79 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.builtin_hpp">Header <boost/type_erasure/builtin.hpp></a></span></dt>
80 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.call_hpp">Header <boost/type_erasure/call.hpp></a></span></dt>
81 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.callable_hpp">Header <boost/type_erasure/callable.hpp></a></span></dt>
82 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.check_match_hpp">Header <boost/type_erasure/check_match.hpp></a></span></dt>
83 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.concept_interface_hpp">Header <boost/type_erasure/concept_interface.hpp></a></span></dt>
84 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.concept_of_hpp">Header <boost/type_erasure/concept_of.hpp></a></span></dt>
85 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.config_hpp">Header <boost/type_erasure/config.hpp></a></span></dt>
86 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.constructible_hpp">Header <boost/type_erasure/constructible.hpp></a></span></dt>
87 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.deduced_hpp">Header <boost/type_erasure/deduced.hpp></a></span></dt>
88 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.derived_hpp">Header <boost/type_erasure/derived.hpp></a></span></dt>
89 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.dynamic_any_cast_hpp">Header <boost/type_erasure/dynamic_any_cast.hpp></a></span></dt>
90 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.dynamic_binding_hpp">Header <boost/type_erasure/dynamic_binding.hpp></a></span></dt>
91 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.exception_hpp">Header <boost/type_erasure/exception.hpp></a></span></dt>
92 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.free_hpp">Header <boost/type_erasure/free.hpp></a></span></dt>
93 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_empty_hpp">Header <boost/type_erasure/is_empty.hpp></a></span></dt>
94 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_placeholder_hpp">Header <boost/type_erasure/is_placeholder.hpp></a></span></dt>
95 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.is_subconcept_hpp">Header <boost/type_erasure/is_subconcept.hpp></a></span></dt>
96 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.iterator_hpp">Header <boost/type_erasure/iterator.hpp></a></span></dt>
97 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.member_hpp">Header <boost/type_erasure/member.hpp></a></span></dt>
98 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.operators_hpp">Header <boost/type_erasure/operators.hpp></a></span></dt>
99 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.param_hpp">Header <boost/type_erasure/param.hpp></a></span></dt>
100 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.placeholder_hpp">Header <boost/type_erasure/placeholder.hpp></a></span></dt>
101 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.placeholder_of_hpp">Header <boost/type_erasure/placeholder_of.hpp></a></span></dt>
102 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.rebind_any_hpp">Header <boost/type_erasure/rebind_any.hpp></a></span></dt>
103 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.register_binding_hpp">Header <boost/type_erasure/register_binding.hpp></a></span></dt>
104 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.relaxed_hpp">Header <boost/type_erasure/relaxed.hpp></a></span></dt>
105 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.require_match_hpp">Header <boost/type_erasure/require_match.hpp></a></span></dt>
106 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.same_type_hpp">Header <boost/type_erasure/same_type.hpp></a></span></dt>
107 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.static_binding_hpp">Header <boost/type_erasure/static_binding.hpp></a></span></dt>
108 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.tuple_hpp">Header <boost/type_erasure/tuple.hpp></a></span></dt>
109 <dt><span class="section"><a href="boost_typeerasure/reference.html#header.boost.type_erasure.typeid_of_hpp">Header <boost/type_erasure/typeid_of.hpp></a></span></dt>
111 <dt><span class="section"><a href="boost_typeerasure/rationale.html">Rationale</a></span></dt>
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>
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>
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>
133 The Boost.TypeErasure library provides runtime polymorphism in C++ that is
134 more flexible than that provided by the core language.
137 C++ has two distinct kinds of polymorphism, virtual functions and templates,
138 each of which has its own advantages and disadvantages.
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
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.
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.
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
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.
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.
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
181 The Boost.TypeErasure library combines the superior abstraction capabilities
182 of templates, with the runtime flexibility of virtual functions.
185 Boost includes several special cases of this kind of polymorphism:
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.
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
195 <li class="listitem">
196 Boost.Range provides <code class="computeroutput"><span class="identifier">any_iterator</span></code>.
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>
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>
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>