9
3

binders.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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>Binders</h1>
  26. <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
  27. provides enhanced versions of both the binder function object adapters from
  28. the C++ Standard Library (&sect;20.3.6):</p>
  29. <ul>
  30. <li><tt>binder1st</tt></li>
  31. <li><tt>binder2nd</tt></li>
  32. </ul>
  33. <p>As well as the corresponding helper functions</p>
  34. <ul>
  35. <li><tt>bind1st</tt></li>
  36. <li><tt>bind2nd</tt></li>
  37. </ul>
  38. <p>The key benefit of these adapters over those in the Standard Library is
  39. they avoid the problem of <a href="#refref">references to
  40. references.</a></p>
  41. <h3>Usage</h3>
  42. <p>Usage is identical to the standard binders. For example,</p>
  43. <blockquote>
  44. <pre>
  45. class Foo {
  46. public:
  47. void bar(std::ostream &amp;);
  48. // ...
  49. };
  50. // ...
  51. std::vector&lt;Foo&gt; c;
  52. // ...
  53. std::for_each(c.begin(), c.end(),
  54. boost::bind2nd(boost::mem_fun_ref(&amp;Foo::bar), std::cout));
  55. </pre>
  56. </blockquote>
  57. <h3 id="refref">References to References</h3>
  58. <p>Consider the usage example above</p>
  59. <blockquote>
  60. <pre>
  61. class Foo {
  62. public:
  63. void bar(<strong>std::ostream &amp;</strong>);
  64. // ...
  65. };
  66. // ...
  67. std::for_each(c.begin(), c.end(),
  68. boost::bind2nd(boost::mem_fun_ref(&amp;Foo::bar), std::cout));
  69. </pre>
  70. </blockquote>
  71. <p>If this had been written using <tt>std::bind2nd</tt> and
  72. <tt>std::mem_fun_ref</tt>, it would be unlikely to compile.</p>
  73. <p>The problem arises because <tt>bar</tt> takes a reference argument. The
  74. Standard defines <tt>std::mem_fun_ref</tt> such that it creates a function
  75. object whose <tt>second_argument_type</tt> will be
  76. <tt>std::ostream&amp;</tt>.</p>
  77. <p>The call to <tt>bind2nd</tt> creates a <tt>binder2nd</tt> which the
  78. Standard defines as follows:</p>
  79. <blockquote>
  80. <pre>
  81. template &lt;class Operation&gt;
  82. class binder2nd
  83. : public unary_function&lt;typename Operation::first_argument_type,
  84. typename Operation::result_type&gt; {
  85. ...
  86. public:
  87. binder2nd(const Operation&amp; x,
  88. <strong>const typename Operation::second_argument_type&amp; y</strong>);
  89. ...
  90. </pre>
  91. </blockquote>
  92. <p>Since our operation's <tt>second_argument_type</tt> is
  93. <tt>std::ostream&amp;</tt>, the type of <tt>y</tt> in the constructor would
  94. be <tt>std::ostream&amp;&amp;</tt>. Since you cannot have a reference to a
  95. reference, at this point we should get a compilation error because
  96. references to references are illegal in C++ (but see <a href=
  97. "http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#106">C++
  98. Standard core language active issues list</a>).</p>
  99. <p>The binders in this library avoid this problem by using the Boost
  100. <tt><a href="../utility/call_traits.htm">call_traits</a></tt>
  101. templates.</p>
  102. <p>Our constructor is declared</p>
  103. <blockquote>
  104. <pre>
  105. binder2nd(const Operation&amp; x,
  106. <strong>typename call_traits&lt;
  107. typename binary_traits&lt;Operation&gt;::second_argument_type
  108. &gt;::param_type y</strong>)
  109. </pre>
  110. </blockquote>
  111. <p>As a result, <tt>y</tt> has a type of <tt>std::ostream&amp;</tt>, and
  112. our example compiles.</p>
  113. <hr>
  114. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  115. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  116. height="31" width="88"></a></p>
  117. <p>Revised
  118. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
  119. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
  120. <p><i>Copyright &copy; 2000 Cadenza New Zealand Ltd.</i></p>
  121. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  122. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  123. copy at <a href=
  124. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  125. </body>
  126. </html>