Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/ptr_fun.html @ 14

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

added boost

File size: 4.9 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>Function Pointer Adapters</h1>
22
23<p>The header <nobr><a
24href="../../boost/functional.hpp">functional.hpp</a></nobr> provides
25enhanced versions of both the function pointer adapters from the C++
26Standard Library <nobr>(&sect 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
40with the adapters in this library due to our use of <a
41href="function_traits.html">function object traits</a>.  You will
42however need to use them if your implementation fails to work properly
43with our traits classes (due to lack if partial specialisation), or if
44you 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
49standard function pointer adapters.  For example,</p>
50
51<blockquote><pre>
52bool bad(std::string foo) { ... }
53...
54std::vector&lt;std::string&gt; c;
55...
56std::vector&lt;std::string&gt;::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
61href="negators.html">negators</a> that support function object traits,
62so the line above could equally be written
63
64<blockquote><pre>
65std::vector&lt;std::string&gt;::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>(&sect;20.3.8 &para;2):</nobr>
74
75<blockquote><pre>
76template &lt;class Arg, class Result&gt;
77class pointer_to_unary_function : public unary_function&lt;Arg, Result&gt; {
78public:
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
85exactly the same type as the argument to the wrapped function.  If this
86is 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
90instead declaring the argument as <nobr><tt>const Arg&</tt></nobr>, then
91if Arg were a reference type, we would have a reference to a reference,
92which is currently illegal (but see <a
93href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">C++
94core 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
98wrapped function's argument is a reference.  If it
99is a reference, we want to declare it simply as
100<nobr><tt>Arg</tt></nobr>; if it is a value we want to
101declare it as <nobr><tt>const Arg&</tt></nobr>.
102
103<p>The Boost <nobr><a
104href="../utility/call_traits.htm">call_traits</a></nobr> class
105template contains a <tt><nobr>param_type</nobr></tt> typedef, which
106uses partial specialisation to make precisely this decision.  By
107declaring the <nobr><tt>operator()</tt></nobr> as
108
109<blockquote><pre>
110Result operator()(typename call_traits&lt;Arg&gt;::param_type x) const
111</pre></blockquote>
112
113<p>we achieve the desired result - we improve efficiency without
114generating references to references.</p>
115
116<h3>Limitations</h3>
117
118<p>The call traits template used to realise this improvement relies
119on partial specialisation, so this improvement is only available on
120compilers that support that feature.  With other compilers, the
121argument passed to the function will always be passed by
122reference, thus generating the possibility of references to references.
123
124<hr>
125
126<p>Copyright &copy; 2000 Cadenza New Zealand Ltd.  Permission to copy,
127use, modify, sell and distribute this document is granted provided
128this copyright notice appears in all copies. This document is provided
129"as is" without express or implied warranty, and with no claim as to
130its suitability for any purpose.</p>
131
132<p>Revised 28 June 2000</p>
133
134</body>
135</html>
Note: See TracBrowser for help on using the repository browser.