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>Negators</h1> |
---|
22 | |
---|
23 | <p>The header <nobr><a |
---|
24 | href="../../boost/functional.hpp">functional.hpp</a></nobr> provides |
---|
25 | enhanced versions of both the negator adapters from the C++ Standard |
---|
26 | Library (§20.3.5):</p> |
---|
27 | |
---|
28 | <ul> |
---|
29 | <li><tt>unary_negate</tt></li> |
---|
30 | <li><tt>binary_negate</tt></li> |
---|
31 | </ul> |
---|
32 | |
---|
33 | <p>As well as the corresponding helper functions</p> |
---|
34 | |
---|
35 | <ul> |
---|
36 | <li><tt>not1</tt></li> |
---|
37 | <li><tt>not2</tt></li> |
---|
38 | </ul> |
---|
39 | |
---|
40 | <p>However, the negators in this library improve on the standard |
---|
41 | versions in two ways: |
---|
42 | |
---|
43 | <ul> |
---|
44 | <li>They use <a href="function_traits.html">function object traits</a> |
---|
45 | to avoid the need for <tt><nobr>ptr_fun</nobr></tt> when negating a |
---|
46 | function rather than an adaptable function object. |
---|
47 | </li> |
---|
48 | <li>They use Boost <nobr><a |
---|
49 | href="../utility/call_traits.htm">call traits</a></nobr> to determine |
---|
50 | the best way to declare their arguments and pass them through |
---|
51 | to the adapted function (see <a href="#arguments">below</a>). |
---|
52 | </li> |
---|
53 | </ul> |
---|
54 | |
---|
55 | <h3>Usage</h3> |
---|
56 | |
---|
57 | <p>Usage is identical to the standard negators. For example,</p> |
---|
58 | |
---|
59 | <blockquote><pre> |
---|
60 | bool bad(const Foo &foo) { ... } |
---|
61 | ... |
---|
62 | std::vector<Foo> c; |
---|
63 | ... |
---|
64 | std::find_if(c.begin(), c.end(), boost::not1(bad)); |
---|
65 | </pre></blockquote> |
---|
66 | |
---|
67 | <h3 id="arguments">Argument Types</h3> |
---|
68 | |
---|
69 | <p>The C++ Standard <nobr>(§20.3.5)</nobr> defines unary negate |
---|
70 | like this (binary negate is similar):</p> |
---|
71 | |
---|
72 | <blockquote><pre> |
---|
73 | template <class Predicate> |
---|
74 | class unary_negate |
---|
75 | : public unary_function<typename Predicate::argument_type,bool> { |
---|
76 | public: |
---|
77 | explicit unary_negate(const Predicate& pred); |
---|
78 | bool operator()(<strong>const typename Predicate::argument_type&</strong> x) const; |
---|
79 | };</pre></blockquote> |
---|
80 | |
---|
81 | <p>Note that if the Predicate's <nobr><tt>argument_type</tt></nobr> is |
---|
82 | a reference, the type of <nobr><tt>operator()</tt>'s</nobr> argument |
---|
83 | would be a reference to a reference. Currently this is illegal in C++ |
---|
84 | (but see the <a |
---|
85 | href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106"> |
---|
86 | C++ standard core language active issues list</a>).</p> |
---|
87 | |
---|
88 | <p>However, if we instead defined <nobr><tt>operator()</tt></nobr> |
---|
89 | to accept Predicate's argument_type unmodified, this would be |
---|
90 | needlessly inefficient if it were a value type; the argument would be |
---|
91 | copied twice - once when calling <nobr><tt>unary_negate</tt>'s</nobr> |
---|
92 | <nobr><tt>operator()</tt></nobr>, and again when <nobr><tt>operator()</tt></nobr> |
---|
93 | called the adapted function.</p> |
---|
94 | |
---|
95 | <p>So how we want to declare the argument for |
---|
96 | <nobr><tt>operator()</tt></nobr> depends on whether or not the |
---|
97 | Predicate's <nobr><tt>argument_type</tt></nobr> is a reference. If it |
---|
98 | is a reference, we want to declare it simply as |
---|
99 | <nobr><tt>argument_type</tt></nobr>; if it is a value we want to |
---|
100 | declare it as <nobr><tt>const argument_type&</tt></nobr>. |
---|
101 | |
---|
102 | <p>The Boost <nobr><a |
---|
103 | href="../utility/call_traits.htm">call_traits</a></nobr> class |
---|
104 | template contains a <tt><nobr>param_type</nobr></tt> typedef, which |
---|
105 | uses partial specialisation to make precisely this decision. If we were |
---|
106 | to declare <nobr><tt>operator()</tt></nobr> as</p> |
---|
107 | |
---|
108 | <blockquote><pre> |
---|
109 | bool operator()(typename call_traits<typename Predicate::argument_type>::param_type x) const |
---|
110 | </pre></blockquote> |
---|
111 | |
---|
112 | <p>the desired result would be achieved - we would eliminate |
---|
113 | references to references without loss of efficiency. In fact, the |
---|
114 | actual declaration is slightly more complicated because of the use of |
---|
115 | function object traits, but the effect remains the same.</p> |
---|
116 | |
---|
117 | <h3>Limitations</h3> |
---|
118 | |
---|
119 | <p>Both the function object traits and call traits used to realise |
---|
120 | these improvements rely on partial specialisation, these improvements |
---|
121 | are only available on compilers that support that feature. With other |
---|
122 | compilers, the negators in this library behave very much like those |
---|
123 | in the Standard - <nobr><tt>ptr_fun</tt></nobr> will be required to |
---|
124 | adapt functions, and references to references will not be avoided. |
---|
125 | |
---|
126 | <hr> |
---|
127 | <p>Copyright © 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> |
---|