File: negators.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (132 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Boost Function Object Adapter Library</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<table border="1" bgcolor="#007F7F" cellpadding="2">
  <tr>
    <td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" WIDTH="277" HEIGHT="86"></td>
    <td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td>
    <td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td>
    <td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td>
    <td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td>
    <td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td>
  </tr>
</table>

<h1>Negators</h1>

<p>The header <nobr><a
href="../../boost/functional.hpp">functional.hpp</a></nobr> provides
enhanced versions of both the negator adapters from the C++ Standard
Library (&sect;20.3.5):</p>

<ul>
<li><tt>unary_negate</tt></li>
<li><tt>binary_negate</tt></li>
</ul>

<p>As well as the corresponding helper functions</p>

<ul>
<li><tt>not1</tt></li>
<li><tt>not2</tt></li>
</ul>

<p>However, the negators in this library improve on the standard
versions in two ways:

<ul>
<li>They use <a href="function_traits.html">function object traits</a>
to avoid the need for <tt><nobr>ptr_fun</nobr></tt> when negating a
function rather than an adaptable function object.
</li>
<li>They use Boost <nobr><a
href="../utility/call_traits.htm">call traits</a></nobr> to determine
the best way to declare their arguments and pass them through
to the adapted function (see <a href="#arguments">below</a>).  
</li>
</ul>

<h3>Usage</h3>

<p>Usage is identical to the standard negators.  For example,</p>

<blockquote><pre>
bool bad(const Foo &foo) { ... }
...
std::vector&lt;Foo&gt; c;
...
std::find_if(c.begin(), c.end(), boost::not1(bad));
</pre></blockquote>

<h3 id="arguments">Argument Types</h3>

<p>The C++ Standard <nobr>(&sect;20.3.5)</nobr> defines unary negate
like this (binary negate is similar):</p>

<blockquote><pre>
template &lt;class Predicate&gt;
  class unary_negate
    : public unary_function&lt;typename Predicate::argument_type,bool&gt; {
public:
  explicit unary_negate(const Predicate& pred);
  bool operator()(<strong>const typename Predicate::argument_type&</strong> x) const;
};</pre></blockquote>

<p>Note that if the Predicate's <nobr><tt>argument_type</tt></nobr> is
a reference, the type of <nobr><tt>operator()</tt>'s</nobr> argument
would be a reference to a reference.  Currently this is illegal in C++
(but see the <a
href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#106">
C++ standard core language active issues list</a>).</p>

<p>However, if we instead defined <nobr><tt>operator()</tt></nobr>
to accept Predicate's argument_type unmodified, this would be
needlessly inefficient if it were a value type; the argument would be
copied twice - once when calling <nobr><tt>unary_negate</tt>'s</nobr>
<nobr><tt>operator()</tt></nobr>, and again when <nobr><tt>operator()</tt></nobr>
called the adapted function.</p>

<p>So how we want to declare the argument for
<nobr><tt>operator()</tt></nobr> depends on whether or not the
Predicate's <nobr><tt>argument_type</tt></nobr> is a reference.  If it
is a reference, we want to declare it simply as
<nobr><tt>argument_type</tt></nobr>; if it is a value we want to
declare it as <nobr><tt>const argument_type&</tt></nobr>.

<p>The Boost <nobr><a
href="../utility/call_traits.htm">call_traits</a></nobr> class
template contains a <tt><nobr>param_type</nobr></tt> typedef, which
uses partial specialisation to make precisely this decision.  If we were
to declare <nobr><tt>operator()</tt></nobr> as</p>

<blockquote><pre>
bool operator()(typename call_traits&lt;typename Predicate::argument_type&gt;::param_type x) const
</pre></blockquote>

<p>the desired result would be achieved - we would eliminate
references to references without loss of efficiency.  In fact, the
actual declaration is slightly more complicated because of the use of
function object traits, but the effect remains the same.</p>

<h3>Limitations</h3>

<p>Both the function object traits and call traits used to realise
these improvements rely on partial specialisation, these improvements
are only available on compilers that support that feature.  With other
compilers, the negators in this library behave very much like those
in the Standard - <nobr><tt>ptr_fun</tt></nobr> will be required to 
adapt functions, and references to references will not be avoided.

<hr>
<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>

<p>Revised 28 June 2000</p>

</body>
</html>