Fixed a bug in the semantics of less_pointees()

[SVN r21132]
This commit is contained in:
Fernando Cacciola 2003-12-04 01:47:31 +00:00
parent 61fb5a0b8f
commit ec36cd8c54
2 changed files with 36 additions and 8 deletions

View File

@ -97,22 +97,50 @@ But boost::optional<T>, on the other hand, which is also a model of Option
deep-copy and deep-relational semantics.<br> deep-copy and deep-relational semantics.<br>
If generic code is written for this concept, it is important not to use relational If generic code is written for this concept, it is important not to use relational
operators directly because the semantics might be different depending on the actual type.<br> operators directly because the semantics might be different depending on the actual type.<br>
Still, the concept itsef can be used to define a <i>deep</i> equality-test that can Still, the concept itsef can be used to define <i>deep</i> relational tests that can
be used in generic code with any type which models OptionalPointee:</p> be used in generic code with any type which models OptionalPointee:</p>
<a name="equal"></a> <a name="equal"></a>
<pre> <p><u>Equivalence relation:</u></p>
template&lt;class OptionalPointee&gt; <pre>template&lt;class OptionalPointee&gt;
inline inline
bool equal_pointees ( OptionalPointee const&amp; x, OptionalPointee const&amp; y ) bool equal_pointees ( OptionalPointee const&amp; x, OptionalPointee const&amp; y )
{ {
return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ; return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ;
} }
template&lt;class OptionalPointee&gt;
struct equal_pointees_t : std::binary_function&lt;bool,OptionalPointee,OptionalPointee&gt;
{
bool operator() ( OptionalPointee const& x, OptionalPointee const& y ) const
{ return equal_pointees(x,y) ; }
} ;
</pre> </pre>
<p>The preceding generic function has the following semantics:<br> <p>The preceding generic function and function object have the following semantics:<br>
If both x and y have valid pointees, it compares pointee's values via (*x == *y).<br> If both x and y have valid pointees, it compares values via (*x == *y) or (*x &lt;
*y).<br>
If only one has a valid pointee, returns false.<br> If only one has a valid pointee, returns false.<br>
If both have invalid pointees, returns true.</p> If both have invalid pointees, returns true.</p>
<p><code>equal_pointees()</code> is implemented in <a href="../../boost/optional.hpp">optional.hpp</a></p> <a name="less"></a>
<p><u>Less-than relation:</u></p>
<pre>template&lt;class OptionalPointee&gt;
inline
bool less_pointees ( OptionalPointee const&amp; x, OptionalPointee const&amp; y )
{
return (!x) != (!y) ? false : ( !x ? false : (*x) == (*y) ) ;
}
template&lt;class OptionalPointee&gt;
struct less_pointees_t : std::binary_function&lt;bool,OptionalPointee,OptionalPointee&gt;
{
bool operator() ( OptionalPointee const& x, OptionalPointee const& y ) const
{ return less_pointees(x,y) ; }
} ;
</pre>
<p>The preceding generic function and function object have the following semantics:<br>
If both x and y have valid pointees, it compares values via (*x == *y) or (*x &lt;
*y).<br>
If only one has a valid pointee, returns false.<br>
If both have invalid pointees, returns false.</p>
<p>All these functions and function
objects are is implemented in <a href="../../boost/utility/compare_pointees.hpp">compare_pointees.hpp</a></p>
<p>Notice that OptionalPointee does not imply aliasing (and optional&lt;&gt; for instance does not alias); <p>Notice that OptionalPointee does not imply aliasing (and optional&lt;&gt; for instance does not alias);
so direct usage of relational operators with the implied aliasing of shallow semantics so direct usage of relational operators with the implied aliasing of shallow semantics
-as with pointers- should not be used with generic code written for this concept.</p> -as with pointers- should not be used with generic code written for this concept.</p>
@ -127,4 +155,4 @@ based on the original concept developed by Augustus Saunders.
</TD></TR></TABLE> </TD></TR></TABLE>
</BODY> </BODY>
</HTML> </HTML>

View File

@ -52,7 +52,7 @@ template<class OptionalPointee>
inline inline
bool less_pointees ( OptionalPointee const& x, OptionalPointee const& y ) bool less_pointees ( OptionalPointee const& x, OptionalPointee const& y )
{ {
return !y ? false : ( !x ? true : (*x) < (*y) ) ; return !y ? false : ( !x ? false : (*x) < (*y) ) ;
} }
template<class OptionalPointee> template<class OptionalPointee>