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