Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame^] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | |
| 3 | <html> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Language" content="en-us"> |
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> |
| 7 | <meta name="GENERATOR" content="Microsoft FrontPage 6.0"> |
| 8 | <meta name="ProgId" content="FrontPage.Editor.Document"> |
| 9 | |
| 10 | <title>Boost Function Object Adapter Library</title> |
| 11 | </head> |
| 12 | |
| 13 | <body bgcolor="#FFFFFF" text="#000000"> |
| 14 | <table border="1" bgcolor="#007F7F" cellpadding="2" summary=""> |
| 15 | <tr> |
| 16 | <td bgcolor="#FFFFFF"><img src="../../boost.png" alt= |
| 17 | "boost.png (6897 bytes)" width="277" height="86"></td> |
| 18 | |
| 19 | <td><a href="../../index.htm"><font face="Arial" color= |
| 20 | "#FFFFFF"><big>Home</big></font></a></td> |
| 21 | |
| 22 | <td><a href="../libraries.htm"><font face="Arial" color= |
| 23 | "#FFFFFF"><big>Libraries</big></font></a></td> |
| 24 | |
| 25 | <td><a href="http://www.boost.org/people/people.htm"><font face="Arial" color= |
| 26 | "#FFFFFF"><big>People</big></font></a></td> |
| 27 | |
| 28 | <td><a href="http://www.boost.org/more/faq.htm"><font face="Arial" color= |
| 29 | "#FFFFFF"><big>FAQ</big></font></a></td> |
| 30 | |
| 31 | <td><a href="../../more/index.htm"><font face="Arial" color= |
| 32 | "#FFFFFF"><big>More</big></font></a></td> |
| 33 | </tr> |
| 34 | </table> |
| 35 | |
| 36 | <h1>Improved Function Object Adapters</h1> |
| 37 | |
| 38 | <p>The header <a href="../../boost/functional.hpp">functional.hpp</a> |
| 39 | provides enhancements to the function object adapters specified in the C++ |
| 40 | Standard Library (sections 20.3.5, through to 20.3.8). The enhancements are |
| 41 | principally possible due to two changes:</p> |
| 42 | |
| 43 | <ol> |
| 44 | <li>We use the Boost <tt><a href= |
| 45 | "../utility/call_traits.htm">call_traits</a></tt> templates to avoid the |
| 46 | problem of <a href="binders.html#refref">references to references</a>, |
| 47 | and to improve the efficiency of <a href="mem_fun.html#args">parameter |
| 48 | passing</a>.</li> |
| 49 | |
| 50 | <li>We use two <a href="function_traits.html">function object traits</a> |
| 51 | class templates to avoid the need for <tt><a href= |
| 52 | "ptr_fun.html">ptr_fun</a></tt> with the adapters in this library.</li> |
| 53 | </ol> |
| 54 | |
| 55 | <h3>Contents</h3> |
| 56 | |
| 57 | <p>The header contains the following function and class templates:</p> |
| 58 | |
| 59 | <table border="1" cellpadding="5" summary=""> |
| 60 | <tr> |
| 61 | <th align="left"><a href="function_traits.html">Function object |
| 62 | traits</a></th> |
| 63 | |
| 64 | <td valign="top"><tt>unary_traits<br> |
| 65 | binary_traits</tt></td> |
| 66 | |
| 67 | <td valign="top">Used to determine the types of function objects' and |
| 68 | functions' arguments. Eliminate the necessity for |
| 69 | <tt>ptr_fun</tt>.</td> |
| 70 | </tr> |
| 71 | |
| 72 | <tr> |
| 73 | <th align="left"><a href="negators.html">Negators</a></th> |
| 74 | |
| 75 | <td valign="top"><tt>unary_negate<br> |
| 76 | binary_negate<br> |
| 77 | not1<br> |
| 78 | not2</tt></td> |
| 79 | |
| 80 | <td valign="top">Based on section 20.3.5 of the standard.</td> |
| 81 | </tr> |
| 82 | |
| 83 | <tr> |
| 84 | <th align="left"><a href="binders.html">Binders</a></th> |
| 85 | |
| 86 | <td valign="top"><tt>binder1st<br> |
| 87 | binder2nd<br> |
| 88 | bind1st<br> |
| 89 | bind2nd</tt></td> |
| 90 | |
| 91 | <td valign="top">Based on section 20.3.6 of the standard.</td> |
| 92 | </tr> |
| 93 | |
| 94 | <tr> |
| 95 | <th align="left"><a href="ptr_fun.html">Adapters for pointers to |
| 96 | functions</a></th> |
| 97 | |
| 98 | <td valign="top"><tt>pointer_to_unary_function<br> |
| 99 | pointer_to_binary_function<br> |
| 100 | ptr_fun</tt></td> |
| 101 | |
| 102 | <td valign="top">Based on section 20.3.7 of the standard. Not required |
| 103 | for use with this library since the binders and negators can adapt |
| 104 | functions, but may be needed with third party adapters.</td> |
| 105 | </tr> |
| 106 | |
| 107 | <tr> |
| 108 | <th align="left"><a href="mem_fun.html">Adapters for pointers to member |
| 109 | functions</a></th> |
| 110 | |
| 111 | <td valign="top"><tt>mem_fun_t<br> |
| 112 | mem_fun1_t<br> |
| 113 | const_mem_fun_t<br> |
| 114 | const_mem_fun1_t<br> |
| 115 | mem_fun_ref_t<br> |
| 116 | mem_fun1_ref_t<br> |
| 117 | const_mem_fun_ref_t<br> |
| 118 | const_mem_fun1_ref_t<br> |
| 119 | mem_fun<br> |
| 120 | mem_fun_ref</tt></td> |
| 121 | |
| 122 | <td valign="top">Based on section 20.3.8 of the standard.</td> |
| 123 | </tr> |
| 124 | </table> |
| 125 | |
| 126 | <h3>Usage</h3> |
| 127 | |
| 128 | <p>Using these adapters should be pretty much the same as using the |
| 129 | standard function object adapters; the only differences are that you need |
| 130 | to write <tt>boost::</tt> instead of <tt>std::</tt>, and that you will get |
| 131 | fewer headaches.</p> |
| 132 | |
| 133 | <p>For example, suppose you had a <tt>Person</tt> class that contained a |
| 134 | <tt>set_name</tt> function:</p> |
| 135 | |
| 136 | <blockquote> |
| 137 | <pre> |
| 138 | class Person |
| 139 | { |
| 140 | public: |
| 141 | void set_name(const std::string &name); |
| 142 | // ... |
| 143 | }; |
| 144 | </pre> |
| 145 | </blockquote> |
| 146 | |
| 147 | <p>You could rename a bunch of people in a collection, <tt>c</tt>, by |
| 148 | writing</p> |
| 149 | |
| 150 | <blockquote> |
| 151 | <pre> |
| 152 | std::for_each(c.begin(), c.end(), |
| 153 | boost::bind2nd(boost::mem_fun_ref(&Person::set_name), "Fred")); |
| 154 | </pre> |
| 155 | </blockquote> |
| 156 | |
| 157 | <p>If the standard adapters had been used instead then this code would |
| 158 | normally fail to compile, because <tt>set_name</tt> takes a reference |
| 159 | argument. Refer to the comments in the <a href="binders.html#refref">binder |
| 160 | documentation</a> to explain why this is so.</p> |
| 161 | |
| 162 | <h3>Compiler Compatibility</h3> |
| 163 | |
| 164 | <p>The header and <a href="test/function_test.cpp">test program</a> have been |
| 165 | compiled with the following compilers:</p> |
| 166 | |
| 167 | <table border="1" cellpadding="5" summary=""> |
| 168 | <tr> |
| 169 | <th>Compiler</th> |
| 170 | |
| 171 | <th>Comments</th> |
| 172 | </tr> |
| 173 | |
| 174 | <tr> |
| 175 | <td valign="top">Borland C++Builder 4 Update 2</td> |
| 176 | |
| 177 | <td valign="top">No known issues.</td> |
| 178 | </tr> |
| 179 | |
| 180 | <tr> |
| 181 | <td valign="top">Borland C++ 5.5</td> |
| 182 | |
| 183 | <td valign="top">No known issues.</td> |
| 184 | </tr> |
| 185 | |
| 186 | <tr> |
| 187 | <td valign="top">g++ 2.95.2</td> |
| 188 | |
| 189 | <td valign="top">No known issues.</td> |
| 190 | </tr> |
| 191 | |
| 192 | <tr> |
| 193 | <td valign="top">Microsoft Visual C++ Service Pack 3</td> |
| 194 | |
| 195 | <td valign="top"> |
| 196 | Compiler lacks partial specialisation, so this library offers little |
| 197 | more than is provided by the standard adapters: |
| 198 | |
| 199 | <ul> |
| 200 | <li>The <tt>call_traits</tt> mechanism is unable to prevent |
| 201 | references to references, and so the adapters in this library will |
| 202 | be usable in fewer situations.</li> |
| 203 | |
| 204 | <li>The <tt>function_traits</tt> mechanism is unable to determine |
| 205 | the argument and result types of functions, therefore |
| 206 | <tt>ptr_fun</tt> continues to be required to adapt functions.</li> |
| 207 | </ul> |
| 208 | </td> |
| 209 | </tr> |
| 210 | </table> |
| 211 | |
| 212 | <h3>Future Directions</h3> |
| 213 | |
| 214 | <p>This library's primary focus is to solve the problem of references to |
| 215 | references while maintaining as much compatibility as possible with the |
| 216 | standard library. This allows you to use the techniques you read about in |
| 217 | books and magazines with many of today's compilers.</p> |
| 218 | |
| 219 | <p>In the longer term, even better solutions are likely:</p> |
| 220 | |
| 221 | <ol> |
| 222 | <li>Several Boost members are working on expression template libraries. |
| 223 | These will allow a more natural syntax for combining and adapting |
| 224 | functions. As this is a new technology, it may be some time before it has |
| 225 | matured and is widely supported by major compilers but shows great |
| 226 | promise. In the meantime, the functional.hpp library fills the gap.</li> |
| 227 | |
| 228 | <li>The Standard Committee has recognised the problem of references to |
| 229 | references occurring during template instantiation and has moved to fix |
| 230 | the standard (see the <a href= |
| 231 | "http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">C++ |
| 232 | standard core language active issues list</a>).</li> |
| 233 | </ol> |
| 234 | |
| 235 | <h3>Author</h3> |
| 236 | |
| 237 | <p><a href="http://www.boost.org/people/mark_rodgers.htm">Mark Rodgers</a></p> |
| 238 | |
| 239 | <h3>Acknowledgements</h3> |
| 240 | |
| 241 | <p>Thanks to <a href="http://www.boost.org/people/john_maddock.htm">John Maddock</a> for |
| 242 | suggesting the mechanism that allowed the function objects traits to work |
| 243 | correctly. <a href="http://www.boost.org/people/jens_maurer.htm">Jens Maurer</a> provided |
| 244 | invaluable feedback during the <a href= |
| 245 | "http://www.boost.org/more/formal_review_process.htm">formal review process</a>.</p> |
| 246 | <hr> |
| 247 | |
| 248 | <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src= |
| 249 | "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional" |
| 250 | height="31" width="88"></a></p> |
| 251 | |
| 252 | <p>Revised |
| 253 | <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02 |
| 254 | December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p> |
| 255 | |
| 256 | <p><i>Copyright © 2000 Cadenza New Zealand Ltd.</i></p> |
| 257 | |
| 258 | <p><i>Distributed under the Boost Software License, Version 1.0. (See |
| 259 | accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or |
| 260 | copy at <a href= |
| 261 | "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p> |
| 262 | </body> |
| 263 | </html> |