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>Function Pointer Adapters</h1> |
---|
22 | |
---|
23 | <p>The header <nobr><a |
---|
24 | href="../../boost/functional.hpp">functional.hpp</a></nobr> provides |
---|
25 | enhanced versions of both the function pointer adapters from the C++ |
---|
26 | Standard Library <nobr>(§ 20.3.7):</nobr></p> |
---|
27 | |
---|
28 | <ul> |
---|
29 | <li><tt>pointer_to_unary_function</tt></li> |
---|
30 | <li><tt>pointer_to_binary_function</tt></li> |
---|
31 | </ul> |
---|
32 | |
---|
33 | <p>As well as the corresponding helper function template:</p> |
---|
34 | |
---|
35 | <ul> |
---|
36 | <li><tt>ptr_fun</tt></li> |
---|
37 | </ul> |
---|
38 | |
---|
39 | <p>However, you should not need to use the adapters in conjunction |
---|
40 | with the adapters in this library due to our use of <a |
---|
41 | href="function_traits.html">function object traits</a>. You will |
---|
42 | however need to use them if your implementation fails to work properly |
---|
43 | with our traits classes (due to lack if partial specialisation), or if |
---|
44 | you wish to use a function object adapter from a third party. |
---|
45 | |
---|
46 | <h3>Usage</h3> |
---|
47 | |
---|
48 | <p>If you need to use these adapters, usage is identical to the |
---|
49 | standard function pointer adapters. For example,</p> |
---|
50 | |
---|
51 | <blockquote><pre> |
---|
52 | bool bad(std::string foo) { ... } |
---|
53 | ... |
---|
54 | std::vector<std::string> c; |
---|
55 | ... |
---|
56 | std::vector<std::string>::iterator it |
---|
57 | = std::find_if(c.begin(), c.end(), std::not1(boost::ptr_fun(bad))); |
---|
58 | </pre></blockquote> |
---|
59 | |
---|
60 | <p>Note however that this library contains enhanced <a |
---|
61 | href="negators.html">negators</a> that support function object traits, |
---|
62 | so the line above could equally be written |
---|
63 | |
---|
64 | <blockquote><pre> |
---|
65 | std::vector<std::string>::iterator it |
---|
66 | = std::find_if(c.begin(), c.end(), boost::not1(bad)); |
---|
67 | </pre></blockquote> |
---|
68 | |
---|
69 | <h3>Argument Types</h3> |
---|
70 | |
---|
71 | <p>The standard defines |
---|
72 | <nobr><tt>pointer_to_unary_function</tt></nobr> like this |
---|
73 | <nobr>(§20.3.8 ¶2):</nobr> |
---|
74 | |
---|
75 | <blockquote><pre> |
---|
76 | template <class Arg, class Result> |
---|
77 | class pointer_to_unary_function : public unary_function<Arg, Result> { |
---|
78 | public: |
---|
79 | explicit pointer_to_unary_function(Result (* f)(<strong>Arg</strong>)); |
---|
80 | Result operator()(<strong>Arg</strong> x) const; |
---|
81 | }; |
---|
82 | </pre></blockquote> |
---|
83 | |
---|
84 | <p>Note that the argument to <nobr><tt>operator()</tt></nobr> is |
---|
85 | exactly the same type as the argument to the wrapped function. If this |
---|
86 | is a value type, the argument will be passed by value and copied twice. |
---|
87 | <nobr><tt>pointer_to_binary_function</tt></nobr> has a similar problem. |
---|
88 | |
---|
89 | <p>However, if we were to try and eliminate this inefficiency by |
---|
90 | instead declaring the argument as <nobr><tt>const Arg&</tt></nobr>, then |
---|
91 | if Arg were a reference type, we would have a reference to a reference, |
---|
92 | which is currently illegal (but see <a |
---|
93 | href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">C++ |
---|
94 | core language issue number 106)</a> |
---|
95 | |
---|
96 | <p>So the way in which we want to declare the argument for |
---|
97 | <nobr><tt>operator()</tt></nobr> depends on whether or not the |
---|
98 | wrapped function's argument is a reference. If it |
---|
99 | is a reference, we want to declare it simply as |
---|
100 | <nobr><tt>Arg</tt></nobr>; if it is a value we want to |
---|
101 | declare it as <nobr><tt>const Arg&</tt></nobr>. |
---|
102 | |
---|
103 | <p>The Boost <nobr><a |
---|
104 | href="../utility/call_traits.htm">call_traits</a></nobr> class |
---|
105 | template contains a <tt><nobr>param_type</nobr></tt> typedef, which |
---|
106 | uses partial specialisation to make precisely this decision. By |
---|
107 | declaring the <nobr><tt>operator()</tt></nobr> as |
---|
108 | |
---|
109 | <blockquote><pre> |
---|
110 | Result operator()(typename call_traits<Arg>::param_type x) const |
---|
111 | </pre></blockquote> |
---|
112 | |
---|
113 | <p>we achieve the desired result - we improve efficiency without |
---|
114 | generating references to references.</p> |
---|
115 | |
---|
116 | <h3>Limitations</h3> |
---|
117 | |
---|
118 | <p>The call traits template used to realise this improvement relies |
---|
119 | on partial specialisation, so this improvement is only available on |
---|
120 | compilers that support that feature. With other compilers, the |
---|
121 | argument passed to the function will always be passed by |
---|
122 | reference, thus generating the possibility of references to references. |
---|
123 | |
---|
124 | <hr> |
---|
125 | |
---|
126 | <p>Copyright © 2000 Cadenza New Zealand Ltd. Permission to copy, |
---|
127 | use, modify, sell and distribute this document is granted provided |
---|
128 | this copyright notice appears in all copies. This document is provided |
---|
129 | "as is" without express or implied warranty, and with no claim as to |
---|
130 | its suitability for any purpose.</p> |
---|
131 | |
---|
132 | <p>Revised 28 June 2000</p> |
---|
133 | |
---|
134 | </body> |
---|
135 | </html> |
---|