| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> |
| <title>Chapter 33. Boost.TypeErasure</title> |
| <link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css"> |
| <meta name="generator" content="DocBook XSL Stylesheets V1.78.1"> |
| <link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
| <link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)"> |
| <link rel="prev" href="tribool/tests.html" title="Testsuite"> |
| <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="tribool/tests.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 33. 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 © 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 <boost/type_erasure/any.hpp></a></span></dt> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| <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> |
| </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 07, 2015 at 22:51:18 GMT</small></p></td> |
| <td align="right"><div class="copyright-footer"></div></td> |
| </tr></table> |
| <hr> |
| <div class="spirit-nav"> |
| <a accesskey="p" href="tribool/tests.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> |