index.html 8.5 KB

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