1 | <?xml version="1.0" encoding="utf-8" ?> |
---|
2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
---|
3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
---|
4 | <head> |
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
6 | <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> |
---|
7 | <title>pointee and indirect_reference</title> |
---|
8 | <meta name="author" content="David Abrahams" /> |
---|
9 | <meta name="organization" content="Boost Consulting" /> |
---|
10 | <meta name="date" content="2006-09-11" /> |
---|
11 | <meta name="copyright" content="Copyright David Abrahams 2004." /> |
---|
12 | <link rel="stylesheet" href="../../../rst.css" type="text/css" /> |
---|
13 | </head> |
---|
14 | <body> |
---|
15 | <div class="document" id="pointee-and-indirect-reference"> |
---|
16 | <h1 class="title"><tt class="docutils literal"><span class="pre">pointee</span></tt> and <tt class="docutils literal"><span class="pre">indirect_reference</span></tt></h1> |
---|
17 | <table class="docinfo" frame="void" rules="none"> |
---|
18 | <col class="docinfo-name" /> |
---|
19 | <col class="docinfo-content" /> |
---|
20 | <tbody valign="top"> |
---|
21 | <tr><th class="docinfo-name">Author:</th> |
---|
22 | <td>David Abrahams</td></tr> |
---|
23 | <tr><th class="docinfo-name">Contact:</th> |
---|
24 | <td><a class="first last reference external" href="mailto:dave@boost-consulting.com">dave@boost-consulting.com</a></td></tr> |
---|
25 | <tr><th class="docinfo-name">Organization:</th> |
---|
26 | <td><a class="first last reference external" href="http://www.boost-consulting.com">Boost Consulting</a></td></tr> |
---|
27 | <tr><th class="docinfo-name">Date:</th> |
---|
28 | <td>2006-09-11</td></tr> |
---|
29 | <tr><th class="docinfo-name">Copyright:</th> |
---|
30 | <td>Copyright David Abrahams 2004.</td></tr> |
---|
31 | </tbody> |
---|
32 | </table> |
---|
33 | <!-- Distributed under the Boost --> |
---|
34 | <!-- Software License, Version 1.0. (See accompanying --> |
---|
35 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
36 | <table class="docutils field-list" frame="void" rules="none"> |
---|
37 | <col class="field-name" /> |
---|
38 | <col class="field-body" /> |
---|
39 | <tbody valign="top"> |
---|
40 | <tr class="field"><th class="field-name">abstract:</th><td class="field-body">Provides the capability to deduce the referent types of |
---|
41 | pointers, smart pointers and iterators in generic code.</td> |
---|
42 | </tr> |
---|
43 | </tbody> |
---|
44 | </table> |
---|
45 | <div class="section" id="overview"> |
---|
46 | <h1>Overview</h1> |
---|
47 | <p>Have you ever wanted to write a generic function that can operate |
---|
48 | on any kind of dereferenceable object? If you have, you've |
---|
49 | probably run into the problem of how to determine the type that the |
---|
50 | object "points at":</p> |
---|
51 | <pre class="literal-block"> |
---|
52 | template <class Dereferenceable> |
---|
53 | void f(Dereferenceable p) |
---|
54 | { |
---|
55 | <em>what-goes-here?</em> value = *p; |
---|
56 | ... |
---|
57 | } |
---|
58 | </pre> |
---|
59 | <div class="section" id="pointee"> |
---|
60 | <h2><tt class="docutils literal"><span class="pre">pointee</span></tt></h2> |
---|
61 | <p>It turns out to be impossible to come up with a fully-general |
---|
62 | algorithm to do determine <em>what-goes-here</em> directly, but it is |
---|
63 | possible to require that <tt class="docutils literal"><span class="pre">pointee<Dereferenceable>::type</span></tt> is |
---|
64 | correct. Naturally, <tt class="docutils literal"><span class="pre">pointee</span></tt> has the same difficulty: it can't |
---|
65 | determine the appropriate <tt class="docutils literal"><span class="pre">::type</span></tt> reliably for all |
---|
66 | <tt class="docutils literal"><span class="pre">Dereferenceable</span></tt>s, but it makes very good guesses (it works |
---|
67 | for all pointers, standard and boost smart pointers, and |
---|
68 | iterators), and when it guesses wrongly, it can be specialized as |
---|
69 | necessary:</p> |
---|
70 | <pre class="literal-block"> |
---|
71 | namespace boost |
---|
72 | { |
---|
73 | template <class T> |
---|
74 | struct pointee<third_party_lib::smart_pointer<T> > |
---|
75 | { |
---|
76 | typedef T type; |
---|
77 | }; |
---|
78 | } |
---|
79 | </pre> |
---|
80 | </div> |
---|
81 | <div class="section" id="indirect-reference"> |
---|
82 | <h2><tt class="docutils literal"><span class="pre">indirect_reference</span></tt></h2> |
---|
83 | <p><tt class="docutils literal"><span class="pre">indirect_reference<T>::type</span></tt> is rather more specialized than |
---|
84 | <tt class="docutils literal"><span class="pre">pointee</span></tt>, and is meant to be used to forward the result of |
---|
85 | dereferencing an object of its argument type. Most dereferenceable |
---|
86 | types just return a reference to their pointee, but some return |
---|
87 | proxy references or return the pointee by value. When that |
---|
88 | information is needed, call on <tt class="docutils literal"><span class="pre">indirect_reference</span></tt>.</p> |
---|
89 | <p>Both of these templates are essential to the correct functioning of |
---|
90 | <a class="reference external" href="indirect_iterator.html"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt></a>.</p> |
---|
91 | </div> |
---|
92 | </div> |
---|
93 | <div class="section" id="reference"> |
---|
94 | <h1>Reference</h1> |
---|
95 | <div class="section" id="id1"> |
---|
96 | <h2><tt class="docutils literal"><span class="pre">pointee</span></tt></h2> |
---|
97 | <!-- Copyright David Abrahams 2004. Use, modification and distribution is --> |
---|
98 | <!-- subject to the Boost Software License, Version 1.0. (See accompanying --> |
---|
99 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
100 | <pre class="literal-block"> |
---|
101 | template <class Dereferenceable> |
---|
102 | struct pointee |
---|
103 | { |
---|
104 | typedef /* see below */ type; |
---|
105 | }; |
---|
106 | </pre> |
---|
107 | <table class="docutils field-list" frame="void" rules="none"> |
---|
108 | <col class="field-name" /> |
---|
109 | <col class="field-body" /> |
---|
110 | <tbody valign="top"> |
---|
111 | <tr class="field"><th class="field-name">Requires:</th><td class="field-body">For an object <tt class="docutils literal"><span class="pre">x</span></tt> of type <tt class="docutils literal"><span class="pre">Dereferenceable</span></tt>, <tt class="docutils literal"><span class="pre">*x</span></tt> |
---|
112 | is well-formed. If <tt class="docutils literal"><span class="pre">++x</span></tt> is ill-formed it shall neither be |
---|
113 | ambiguous nor shall it violate access control, and |
---|
114 | <tt class="docutils literal"><span class="pre">Dereferenceable::element_type</span></tt> shall be an accessible type. |
---|
115 | Otherwise <tt class="docutils literal"><span class="pre">iterator_traits<Dereferenceable>::value_type</span></tt> shall |
---|
116 | be well formed. [Note: These requirements need not apply to |
---|
117 | explicit or partial specializations of <tt class="docutils literal"><span class="pre">pointee</span></tt>]</td> |
---|
118 | </tr> |
---|
119 | </tbody> |
---|
120 | </table> |
---|
121 | <p><tt class="docutils literal"><span class="pre">type</span></tt> is determined according to the following algorithm, where |
---|
122 | <tt class="docutils literal"><span class="pre">x</span></tt> is an object of type <tt class="docutils literal"><span class="pre">Dereferenceable</span></tt>:</p> |
---|
123 | <pre class="literal-block"> |
---|
124 | if ( ++x is ill-formed ) |
---|
125 | { |
---|
126 | return ``Dereferenceable::element_type`` |
---|
127 | } |
---|
128 | else if (``*x`` is a mutable reference to |
---|
129 | std::iterator_traits<Dereferenceable>::value_type) |
---|
130 | { |
---|
131 | return iterator_traits<Dereferenceable>::value_type |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | return iterator_traits<Dereferenceable>::value_type const |
---|
136 | } |
---|
137 | </pre> |
---|
138 | </div> |
---|
139 | <div class="section" id="id2"> |
---|
140 | <h2><tt class="docutils literal"><span class="pre">indirect_reference</span></tt></h2> |
---|
141 | <!-- Copyright David Abrahams 2004. Use, modification and distribution is --> |
---|
142 | <!-- subject to the Boost Software License, Version 1.0. (See accompanying --> |
---|
143 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
144 | <pre class="literal-block"> |
---|
145 | template <class Dereferenceable> |
---|
146 | struct indirect_reference |
---|
147 | { |
---|
148 | typedef /* see below */ type; |
---|
149 | }; |
---|
150 | </pre> |
---|
151 | <table class="docutils field-list" frame="void" rules="none"> |
---|
152 | <col class="field-name" /> |
---|
153 | <col class="field-body" /> |
---|
154 | <tbody valign="top"> |
---|
155 | <tr class="field"><th class="field-name">Requires:</th><td class="field-body">For an object <tt class="docutils literal"><span class="pre">x</span></tt> of type <tt class="docutils literal"><span class="pre">Dereferenceable</span></tt>, <tt class="docutils literal"><span class="pre">*x</span></tt> |
---|
156 | is well-formed. If <tt class="docutils literal"><span class="pre">++x</span></tt> is ill-formed it shall neither be |
---|
157 | ambiguous nor shall it violate access control, and |
---|
158 | <tt class="docutils literal"><span class="pre">pointee<Dereferenceable>::type&</span></tt> shall be well-formed. |
---|
159 | Otherwise <tt class="docutils literal"><span class="pre">iterator_traits<Dereferenceable>::reference</span></tt> shall |
---|
160 | be well formed. [Note: These requirements need not apply to |
---|
161 | explicit or partial specializations of <tt class="docutils literal"><span class="pre">indirect_reference</span></tt>]</td> |
---|
162 | </tr> |
---|
163 | </tbody> |
---|
164 | </table> |
---|
165 | <p><tt class="docutils literal"><span class="pre">type</span></tt> is determined according to the following algorithm, where |
---|
166 | <tt class="docutils literal"><span class="pre">x</span></tt> is an object of type <tt class="docutils literal"><span class="pre">Dereferenceable</span></tt>:</p> |
---|
167 | <pre class="literal-block"> |
---|
168 | if ( ++x is ill-formed ) |
---|
169 | return ``pointee<Dereferenceable>::type&`` |
---|
170 | else |
---|
171 | std::iterator_traits<Dereferenceable>::reference |
---|
172 | </pre> |
---|
173 | </div> |
---|
174 | </div> |
---|
175 | </div> |
---|
176 | <div class="footer"> |
---|
177 | <hr class="footer" /> |
---|
178 | <a class="reference external" href="pointee.rst">View document source</a>. |
---|
179 | Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source. |
---|
180 | |
---|
181 | </div> |
---|
182 | </body> |
---|
183 | </html> |
---|