summaryrefslogtreecommitdiff
path: root/doc/html/boost_typeerasure/rationale.html
blob: 8694172eb2be4f0b569f1c9a246394e5f40e7186 (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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Rationale</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="../boost_typeerasure.html" title="Chapter&#160;33.&#160;Boost.TypeErasure">
<link rel="prev" href="../boost/type_erasure/typeid_of.html" title="Function typeid_of">
<link rel="next" href="future.html" title="Future Work">
</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="../boost/type_erasure/typeid_of.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.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="future.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeerasure.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="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="rationale.html#boost_typeerasure.rationale.why_non_member_functions_">Why
      non-member functions?</a></span></dt>
<dt><span class="section"><a href="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="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></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_" title="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>
</h3></div></div></div>
<p>
        When using references the destructor isn't needed. By not assuming it implicitly,
        we allow capturing types with private or protected destructors by reference.
        For the sake of consistency, it must be specified when capturing by value
        as well.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.why_non_member_functions_"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.why_non_member_functions_" title="Why non-member functions?">Why
      non-member functions?</a>
</h3></div></div></div>
<p>
        The members of <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code>
        can be customized. By using free functions, we guarantee that we don't interfere
        with anything that a user might want.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.placeholder"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.placeholder" title="Why are the placeholders called _a, _b and not _1 _2">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>
</h3></div></div></div>
<p>
        An earlier version of the library used the names <code class="computeroutput"><span class="identifier">_1</span></code>,
        <code class="computeroutput"><span class="identifier">_2</span></code>, etc. instead of <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code>,
        etc. This caused a certain amount of confusion because the numbered placeholders
        are already used with a somewhat different meaning by several other libraries
        including Boost/Std Bind, Boost.Phoenix, and Boost.MPL. I eventually decided
        that since the placeholders represented named parameters instead of positional
        parameters, letters were more appropriate than numbers.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeerasure.rationale.ref"></a><a class="link" href="rationale.html#boost_typeerasure.rationale.ref" title="Why not use boost::ref for references?">Why not use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code>
      for references?</a>
</h3></div></div></div>
<p>
        Boost.Function allows you to use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code> to
        store a reference to a function object. However, in the general case treating
        references and values in the same way causes inconsistent behavior that is
        difficult to reason about. If Boost.TypeErasure handled references like this,
        then, when you copy an <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code>,
        you would have no idea whether the new object is a real copy or just a new
        reference to the same underlying object. Boost.Function can get away with
        it, because it doesn't expose any mutating operations on the stored function
        object.
      </p>
<p>
        Another method that has been proposed is only to keep a reference the first
        time.
      </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">any</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
<span class="identifier">any</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span> <span class="comment">// makes a copy</span>
</pre>
<p>
        Unfortunately, this doesn't handle all use cases, as there is no reliable
        way to return such a reference from a function. In addition it adds overhead
        whether it's needed or not, as we would have to add a flag to any to keep
        track of whether or not it is storing a reference. (The alternate method
        of storing this in the "<code class="computeroutput"><span class="identifier">clone</span></code>"
        method in the vtable is impossibly complex to implement given the decoupled
        vtables that Boost.TypeErasure uses and it still adds overhead.).
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Steven Watanabe<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></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost/type_erasure/typeid_of.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.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="future.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>