1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 <meta http-equiv="Content-Language" content="en-us">
6 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
8 <title>Boost Function Object Adapter Library</title>
11 <body bgcolor="#FFFFFF" text="#000000">
12 <table border="1" bgcolor="#007F7F" cellpadding="2" summary="">
14 <td bgcolor="#FFFFFF"><img src="../../boost.png" alt=
15 "boost.png (6897 bytes)" width="277" height="86"></td>
17 <td><a href="../../index.htm"><font face="Arial" color=
18 "#FFFFFF"><big>Home</big></font></a></td>
20 <td><a href="../libraries.htm"><font face="Arial" color=
21 "#FFFFFF"><big>Libraries</big></font></a></td>
23 <td><a href="http://www.boost.org/people/people.htm"><font face="Arial" color=
24 "#FFFFFF"><big>People</big></font></a></td>
26 <td><a href="http://www.boost.org/more/faq.htm"><font face="Arial" color=
27 "#FFFFFF"><big>FAQ</big></font></a></td>
29 <td><a href="../../more/index.htm"><font face="Arial" color=
30 "#FFFFFF"><big>More</big></font></a></td>
36 <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
37 provides enhanced versions of both the negator adapters from the C++
38 Standard Library (§20.3.5):</p>
41 <li><tt>unary_negate</tt></li>
43 <li><tt>binary_negate</tt></li>
46 <p>As well as the corresponding helper functions</p>
49 <li><tt>not1</tt></li>
51 <li><tt>not2</tt></li>
54 <p>However, the negators in this library improve on the standard versions
58 <li>They use <a href="function_traits.html">function object traits</a> to
59 avoid the need for <tt>ptr_fun</tt> when negating a function rather than
60 an adaptable function object.</li>
62 <li>They use Boost <a href=
63 "../utility/call_traits.htm">call traits</a> to determine the best
64 way to declare their arguments and pass them through to the adapted
65 function (see <a href="#arguments">below</a>).</li>
70 <p>Usage is identical to the standard negators. For example,</p>
74 bool bad(const Foo &foo) { ... }
76 std::vector<Foo> c;
78 std::find_if(c.begin(), c.end(), boost::not1(bad));
82 <h3 id="arguments">Argument Types</h3>
84 <p>The C++ Standard (§20.3.5) defines unary negate like this (binary
85 negate is similar):</p>
89 template <class Predicate>
91 : public unary_function<typename Predicate::argument_type,bool> {
93 explicit unary_negate(const Predicate& pred);
94 bool operator()(<strong>const typename Predicate::argument_type&</strong> x) const;
99 <p>Note that if the Predicate's <tt>argument_type</tt> is a reference, the
100 type of <tt>operator()</tt>'s argument would be a reference to a reference.
101 Currently this is illegal in C++ (but see the <a href=
102 "http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">C++
103 standard core language active issues list</a>).</p>
105 <p>However, if we instead defined <tt>operator()</tt> to accept Predicate's
106 argument_type unmodified, this would be needlessly inefficient if it were a
107 value type; the argument would be copied twice - once when calling
108 <tt>unary_negate</tt>'s <tt>operator()</tt>, and again when
109 <tt>operator()</tt> called the adapted function.</p>
111 <p>So how we want to declare the argument for <tt>operator()</tt> depends
112 on whether or not the Predicate's <tt>argument_type</tt> is a reference. If
113 it is a reference, we want to declare it simply as <tt>argument_type</tt>;
114 if it is a value we want to declare it as
115 <tt>const argument_type&</tt>.</p>
117 <p>The Boost <a href="../utility/call_traits.htm">call_traits</a> class
118 template contains a <tt>param_type</tt> typedef, which uses partial
119 specialisation to make precisely this decision. If we were to declare
120 <tt>operator()</tt> as</p>
124 bool operator()(typename call_traits<typename Predicate::argument_type>::param_type x) const
128 <p>the desired result would be achieved - we would eliminate references to
129 references without loss of efficiency. In fact, the actual declaration is
130 slightly more complicated because of the use of function object traits, but
131 the effect remains the same.</p>
135 <p>Both the function object traits and call traits used to realise these
136 improvements rely on partial specialisation, these improvements are only
137 available on compilers that support that feature. With other compilers, the
138 negators in this library behave very much like those in the Standard -
139 <tt>ptr_fun</tt> will be required to adapt functions, and references to
140 references will not be avoided.</p>
143 <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
144 "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
145 height="31" width="88"></a></p>
148 <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
149 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
151 <p><i>Copyright © 2000 Cadenza New Zealand Ltd.</i></p>
153 <p><i>Distributed under the Boost Software License, Version 1.0. (See
154 accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
156 "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>