1 | <html> |
---|
2 | |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
5 | <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> |
---|
6 | <meta name="ProgId" content="FrontPage.Editor.Document"> |
---|
7 | <title>Boost Function Object Adapter Library</title> |
---|
8 | </head> |
---|
9 | |
---|
10 | <body bgcolor="#FFFFFF" text="#000000"> |
---|
11 | |
---|
12 | <table border="1" bgcolor="#007F7F" cellpadding="2"> |
---|
13 | <tr> |
---|
14 | <td bgcolor="#FFFFFF"><img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86"></td> |
---|
15 | <td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home</big></font></a></td> |
---|
16 | <td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries</big></font></a></td> |
---|
17 | <td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People</big></font></a></td> |
---|
18 | <td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ</big></font></a></td> |
---|
19 | <td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More</big></font></a></td> |
---|
20 | </tr> |
---|
21 | </table> |
---|
22 | <h1>Improved Function Object Adapters</h1> |
---|
23 | <p>The header <nobr><a href="../../boost/functional.hpp">functional.hpp</a></nobr> |
---|
24 | provides enhancements to the function object adapters specified in the C++ |
---|
25 | Standard Library (sections 20.3.5, through to 20.3.8). The enhancements are |
---|
26 | principally possible due to two changes:</p> |
---|
27 | <ol> |
---|
28 | <li>We use the Boost <nobr><tt><a href="../utility/call_traits.htm">call_traits</a></tt></nobr> |
---|
29 | templates to avoid the problem of <a href="binders.html#refref">references |
---|
30 | to references</a>, and to improve the efficiency of <a href="mem_fun.html#args">parameter |
---|
31 | passing</a>.</li> |
---|
32 | <li>We use two <a href="function_traits.html">function object traits</a> class |
---|
33 | templates to avoid the need for <nobr><tt><a href="ptr_fun.html">ptr_fun</a></tt></nobr> |
---|
34 | with the adapters in this library.</li> |
---|
35 | </ol> |
---|
36 | <h3>Contents</h3> |
---|
37 | <p>The header contains the following function and class templates:</p> |
---|
38 | <table border="1" cellpadding="5"> |
---|
39 | <tr> |
---|
40 | <th align="left"><a href="function_traits.html">Function object traits</a> |
---|
41 | <td valign="top"><tt><nobr>unary_traits</nobr><br> |
---|
42 | <nobr>binary_traits</nobr></tt></td> |
---|
43 | <td valign="top">Used to determine the types of function objects' and |
---|
44 | functions' arguments. Eliminate the necessity for <nobr><tt>ptr_fun</tt></nobr>.</td> |
---|
45 | </tr> |
---|
46 | <tr> |
---|
47 | <th align="left"><a href="negators.html">Negators</a></th> |
---|
48 | <td valign="top"><tt><nobr>unary_negate</nobr><br> |
---|
49 | <nobr>binary_negate</nobr><br> |
---|
50 | <nobr>not1</nobr><br> |
---|
51 | <nobr>not2</nobr></tt></td> |
---|
52 | <td valign="top">Based on section 20.3.5 of the standard.</td> |
---|
53 | </tr> |
---|
54 | <tr> |
---|
55 | <th align="left"><a href="binders.html">Binders</a></th> |
---|
56 | <td valign="top"><tt><nobr>binder1st</nobr><br> |
---|
57 | <nobr>binder2nd</nobr><br> |
---|
58 | <nobr>bind1st</nobr><br> |
---|
59 | <nobr>bind2nd</nobr></tt></td> |
---|
60 | <td valign="top">Based on section 20.3.6 of the standard.</td> |
---|
61 | </tr> |
---|
62 | <tr> |
---|
63 | <th align="left"><a href="ptr_fun.html">Adapters for pointers to functions</a></th> |
---|
64 | <td valign="top"><tt><nobr>pointer_to_unary_function</nobr><br> |
---|
65 | <nobr>pointer_to_binary_function</nobr><br> |
---|
66 | <nobr>ptr_fun</nobr></tt></td> |
---|
67 | <td valign="top">Based on section 20.3.7 of the standard. Not required for |
---|
68 | use with this library since the binders and negators can adapt functions, |
---|
69 | but may be needed with third party adapters.</td> |
---|
70 | </tr> |
---|
71 | <tr> |
---|
72 | <th align="left"><a href="mem_fun.html">Adapters for pointers to member |
---|
73 | functions</a></th> |
---|
74 | <td valign="top"><tt><nobr>mem_fun_t</nobr><br> |
---|
75 | <nobr>mem_fun1_t</nobr><br> |
---|
76 | <nobr>const_mem_fun_t</nobr><br> |
---|
77 | <nobr>const_mem_fun1_t</nobr><br> |
---|
78 | <nobr>mem_fun_ref_t</nobr><br> |
---|
79 | <nobr>mem_fun1_ref_t</nobr><br> |
---|
80 | <nobr>const_mem_fun_ref_t</nobr><br> |
---|
81 | <nobr>const_mem_fun1_ref_t</nobr><br> |
---|
82 | <nobr>mem_fun</nobr><br> |
---|
83 | <nobr>mem_fun_ref</nobr></tt></td> |
---|
84 | <td valign="top">Based on section 20.3.8 of the standard.</td> |
---|
85 | </tr> |
---|
86 | </table> |
---|
87 | <h3>Usage</h3> |
---|
88 | <p>Using these adapters should be pretty much the same as using the standard |
---|
89 | function object adapters; the only differences are that you need to write <nobr><tt>boost::</tt></nobr> |
---|
90 | instead of <nobr><tt>std::</tt></nobr>, and that you will get fewer headaches.</p> |
---|
91 | <p>For example, suppose you had a <tt>Person</tt> class that contained a <nobr><tt>set_name</tt></nobr> |
---|
92 | function: |
---|
93 | <blockquote> |
---|
94 | <pre> |
---|
95 | class Person |
---|
96 | { |
---|
97 | public: |
---|
98 | void set_name(const std::string &name); |
---|
99 | // ... |
---|
100 | }; |
---|
101 | </pre> |
---|
102 | </blockquote> |
---|
103 | <p>You could rename a bunch of people in a collection, <tt>c</tt>, by writing</p> |
---|
104 | <blockquote> |
---|
105 | <pre> |
---|
106 | std::for_each(c.begin(), c.end(), |
---|
107 | boost::bind2nd(boost::mem_fun_ref(&Person::set_name), "Fred")); |
---|
108 | </pre> |
---|
109 | </blockquote> |
---|
110 | <p>If the standard adapters had been used instead then this code would normally |
---|
111 | fail to compile, because <tt><nobr>set_name</nobr></tt> takes a reference |
---|
112 | argument. Refer to the comments in the <a href="binders.html#refref">binder |
---|
113 | documentation</a> to explain why this is so.</p> |
---|
114 | <h3>Compiler Compatibility</h3> |
---|
115 | <p>The header and <a href="function_test.cpp">test program</a> have been |
---|
116 | compiled with the following compilers:</p> |
---|
117 | <table border="1" cellpadding="5"> |
---|
118 | <tr> |
---|
119 | <th>Compiler</th> |
---|
120 | <th>Comments</th> |
---|
121 | </tr> |
---|
122 | <tr> |
---|
123 | <td valign="top">Borland C++Builder 4 Update 2</td> |
---|
124 | <td valign="top">No known issues.</td> |
---|
125 | </tr> |
---|
126 | <tr> |
---|
127 | <td valign="top">Borland C++ 5.5</td> |
---|
128 | <td valign="top">No known issues.</td> |
---|
129 | </tr> |
---|
130 | <tr> |
---|
131 | <td valign="top">g++ 2.95.2</td> |
---|
132 | <td valign="top">No known issues.</td> |
---|
133 | </tr> |
---|
134 | <tr> |
---|
135 | <td valign="top">Microsoft Visual C++ Service Pack 3</td> |
---|
136 | <td valign="top">Compiler lacks partial specialisation, so this library |
---|
137 | offers little more than is provided by the standard adapters: |
---|
138 | <ul> |
---|
139 | <li>The <nobr><tt>call_traits</tt></nobr> mechanism is unable to prevent |
---|
140 | references to references, and so the adapters in this library will be |
---|
141 | usable in fewer situations.</li> |
---|
142 | <li>The <nobr><tt>function_traits</tt></nobr> mechanism is unable to |
---|
143 | determine the argument and result types of functions, therefore <nobr><tt>ptr_fun</tt></nobr> |
---|
144 | continues to be required to adapt functions. |
---|
145 | </ul> |
---|
146 | </td> |
---|
147 | </tr> |
---|
148 | </table> |
---|
149 | <h3>Future Directions</h3> |
---|
150 | <p>This library's primary focus is to solve the problem of references to |
---|
151 | references while maintaining as much compatibility as possible with the standard |
---|
152 | library. This allows you to use the techniques you read about in books and |
---|
153 | magazines with many of today's compilers.</p> |
---|
154 | <p>In the longer term, even better solutions are likely:</p> |
---|
155 | <ol> |
---|
156 | <li>Several Boost members are working on expression template libraries. These |
---|
157 | will allow a more natural syntax for combining and adapting functions. As |
---|
158 | this is a new technology, it may be some time before it has matured and is |
---|
159 | widely supported by major compilers but shows great promise. In the |
---|
160 | meantime, the functional.hpp library fills the gap.</li> |
---|
161 | <li>The Standard Committee has recognised the problem of references to |
---|
162 | references occurring during template instantiation and has moved to fix the |
---|
163 | standard (see the <a href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">C++ |
---|
164 | standard core language active issues list</a>).</li> |
---|
165 | </ol> |
---|
166 | <h3>Author</h3> |
---|
167 | <p><a href="../../people/mark_rodgers.htm">Mark Rodgers</a></p> |
---|
168 | <h3>Acknowledgements</h3> |
---|
169 | <p>Thanks to <a href="../../people/john_maddock.htm">John Maddock</a> for |
---|
170 | suggesting the mechanism that allowed the function objects traits to work |
---|
171 | correctly. <a href="../../people/jens_maurer.htm">Jens Maurer</a> provided |
---|
172 | invaluable feedback during the <a href="../../more/formal_review_process.htm">formal |
---|
173 | review process</a>. |
---|
174 | <hr> |
---|
175 | <p>Copyright © 2000 Cadenza New Zealand Ltd. Permission to copy, use, modify, |
---|
176 | sell and distribute this document is granted provided this copyright notice |
---|
177 | appears in all copies. This document is provided "as is" without |
---|
178 | express or implied warranty, and with no claim as to its suitability for any |
---|
179 | purpose.</p> |
---|
180 | <p>Revised 28 June 2000</p> |
---|
181 | |
---|
182 | </body> |
---|
183 | |
---|
184 | </html> |
---|