Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added boost

File size: 5.0 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>Negators</h1>
22
23<p>The header <nobr><a
24href="../../boost/functional.hpp">functional.hpp</a></nobr> provides
25enhanced versions of both the negator adapters from the C++ Standard
26Library (&sect;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
41versions in two ways:
42
43<ul>
44<li>They use <a href="function_traits.html">function object traits</a>
45to avoid the need for <tt><nobr>ptr_fun</nobr></tt> when negating a
46function rather than an adaptable function object.
47</li>
48<li>They use Boost <nobr><a
49href="../utility/call_traits.htm">call traits</a></nobr> to determine
50the best way to declare their arguments and pass them through
51to 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>
60bool bad(const Foo &foo) { ... }
61...
62std::vector&lt;Foo&gt; c;
63...
64std::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>(&sect;20.3.5)</nobr> defines unary negate
70like this (binary negate is similar):</p>
71
72<blockquote><pre>
73template &lt;class Predicate&gt;
74  class unary_negate
75    : public unary_function&lt;typename Predicate::argument_type,bool&gt; {
76public:
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
82a reference, the type of <nobr><tt>operator()</tt>'s</nobr> argument
83would be a reference to a reference.  Currently this is illegal in C++
84(but see the <a
85href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">
86C++ standard core language active issues list</a>).</p>
87
88<p>However, if we instead defined <nobr><tt>operator()</tt></nobr>
89to accept Predicate's argument_type unmodified, this would be
90needlessly inefficient if it were a value type; the argument would be
91copied 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>
93called 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
97Predicate's <nobr><tt>argument_type</tt></nobr> is a reference.  If it
98is a reference, we want to declare it simply as
99<nobr><tt>argument_type</tt></nobr>; if it is a value we want to
100declare it as <nobr><tt>const argument_type&</tt></nobr>.
101
102<p>The Boost <nobr><a
103href="../utility/call_traits.htm">call_traits</a></nobr> class
104template contains a <tt><nobr>param_type</nobr></tt> typedef, which
105uses partial specialisation to make precisely this decision.  If we were
106to declare <nobr><tt>operator()</tt></nobr> as</p>
107
108<blockquote><pre>
109bool operator()(typename call_traits&lt;typename Predicate::argument_type&gt;::param_type x) const
110</pre></blockquote>
111
112<p>the desired result would be achieved - we would eliminate
113references to references without loss of efficiency.  In fact, the
114actual declaration is slightly more complicated because of the use of
115function 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
120these improvements rely on partial specialisation, these improvements
121are only available on compilers that support that feature.  With other
122compilers, the negators in this library behave very much like those
123in the Standard - <nobr><tt>ptr_fun</tt></nobr> will be required to
124adapt functions, and references to references will not be avoided.
125
126<hr>
127<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>
128
129<p>Revised 28 June 2000</p>
130
131</body>
132</html>
Note: See TracBrowser for help on using the repository browser.