forked from boostorg/functional
Merged L & C issue fixes from trunk to branch.
[SVN r36246]
This commit is contained in:
+117
-91
@@ -1,132 +1,158 @@
|
||||
<html>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<title>Boost Function Object Adapter Library</title>
|
||||
<meta http-equiv="Content-Language" content="en-us">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
|
||||
|
||||
<title>Boost Function Object Adapter Library</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<table border="1" bgcolor="#007F7F" cellpadding="2" summary="">
|
||||
<tr>
|
||||
<td bgcolor="#FFFFFF"><img src="../../boost.png" alt=
|
||||
"boost.png (6897 bytes)" width="277" height="86"></td>
|
||||
|
||||
<table border="1" bgcolor="#007F7F" cellpadding="2">
|
||||
<tr>
|
||||
<td bgcolor="#FFFFFF"><img src="../../boost.png" alt="boost.png (6897 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>
|
||||
<td><a href="../../index.htm"><font face="Arial" color=
|
||||
"#FFFFFF"><big>Home</big></font></a></td>
|
||||
|
||||
<h1>Negators</h1>
|
||||
<td><a href="../libraries.htm"><font face="Arial" color=
|
||||
"#FFFFFF"><big>Libraries</big></font></a></td>
|
||||
|
||||
<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 (§20.3.5):</p>
|
||||
<td><a href="../../people/people.htm"><font face="Arial" color=
|
||||
"#FFFFFF"><big>People</big></font></a></td>
|
||||
|
||||
<ul>
|
||||
<li><tt>unary_negate</tt></li>
|
||||
<li><tt>binary_negate</tt></li>
|
||||
</ul>
|
||||
<td><a href="../../more/faq.htm"><font face="Arial" color=
|
||||
"#FFFFFF"><big>FAQ</big></font></a></td>
|
||||
|
||||
<p>As well as the corresponding helper functions</p>
|
||||
<td><a href="../../more/index.htm"><font face="Arial" color=
|
||||
"#FFFFFF"><big>More</big></font></a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<ul>
|
||||
<li><tt>not1</tt></li>
|
||||
<li><tt>not2</tt></li>
|
||||
</ul>
|
||||
<h1>Negators</h1>
|
||||
|
||||
<p>However, the negators in this library improve on the standard
|
||||
versions in two ways:
|
||||
<p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
|
||||
provides enhanced versions of both the negator adapters from the C++
|
||||
Standard Library (§20.3.5):</p>
|
||||
|
||||
<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>
|
||||
<ul>
|
||||
<li><tt>unary_negate</tt></li>
|
||||
|
||||
<h3>Usage</h3>
|
||||
<li><tt>binary_negate</tt></li>
|
||||
</ul>
|
||||
|
||||
<p>Usage is identical to the standard negators. For example,</p>
|
||||
<p>As well as the corresponding helper functions</p>
|
||||
|
||||
<blockquote><pre>
|
||||
bool bad(const Foo &foo) { ... }
|
||||
<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:</p>
|
||||
|
||||
<ul>
|
||||
<li>They use <a href="function_traits.html">function object traits</a> to
|
||||
avoid the need for <tt>ptr_fun</tt> when negating a function rather than
|
||||
an adaptable function object.</li>
|
||||
|
||||
<li>They use Boost <a href=
|
||||
"../utility/call_traits.htm">call traits</a> 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<Foo> c;
|
||||
...
|
||||
std::find_if(c.begin(), c.end(), boost::not1(bad));
|
||||
</pre></blockquote>
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h3 id="arguments">Argument Types</h3>
|
||||
<h3 id="arguments">Argument Types</h3>
|
||||
|
||||
<p>The C++ Standard <nobr>(§20.3.5)</nobr> defines unary negate
|
||||
like this (binary negate is similar):</p>
|
||||
<p>The C++ Standard (§20.3.5) defines unary negate like this (binary
|
||||
negate is similar):</p>
|
||||
|
||||
<blockquote><pre>
|
||||
<blockquote>
|
||||
<pre>
|
||||
template <class Predicate>
|
||||
class unary_negate
|
||||
: public unary_function<typename Predicate::argument_type,bool> {
|
||||
public:
|
||||
explicit unary_negate(const Predicate& pred);
|
||||
bool operator()(<strong>const typename Predicate::argument_type&</strong> x) const;
|
||||
};</pre></blockquote>
|
||||
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>Note that if the Predicate's <tt>argument_type</tt> is a reference, the
|
||||
type of <tt>operator()</tt>'s 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>However, if we instead defined <tt>operator()</tt> 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
|
||||
<tt>unary_negate</tt>'s <tt>operator()</tt>, and again when
|
||||
<tt>operator()</tt> 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>So how we want to declare the argument for <tt>operator()</tt> depends
|
||||
on whether or not the Predicate's <tt>argument_type</tt> is a reference. If
|
||||
it is a reference, we want to declare it simply as <tt>argument_type</tt>;
|
||||
if it is a value we want to declare it as
|
||||
<tt>const argument_type&</tt>.</p>
|
||||
|
||||
<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>
|
||||
<p>The Boost <a href="../utility/call_traits.htm">call_traits</a> class
|
||||
template contains a <tt>param_type</tt> typedef, which uses partial
|
||||
specialisation to make precisely this decision. If we were to declare
|
||||
<tt>operator()</tt> as</p>
|
||||
|
||||
<blockquote><pre>
|
||||
<blockquote>
|
||||
<pre>
|
||||
bool operator()(typename call_traits<typename Predicate::argument_type>::param_type x) const
|
||||
</pre></blockquote>
|
||||
</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>
|
||||
<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>
|
||||
<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.
|
||||
<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 -
|
||||
<tt>ptr_fun</tt> will be required to adapt functions, and references to
|
||||
references will not be avoided.</p>
|
||||
<hr>
|
||||
|
||||
<hr>
|
||||
<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>
|
||||
<p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
|
||||
"http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
|
||||
height="31" width="88"></a></p>
|
||||
|
||||
<p>Revised 28 June 2000</p>
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
|
||||
December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
|
||||
|
||||
<p><i>Copyright © 2000 Cadenza New Zealand Ltd.</i></p>
|
||||
|
||||
<p><i>Distributed under the Boost Software License, Version 1.0. (See
|
||||
accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
|
||||
copy at <a href=
|
||||
"http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user