Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/binders.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 4.3 KB
Line 
1<html>
2
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5<title>Boost Function Object Adapter Library</title>
6</head>
7
8<body bgcolor="#FFFFFF" text="#000000">
9
10<table border="1" bgcolor="#007F7F" cellpadding="2">
11  <tr>
12    <td bgcolor="#FFFFFF"><img src="../../boost.png" alt="boost.png (6897 bytes)" WIDTH="277" HEIGHT="86"></td>
13    <td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td>
14    <td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td>
15    <td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td>
16    <td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td>
17    <td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td>
18  </tr>
19</table>
20
21<h1>Binders</h1>
22
23<p>The header <nobr><a
24href="../../boost/functional.hpp">functional.hpp</a></nobr> provides
25enhanced versions of both the binder function object adapters from the
26C++ Standard Library <nobr>(&sect;20.3.6):</nobr></p>
27
28<ul>
29<li><tt>binder1st</tt></li>
30<li><tt>binder2nd</tt></li>
31</ul>
32
33<p>As well as the corresponding helper functions</p>
34
35<ul>
36<li><tt>bind1st</tt></li>
37<li><tt>bind2nd</tt></li>
38</ul>
39
40<p>The key benefit of these adapters over those in the Standard
41Library is they avoid the problem of <a href="#refref">references to
42references.</a>
43
44<h3>Usage</h3>
45
46<p>Usage is identical to the standard binders.  For example,</p>
47
48<blockquote><pre>
49class Foo {
50public:
51  void bar(std::ostream &);
52  // ...
53};
54// ...
55std::vector&lt;Foo&gt; c;
56// ...
57std::for_each(c.begin(), c.end(),
58              boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout));
59</pre></blockquote>
60
61<h3 id="refref">References to References</h3>
62
63<p>Consider the usage example above</p>
64
65<blockquote><pre>
66class Foo {
67public:
68  void bar(<strong>std::ostream &</strong>);
69  // ...
70};
71// ...
72std::for_each(c.begin(), c.end(),
73              boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout));
74</pre></blockquote>
75
76<p>If this had been written using <tt><nobr>std::bind2nd</nobr></tt>
77and <tt><nobr>std::mem_fun_ref</nobr></tt>, it would be unlikely to
78compile.</p>
79
80<p>The problem arises because <tt><nobr>bar</nobr></tt> takes a
81reference argument.  The Standard defines
82<tt><nobr>std::mem_fun_ref</nobr></tt> such that it creates a function
83object whose <tt><nobr>second_argument_type</nobr></tt> will be
84<tt><nobr>std::ostream&</nobr></tt>.</p>
85
86<p>The call to <tt><nobr>bind2nd</nobr></tt> creates a
87<tt><nobr>binder2nd</nobr></tt> which the Standard defines as follows:
88
89<blockquote><pre>
90template &lt;class Operation&gt;
91class binder2nd
92    : public unary_function&lt;typename Operation::first_argument_type,
93                            typename Operation::result_type&gt; {
94...
95public:
96  binder2nd(const Operation& x,
97            <strong>const typename Operation::second_argument_type& y</strong>);
98  ...
99</pre></blockquote>
100
101<p>Since our operation's <tt><nobr>second_argument_type</nobr></tt> is
102<tt><nobr>std::ostream&</nobr></tt>, the type of <tt>y</tt> in the
103constructor would be <tt><nobr>std::ostream&&</nobr></tt>.  Since you
104cannot have a reference to a reference, at this point we should get a
105compilation error because references to references are illegal in C++
106(but see <a
107href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">
108C++ Standard core language active issues list</a>).</p>
109
110<p>The binders in this library avoid this problem by using the Boost
111<nobr><tt><a
112href="../utility/call_traits.htm">call_traits</a></tt></nobr> templates.</p>
113
114<p>Our constructor is declared
115
116<blockquote><pre>
117binder2nd(const Operation& x,
118          <strong>typename call_traits&lt;
119             typename binary_traits&lt;Operation&gt;::second_argument_type
120          &gt;::param_type y</strong>)
121</pre></blockquote>
122
123<p>As a result, <tt>y</tt> has a type of <tt><nobr>std::ostream&</nobr></tt>,
124and our example compiles.</p>
125
126<hr>
127<p>Copyright &copy; 2000 Cadenza New Zealand Ltd.  Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.</p>
128
129<p>Revised 28 June 2000</p>
130
131</body>
132</html>
Note: See TracBrowser for help on using the repository browser.