Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/lambda/extending.html @ 25

Last change on this file since 25 was 12, checked in by landauf, 17 years ago

added boost

File size: 14.5 KB
RevLine 
[12]1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>Extending return type deduction system</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries">
8<link rel="up" href="../lambda.html" title="Chapter 6. Boost.Lambda">
9<link rel="prev" href="le_in_details.html" title="Lambda expressions in details">
10<link rel="next" href="s07.html" title="Practical considerations">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%">
14<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td>
15<td align="center"><a href="../../../index.htm">Home</a></td>
16<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="../../../people/people.htm">People</a></td>
18<td align="center"><a href="../../../more/faq.htm">FAQ</a></td>
19<td align="center"><a href="../../../more/index.htm">More</a></td>
20</table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="le_in_details.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s07.html"><img src="../images/next.png" alt="Next"></a>
24</div>
25<div class="section" lang="en">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="lambda.extending"></a>Extending return type deduction system</h3></div></div></div>
28<p>
29
30In this section, we explain  how to extend the return type deduction system
31to cover user defined operators.
32
33In many cases this is not necessary,
34as the BLL defines default return types for operators.
35
36For example, the default return type for all comparison operators is
37<code class="literal">bool</code>, and as long as the user defined comparison operators
38have a bool return type, there is no need to write new specializations
39for the return type deduction classes.
40
41Sometimes this cannot be avoided, though.
42
43</p>
44<p>
45The overloadable user defined operators are either unary or binary.
46
47For each arity, there are two traits templates that define the
48return types of the different operators.
49
50Hence, the return type system can be extended by providing more
51specializations for these templates.
52
53The templates for unary functors are
54
55<code class="literal">
56plain_return_type_1&lt;Action, A&gt;
57</code>
58
59and
60
61<code class="literal">
62return_type_1&lt;Action, A&gt;
63</code>, and
64
65<code class="literal">
66plain_return_type_2&lt;Action, A, B&gt;
67</code>
68
69and
70
71<code class="literal">
72return_type_2&lt;Action, A, B&gt;
73</code>
74
75respectively for binary functors.
76
77</p>
78<p>
79The first parameter (<code class="literal">Action</code>) to all these templates
80is the <span class="emphasis"><em>action</em></span> class, which specifies the operator.
81
82Operators with similar return type rules are grouped together into
83<span class="emphasis"><em>action groups</em></span>,
84and only the action class and action group together define the operator
85unambiguously.
86
87As an example, the action type
88<code class="literal">arithmetic_action&lt;plus_action&gt;</code> stands for
89<code class="literal">operator+</code>.
90
91The complete listing of different action types is shown in
92<a href="extending.html#table:actions" title="Table 6.2. Action types">Table 6.2, &#8220;Action types&#8221;</a>.
93</p>
94<p>
95The latter parameters, <code class="literal">A</code> in the unary case,
96or <code class="literal">A</code> and <code class="literal">B</code> in the binary case,
97stand for the argument types of the operator call.
98
99The two sets of templates,
100<code class="literal">plain_return_type_<em class="parameter"><code>n</code></em></code> and
101<code class="literal">return_type_<em class="parameter"><code>n</code></em></code> 
102(<em class="parameter"><code>n</code></em> is 1 or 2) differ in the way how parameter types
103are presented to them.
104
105For the former templates, the parameter types are always provided as
106non-reference types, and do not have const or volatile qualifiers.
107
108This makes specializing easy, as commonly one specialization for each
109user defined operator, or operator group, is enough.
110
111On the other hand, if a particular operator is overloaded for different
112cv-qualifications of the same argument types,
113and the return types of these overloaded versions differ, a more fine-grained control is needed.
114
115Hence, for the latter templates, the parameter types preserve the
116cv-qualifiers, and are non-reference types as well.
117 
118The downside is, that for an overloaded set of operators of the
119kind described above, one may end up needing up to
12016 <code class="literal">return_type_2</code> specializations.
121</p>
122<p>
123Suppose the user has overloaded the following operators for some user defined
124types <code class="literal">X</code>, <code class="literal">Y</code> and <code class="literal">Z</code>:
125
126</p>
127<pre class="programlisting">
128Z operator+(const X&amp;, const Y&amp;);
129Z operator-(const X&amp;, const Y&amp;);
130</pre>
131<p>
132
133Now, one can add a specialization stating, that if the left hand argument
134is of type <code class="literal">X</code>, and the right hand one of type
135<code class="literal">Y</code>, the return type of all such binary arithmetic
136operators is <code class="literal">Z</code>:
137
138</p>
139<pre class="programlisting">
140namespace boost {
141namespace lambda {
142 
143template&lt;class Act&gt; 
144struct plain_return_type_2&lt;arithmetic_action&lt;Act&gt;, X, Y&gt; {
145  typedef Z type;
146};
147
148}
149}
150</pre>
151<p>
152
153Having this specialization defined, BLL is capable of correctly
154deducing the return type of the above two operators.
155
156Note, that the specializations must be in the same namespace,
157<code class="literal">::boost::lambda</code>, with the primary template.
158
159For brevity, we do not show the namespace definitions in the examples below.
160</p>
161<p>
162It is possible to specialize on the level of an individual operator as well,
163in addition to providing a specialization for a group of operators.
164Say, we add a new arithmetic operator for argument types <code class="literal">X</code> 
165and <code class="literal">Y</code>:
166
167</p>
168<pre class="programlisting">
169X operator*(const X&amp;, const Y&amp;);
170</pre>
171<p>
172
173Our first rule for all arithmetic operators specifies that the return
174type of this operator is <code class="literal">Z</code>,
175which obviously is not the case.
176Hence, we provide a new rule for the multiplication operator:
177
178</p>
179<pre class="programlisting">
180template&lt;&gt; 
181struct plain_return_type_2&lt;arithmetic_action&lt;multiply_action&gt;, X, Y&gt; {
182  typedef X type;
183};
184</pre>
185<p>
186The specializations can define arbitrary mappings from the argument types
187to the return type.
188
189Suppose we have some mathematical vector type, templated on the element type:
190
191</p>
192<pre class="programlisting">
193template &lt;class T&gt; class my_vector;
194</pre>
195<p>
196
197Suppose the addition operator is defined between any two
198<code class="literal">my_vector</code> instantiations,
199as long as the addition operator is defined between their element types.
200
201Furthermore, the element type of the resulting <code class="literal">my_vector</code> 
202is the same as the result type of the addition between the element types.
203
204E.g., adding <code class="literal">my_vector&lt;int&gt;</code> and
205<code class="literal">my_vector&lt;double&gt;</code> results in
206<code class="literal">my_vector&lt;double&gt;</code>.
207
208The BLL has traits classes to perform the implicit built-in and standard
209type conversions between integral, floating point, and complex classes.
210
211Using BLL tools, the addition operator described above can be defined as:
212
213</p>
214<pre class="programlisting">
215template&lt;class A, class B&gt; 
216my_vector&lt;typename return_type_2&lt;arithmetic_action&lt;plus_action&gt;, A, B&gt;::type&gt;
217operator+(const my_vector&lt;A&gt;&amp; a, const my_vector&lt;B&gt;&amp; b)
218{
219  typedef typename
220    return_type_2&lt;arithmetic_action&lt;plus_action&gt;, A, B&gt;::type res_type;
221  return my_vector&lt;res_type&gt;();
222}
223</pre>
224<p>
225To allow BLL to deduce the type of <code class="literal">my_vector</code> 
226additions correctly, we can define:
227
228</p>
229<pre class="programlisting">
230template&lt;class A, class B&gt; 
231class plain_return_type_2&lt;arithmetic_action&lt;plus_action&gt;,
232                           my_vector&lt;A&gt;, my_vector&lt;B&gt; &gt; {
233  typedef typename
234    return_type_2&lt;arithmetic_action&lt;plus_action&gt;, A, B&gt;::type res_type;
235public:
236  typedef my_vector&lt;res_type&gt; type;
237};
238</pre>
239<p>
240Note, that we are reusing the existing specializations for the
241BLL <code class="literal">return_type_2</code> template,
242which require that the argument types are references.
243</p>
244<div class="table">
245<a name="table:actions"></a><p class="title"><b>Table 6.2. Action types</b></p>
246<table class="table" summary="Action types">
247<colgroup>
248<col>
249<col>
250</colgroup>
251<tbody>
252<tr>
253<td><code class="literal">+</code></td>
254<td><code class="literal">arithmetic_action&lt;plus_action&gt;</code></td>
255</tr>
256<tr>
257<td><code class="literal">-</code></td>
258<td><code class="literal">arithmetic_action&lt;minus_action&gt;</code></td>
259</tr>
260<tr>
261<td><code class="literal">*</code></td>
262<td><code class="literal">arithmetic_action&lt;multiply_action&gt;</code></td>
263</tr>
264<tr>
265<td><code class="literal">/</code></td>
266<td><code class="literal">arithmetic_action&lt;divide_action&gt;</code></td>
267</tr>
268<tr>
269<td><code class="literal">%</code></td>
270<td><code class="literal">arithmetic_action&lt;remainder_action&gt;</code></td>
271</tr>
272<tr>
273<td><code class="literal">+</code></td>
274<td><code class="literal">unary_arithmetic_action&lt;plus_action&gt;</code></td>
275</tr>
276<tr>
277<td><code class="literal">-</code></td>
278<td><code class="literal">unary_arithmetic_action&lt;minus_action&gt;</code></td>
279</tr>
280<tr>
281<td><code class="literal">&amp;</code></td>
282<td><code class="literal">bitwise_action&lt;and_action&gt;</code></td>
283</tr>
284<tr>
285<td><code class="literal">|</code></td>
286<td><code class="literal">bitwise_action&lt;or_action&gt;</code></td>
287</tr>
288<tr>
289<td><code class="literal">~</code></td>
290<td><code class="literal">bitwise_action&lt;not_action&gt;</code></td>
291</tr>
292<tr>
293<td><code class="literal">^</code></td>
294<td><code class="literal">bitwise_action&lt;xor_action&gt;</code></td>
295</tr>
296<tr>
297<td><code class="literal">&lt;&lt;</code></td>
298<td><code class="literal">bitwise_action&lt;leftshift_action_no_stream&gt;</code></td>
299</tr>
300<tr>
301<td><code class="literal">&gt;&gt;</code></td>
302<td><code class="literal">bitwise_action&lt;rightshift_action_no_stream&gt;</code></td>
303</tr>
304<tr>
305<td><code class="literal">&amp;&amp;</code></td>
306<td><code class="literal">logical_action&lt;and_action&gt;</code></td>
307</tr>
308<tr>
309<td><code class="literal">||</code></td>
310<td><code class="literal">logical_action&lt;or_action&gt;</code></td>
311</tr>
312<tr>
313<td><code class="literal">!</code></td>
314<td><code class="literal">logical_action&lt;not_action&gt;</code></td>
315</tr>
316<tr>
317<td><code class="literal">&lt;</code></td>
318<td><code class="literal">relational_action&lt;less_action&gt;</code></td>
319</tr>
320<tr>
321<td><code class="literal">&gt;</code></td>
322<td><code class="literal">relational_action&lt;greater_action&gt;</code></td>
323</tr>
324<tr>
325<td><code class="literal">&lt;=</code></td>
326<td><code class="literal">relational_action&lt;lessorequal_action&gt;</code></td>
327</tr>
328<tr>
329<td><code class="literal">&gt;=</code></td>
330<td><code class="literal">relational_action&lt;greaterorequal_action&gt;</code></td>
331</tr>
332<tr>
333<td><code class="literal">==</code></td>
334<td><code class="literal">relational_action&lt;equal_action&gt;</code></td>
335</tr>
336<tr>
337<td><code class="literal">!=</code></td>
338<td><code class="literal">relational_action&lt;notequal_action&gt;</code></td>
339</tr>
340<tr>
341<td><code class="literal">+=</code></td>
342<td><code class="literal">arithmetic_assignment_action&lt;plus_action&gt;</code></td>
343</tr>
344<tr>
345<td><code class="literal">-=</code></td>
346<td><code class="literal">arithmetic_assignment_action&lt;minus_action&gt;</code></td>
347</tr>
348<tr>
349<td><code class="literal">*=</code></td>
350<td><code class="literal">arithmetic_assignment_action&lt;multiply_action&gt;</code></td>
351</tr>
352<tr>
353<td><code class="literal">/=</code></td>
354<td><code class="literal">arithmetic_assignment_action&lt;divide_action&gt;</code></td>
355</tr>
356<tr>
357<td><code class="literal">%=</code></td>
358<td><code class="literal">arithmetic_assignment_action&lt;remainder_action&gt;</code></td>
359</tr>
360<tr>
361<td><code class="literal">&amp;=</code></td>
362<td><code class="literal">bitwise_assignment_action&lt;and_action&gt;</code></td>
363</tr>
364<tr>
365<td><code class="literal">=|</code></td>
366<td><code class="literal">bitwise_assignment_action&lt;or_action&gt;</code></td>
367</tr>
368<tr>
369<td><code class="literal">^=</code></td>
370<td><code class="literal">bitwise_assignment_action&lt;xor_action&gt;</code></td>
371</tr>
372<tr>
373<td><code class="literal">&lt;&lt;=</code></td>
374<td><code class="literal">bitwise_assignment_action&lt;leftshift_action&gt;</code></td>
375</tr>
376<tr>
377<td><code class="literal">&gt;&gt;=</code></td>
378<td><code class="literal">bitwise_assignment_action&lt;rightshift_action&gt;</code></td>
379</tr>
380<tr>
381<td><code class="literal">++</code></td>
382<td><code class="literal">pre_increment_decrement_action&lt;increment_action&gt;</code></td>
383</tr>
384<tr>
385<td><code class="literal">--</code></td>
386<td><code class="literal">pre_increment_decrement_action&lt;decrement_action&gt;</code></td>
387</tr>
388<tr>
389<td><code class="literal">++</code></td>
390<td><code class="literal">post_increment_decrement_action&lt;increment_action&gt;</code></td>
391</tr>
392<tr>
393<td><code class="literal">--</code></td>
394<td><code class="literal">post_increment_decrement_action&lt;decrement_action&gt;</code></td>
395</tr>
396<tr>
397<td><code class="literal">&amp;</code></td>
398<td><code class="literal">other_action&lt;address_of_action&gt;</code></td>
399</tr>
400<tr>
401<td><code class="literal">*</code></td>
402<td><code class="literal">other_action&lt;contents_of_action&gt;</code></td>
403</tr>
404<tr>
405<td><code class="literal">,</code></td>
406<td><code class="literal">other_action&lt;comma_action&gt;</code></td>
407</tr>
408</tbody>
409</table>
410</div>
411</div>
412<table width="100%"><tr>
413<td align="left"></td>
414<td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td>
415</tr></table>
416<hr>
417<div class="spirit-nav">
418<a accesskey="p" href="le_in_details.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s07.html"><img src="../images/next.png" alt="Next"></a>
419</div>
420</body>
421</html>
Note: See TracBrowser for help on using the repository browser.