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