Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/doc/html/lambda/extending.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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