File: binders.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (132 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Boost Function Object Adapter Library</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<table border="1" bgcolor="#007F7F" cellpadding="2">
  <tr>
    <td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" WIDTH="277" HEIGHT="86"></td>
    <td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td>
    <td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td>
    <td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td>
    <td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td>
    <td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td>
  </tr>
</table>

<h1>Binders</h1>

<p>The header <nobr><a
href="../../boost/functional.hpp">functional.hpp</a></nobr> provides
enhanced versions of both the binder function object adapters from the
C++ Standard Library <nobr>(&sect;20.3.6):</nobr></p>

<ul>
<li><tt>binder1st</tt></li>
<li><tt>binder2nd</tt></li>
</ul>

<p>As well as the corresponding helper functions</p>

<ul>
<li><tt>bind1st</tt></li>
<li><tt>bind2nd</tt></li>
</ul>

<p>The key benefit of these adapters over those in the Standard
Library is they avoid the problem of <a href="#refref">references to
references.</a>

<h3>Usage</h3>

<p>Usage is identical to the standard binders.  For example,</p>

<blockquote><pre>
class Foo {
public:
  void bar(std::ostream &);
  // ...
};
// ...
std::vector&lt;Foo&gt; c;
// ...
std::for_each(c.begin(), c.end(), 
              boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout));
</pre></blockquote>

<h3 id="refref">References to References</h3>

<p>Consider the usage example above</p>

<blockquote><pre>
class Foo {
public:
  void bar(<strong>std::ostream &</strong>);
  // ...
};
// ...
std::for_each(c.begin(), c.end(), 
              boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout));
</pre></blockquote>

<p>If this had been written using <tt><nobr>std::bind2nd</nobr></tt>
and <tt><nobr>std::mem_fun_ref</nobr></tt>, it would be unlikely to
compile.</p>

<p>The problem arises because <tt><nobr>bar</nobr></tt> takes a
reference argument.  The Standard defines
<tt><nobr>std::mem_fun_ref</nobr></tt> such that it creates a function
object whose <tt><nobr>second_argument_type</nobr></tt> will be
<tt><nobr>std::ostream&</nobr></tt>.</p>

<p>The call to <tt><nobr>bind2nd</nobr></tt> creates a
<tt><nobr>binder2nd</nobr></tt> which the Standard defines as follows:

<blockquote><pre>
template &lt;class Operation&gt;
class binder2nd
    : public unary_function&lt;typename Operation::first_argument_type,
                            typename Operation::result_type&gt; {
...
public:
  binder2nd(const Operation& x,
            <strong>const typename Operation::second_argument_type& y</strong>);
  ...
</pre></blockquote>

<p>Since our operation's <tt><nobr>second_argument_type</nobr></tt> is
<tt><nobr>std::ostream&</nobr></tt>, the type of <tt>y</tt> in the
constructor would be <tt><nobr>std::ostream&&</nobr></tt>.  Since you
cannot have a reference to a reference, at this point we should get a
compilation error because references to references are illegal in C++
(but see <a
href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">
C++ Standard core language active issues list</a>).</p>

<p>The binders in this library avoid this problem by using the Boost
<nobr><tt><a
href="../utility/call_traits.htm">call_traits</a></tt></nobr> templates.</p>

<p>Our constructor is declared

<blockquote><pre>
binder2nd(const Operation& x,
          <strong>typename call_traits&lt;
             typename binary_traits&lt;Operation&gt;::second_argument_type
          &gt;::param_type y</strong>)
</pre></blockquote>

<p>As a result, <tt>y</tt> has a type of <tt><nobr>std::ostream&</nobr></tt>, 
and our example compiles.</p>

<hr>
<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>

<p>Revised 28 June 2000</p>

</body>
</html>