ptr_fun.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <title>Boost Function Object Adapter Library</title>
  7. </head>
  8. <body bgcolor="#FFFFFF" text="#000000">
  9. <table border="1" bgcolor="#007F7F" cellpadding="2" summary="">
  10. <tr>
  11. <td bgcolor="#FFFFFF"><img src="../../boost.png" alt=
  12. "boost.png (6897 bytes)" width="277" height="86"></td>
  13. <td><a href="../../index.htm"><font face="Arial" color=
  14. "#FFFFFF"><big>Home</big></font></a></td>
  15. <td><a href="../libraries.htm"><font face="Arial" color=
  16. "#FFFFFF"><big>Libraries</big></font></a></td>
  17. <td><a href="http://www.boost.org/people/people.htm"><font face="Arial" color=
  18. "#FFFFFF"><big>People</big></font></a></td>
  19. <td><a href="http://www.boost.org/more/faq.htm"><font face="Arial" color=
  20. "#FFFFFF"><big>FAQ</big></font></a></td>
  21. <td><a href="../../more/index.htm"><font face="Arial" color=
  22. "#FFFFFF"><big>More</big></font></a></td>
  23. </tr>
  24. </table>
  25. <h1>Function Pointer Adapters</h1>
  26. <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
  27. provides enhanced versions of both the function pointer adapters from the
  28. C++ Standard Library (&sect;20.3.7):</p>
  29. <ul>
  30. <li><tt>pointer_to_unary_function</tt></li>
  31. <li><tt>pointer_to_binary_function</tt></li>
  32. </ul>
  33. <p>As well as the corresponding helper function template:</p>
  34. <ul>
  35. <li><tt>ptr_fun</tt></li>
  36. </ul>
  37. <p>However, you should not need to use the adapters in conjunction with the
  38. adapters in this library due to our use of <a href=
  39. "function_traits.html">function object traits</a>. You will however need to
  40. use them if your implementation fails to work properly with our traits
  41. classes (due to lack if partial specialisation), or if you wish to use a
  42. function object adapter from a third party.</p>
  43. <h3>Usage</h3>
  44. <p>If you need to use these adapters, usage is identical to the standard
  45. function pointer adapters. For example,</p>
  46. <blockquote>
  47. <pre>
  48. bool bad(std::string foo) { ... }
  49. ...
  50. std::vector&lt;std::string&gt; c;
  51. ...
  52. std::vector&lt;std::string&gt;::iterator it
  53. = std::find_if(c.begin(), c.end(), std::not1(boost::ptr_fun(bad)));
  54. </pre>
  55. </blockquote>
  56. <p>Note however that this library contains enhanced <a href=
  57. "negators.html">negators</a> that support function object traits, so the
  58. line above could equally be written</p>
  59. <blockquote>
  60. <pre>
  61. std::vector&lt;std::string&gt;::iterator it
  62. = std::find_if(c.begin(), c.end(), boost::not1(bad));
  63. </pre>
  64. </blockquote>
  65. <h3>Argument Types</h3>
  66. <p>The standard defines <tt>pointer_to_unary_function</tt> like this
  67. (&sect;20.3.8&nbsp;&para;2):</p>
  68. <blockquote>
  69. <pre>
  70. template &lt;class Arg, class Result&gt;
  71. class pointer_to_unary_function : public unary_function&lt;Arg, Result&gt; {
  72. public:
  73. explicit pointer_to_unary_function(Result (* f)(<strong>Arg</strong>));
  74. Result operator()(<strong>Arg</strong> x) const;
  75. };
  76. </pre>
  77. </blockquote>
  78. <p>Note that the argument to <tt>operator()</tt> is exactly the same type
  79. as the argument to the wrapped function. If this is a value type, the
  80. argument will be passed by value and copied twice.
  81. <tt>pointer_to_binary_function</tt> has a similar problem.</p>
  82. <p>However, if we were to try and eliminate this inefficiency by instead
  83. declaring the argument as <tt>const&nbsp;Arg&amp;</tt>, then if Arg were a
  84. reference type, we would have a reference to a reference, which is
  85. currently illegal (but see <a href=
  86. "http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#106">C++ core
  87. language issue number 106)</a></p>
  88. <p>So the way in which we want to declare the argument for
  89. <tt>operator()</tt> depends on whether or not the wrapped function's
  90. argument is a reference. If it is a reference, we want to declare it simply
  91. as <tt>Arg</tt>; if it is a value we want to declare it as
  92. <tt>const&nbsp;Arg&amp;</tt>.</p>
  93. <p>The Boost <a href="../utility/call_traits.htm">call_traits</a> class
  94. template contains a <tt>param_type</tt> typedef, which uses partial
  95. specialisation to make precisely this decision. By declaring the
  96. <tt>operator()</tt> as</p>
  97. <blockquote>
  98. <pre>
  99. Result operator()(typename call_traits&lt;Arg&gt;::param_type x) const
  100. </pre>
  101. </blockquote>
  102. <p>we achieve the desired result - we improve efficiency without generating
  103. references to references.</p>
  104. <h3>Limitations</h3>
  105. <p>The call traits template used to realise this improvement relies on
  106. partial specialisation, so this improvement is only available on compilers
  107. that support that feature. With other compilers, the argument passed to the
  108. function will always be passed by reference, thus generating the
  109. possibility of references to references.</p>
  110. <hr>
  111. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  112. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  113. height="31" width="88"></a></p>
  114. <p>Revised
  115. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
  116. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
  117. <p><i>Copyright &copy; 2000 Cadenza New Zealand Ltd.</i></p>
  118. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  119. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  120. copy at <a href=
  121. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  122. </body>
  123. </html>