1
0
forked from boostorg/bind

Section on operators.

[SVN r27680]
This commit is contained in:
Peter Dimov
2005-03-15 23:44:42 +00:00
parent c25b216898
commit 6602068638

View File

@@ -27,6 +27,7 @@
to members</A></h4>
<h4 style="MARGIN-LEFT: 40pt"><A href="#nested_binds">Using nested binds for function
composition</A></h4>
<h4 style="MARGIN-LEFT: 40pt"><A href="#operators">Overloaded operators</A></h4>
<h3 style="MARGIN-LEFT: 20pt"><A href="#Examples">Examples</A></h3>
<h4 style="MARGIN-LEFT: 40pt"><A href="#with_algorithms">Using bind with standard
algorithms</A></h4>
@@ -280,6 +281,29 @@ std::for_each(v.begin(), v.end(), bind(apply&lt;void&gt;(), _1, 5));
<P>The header <STRONG>boost/bind/protect.hpp</STRONG> contains an implementation of <STRONG>
protect</STRONG>. To protect a <STRONG>bind</STRONG> function object from
evaluation, use <tt>protect(bind(f, ...))</tt>.</P>
<h3><a name="operators">Overloaded operators</a> (new in Boost 1.34)</h3>
<p>For convenience, the function objects produced by <tt>bind</tt> overload
the logical not operator <STRONG>!</STRONG> and the relational operators <STRONG>==</STRONG>,
<STRONG>!=</STRONG>, <STRONG>&lt;</STRONG>, <STRONG>&lt;=</STRONG>, <STRONG>&gt;</STRONG>,
<STRONG>&gt;=</STRONG>.</p>
<P><tt>!bind(f, ...)</tt> is equivalent to <tt>bind( <EM>logical_not</EM>(), bind(f,
...) )</tt>, where <tt><EM>logical_not</EM></tt> is a function object that
takes one argument <tt>x</tt> and returns <tt>!x</tt>.</P>
<P><tt>bind(f, ...) <EM>op</EM> x</tt>, where <EM>op</EM> is a relational operator,
is equivalent to <tt>bind( <EM>relation</EM>(), bind(f, ...), x )</tt>, where <em>relation</em>
is a function object that takes two arguments <tt>a</tt> and <tt>b</tt> and
returns <tt>a <EM>op</EM> b</tt>.</P>
<P>What this means in practice is that you can conveniently negate the result of
<tt>bind</tt>:</P>
<P><tt>std::remove_if( first, last, !bind( &amp;X::visible, _1 ) ); // remove invisible
objects</tt></P>
<P>and compare the result of <tt>bind</tt> against a value:</P>
<P><tt>std::find_if( first, last, bind( &amp;X::name, _1 ) == "peter" );</tt></P>
<P>against a placeholder:</P>
<P><tt>bind( &amp;X::name, _1 ) == _2</tt></P>
<P>or against another <tt>bind</tt> expression:</P>
<P><tt>std::sort( first, last, bind( &amp;X::name, _1 ) &lt; bind( &amp;X::name, _2 )
); // sort by name</tt></P>
<h2><a name="Examples">Examples</a></h2>
<h3><a name="with_algorithms">Using bind with standard algorithms</a></h3>
<pre>class image;