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