mem_fun.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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>Member Function Adapters</h1>
  26. <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
  27. includes improved versions of the full range of member function adapters
  28. from the the C++ Standard Library (&sect;20.3.8):</p>
  29. <ul>
  30. <li><tt>mem_fun_t</tt></li>
  31. <li><tt>mem_fun1_t</tt></li>
  32. <li><tt>const_mem_fun_t</tt></li>
  33. <li><tt>const_mem_fun1_t</tt></li>
  34. <li><tt>mem_fun_ref_t</tt></li>
  35. <li><tt>mem_fun1_ref_t</tt></li>
  36. <li><tt>const_mem_fun_ref_t</tt></li>
  37. <li><tt>const_mem_fun1_ref_t</tt></li>
  38. </ul>
  39. <p>as well as the corresponding overloaded helper functions</p>
  40. <ul>
  41. <li><tt>mem_fun</tt></li>
  42. <li><tt>mem_fun_ref</tt></li>
  43. </ul>
  44. <p>The following changes have been made to the adapters as specified in the
  45. Standard:</p>
  46. <ul>
  47. <li>The <tt>first_argument_type</tt> typedef has been corrected for the
  48. <tt>const_</tt> family of member function adapters (see <a href=
  49. "#firstarg">below</a>).</li>
  50. <li>The argument passed to <tt>mem_fun1_t</tt> and its variants is passed
  51. using the <tt>call_traits::param_type</tt> for the member function's
  52. argument type.</li>
  53. </ul>
  54. <h3 id="firstarg">first_argument_type</h3>
  55. <p>The standard specifies <tt>const_mem_fun1_t</tt>, for example, like
  56. this:</p>
  57. <blockquote>
  58. <pre>
  59. template &lt;class S, class T, class A&gt; class const_mem_fun1_t
  60. : public binary_function&lt;<strong>T*</strong>, A, S&gt; {
  61. public:
  62. explicit const_mem_fun1_t(S (T::*p)(A) const);
  63. S operator()(<strong>const T*</strong> p, A x) const;
  64. };
  65. </pre>
  66. </blockquote>
  67. <p>Note that the first argument to <tt>binary_function</tt> is <tt>T*</tt>
  68. despite the fact that the first argument to <tt>operator()</tt> is actually
  69. of type <tt><em>const</em>&nbsp;T*</tt>.</p>
  70. <p>Does this matter? Well, consider what happens when we write</p>
  71. <blockquote>
  72. <pre>
  73. struct Foo { void bar(int) const; };
  74. const Foo *cp = new Foo;
  75. std::bind1st(std::mem_fun(&amp;Foo::bar), cp);
  76. </pre>
  77. </blockquote>
  78. <p>We have created a <tt>const_mem_fun1_t</tt> object which will
  79. effectively contain the following</p>
  80. <blockquote>
  81. <pre>
  82. typedef Foo* first_argument_type;
  83. </pre>
  84. </blockquote>
  85. <p>The <tt>bind1st</tt> will then create a <tt>binder1st</tt> object that
  86. will use this <tt>typedef</tt> as the type of a member which will be
  87. initialised with <tt>cp</tt>. In other words, we will need to initialise a
  88. <tt>Foo*</tt> member with a <tt>const&nbsp;Foo*</tt> pointer! Clearly this
  89. is not possible, so to implement this your Standard Library vendor will
  90. have had to cast away the constness of <tt>cp</tt>, probably within the
  91. body of <tt>bind1st</tt>.</p>
  92. <p>This hack will not suffice with the improved <a href=
  93. "binders.html">binders</a> in this library, so we have had to provide
  94. corrected versions of the member function adapters as well.</p>
  95. <h3 id="args">Argument Types</h3>
  96. <p>The standard defines <tt>mem_fun1_t</tt>, for example, like this
  97. (&sect;20.3.8&nbsp;&para;2):</p>
  98. <blockquote>
  99. <pre>
  100. template &lt;class S, class T, class A&gt; class mem_fun1_t
  101. : public binary_function&lt;T*, A, S&gt; {
  102. public:
  103. explicit mem_fun1_t(S (T::*p)(<strong>A</strong>));
  104. S operator()(T* p, <strong>A</strong> x) const;
  105. };
  106. </pre>
  107. </blockquote>
  108. <p>Note that the second argument to <tt>operator()</tt> is exactly the same
  109. type as the argument to the member function. If this is a value type, the
  110. argument will be passed by value and copied twice.</p>
  111. <p>However, if we were to try and eliminate this inefficiency by instead
  112. declaring the argument as <tt>const&nbsp;A&amp;</tt>, then if A were a
  113. reference type, we would have a reference to a reference, which is
  114. currently illegal (but see <a href=
  115. "http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#106">C++ core
  116. language issue number 106)</a></p>
  117. <p>So the way in which we want to declare the second argument for
  118. <tt>operator()</tt> depends on whether or not the member function's
  119. argument is a reference. If it is a reference, we want to declare it simply
  120. as <tt>A</tt>; if it is a value we want to declare it as
  121. <tt>const&nbsp;A&amp;</tt>.</p>
  122. <p>The Boost <a href="../utility/call_traits.htm">call_traits</a> class
  123. template contains a <tt>param_type</tt> typedef, which uses partial
  124. specialisation to make precisely this decision. By declaring the
  125. <tt>operator()</tt> as</p>
  126. <blockquote>
  127. <pre>
  128. S operator()(T* p, typename call_traits&lt;A&gt;::param_type x) const
  129. </pre>
  130. </blockquote>
  131. <p>we achieve the desired result - we improve efficiency without generating
  132. references to references.</p>
  133. <h3>Limitations</h3>
  134. <p>The call traits template used to realise some improvements relies on
  135. partial specialisation, so these improvements are only available on
  136. compilers that support that feature. With other compilers, the argument
  137. passed to the member function (in the <tt>mem_fun1_t</tt> family) will
  138. always be passed by reference, thus generating the possibility of
  139. references to references.</p>
  140. <hr>
  141. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  142. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  143. height="31" width="88"></a></p>
  144. <p>Revised
  145. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
  146. <p><i>Copyright &copy; 2000 Cadenza New Zealand Ltd.</i></p>
  147. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  148. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  149. copy at <a href=
  150. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  151. </body>
  152. </html>