mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-13 00:32:14 +00:00
131 lines
6.0 KiB
HTML
131 lines
6.0 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>safe_compare<T, U></title>
|
|
<link rel="stylesheet" href="boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
|
|
<link rel="home" href="index.html" title="Safe Numerics">
|
|
<link rel="up" href="functions.html" title="Functions">
|
|
<link rel="prev" href="safe_cast.html" title="safe_cast<T, U>">
|
|
<link rel="next" href="exception_safety.html" title="Exception Safety">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
|
|
<td align="center"><a href="../../index.html">Home</a></td>
|
|
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="safe_cast.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="exception_safety.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h3 class="title">
|
|
<a name="safe_numerics.safe_compare"></a>safe_compare<T, U></h3></div></div></div>
|
|
<div class="toc"><dl>
|
|
<dt><span class="section"><a href="safe_compare.html#idm354477307616">Synopsis</a></span></dt>
|
|
<dt><span class="section"><a href="safe_compare.html#idm354477305520">Description</a></span></dt>
|
|
<dt><span class="section"><a href="safe_compare.html#idm354477302976">Type requirements</a></span></dt>
|
|
<dt><span class="section"><a href="safe_compare.html#idm354477293424">Header</a></span></dt>
|
|
<dt><span class="section"><a href="safe_compare.html#idm354477291360">Example of use</a></span></dt>
|
|
</dl></div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm354477307616"></a>Synopsis</h4></div></div></div>
|
|
<p>safe_compare is several functions:.</p>
|
|
<pre class="programlisting">template<class T, class U>
|
|
bool safe_compare::less_than(const T & lhs, const U & rhs);
|
|
|
|
template<class T, class U>
|
|
bool safe_compare::less_than_equal(const T & lhs, const U & rhs);
|
|
|
|
template<class T, class U>
|
|
bool safe_compare::greater_than(const T & lhs, const U & rhs);
|
|
|
|
template<class T, class U>
|
|
bool safe_compare::greater_than_equal(const T & lhs, const U & rhs);
|
|
|
|
template<class T, class U>
|
|
bool safe_compare::equal(const T & lhs, const U & rhs);
|
|
|
|
template<class T, class U>
|
|
bool safe_compare::not_equal(const T & lhs, const U & rhs);</pre>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm354477305520"></a>Description</h4></div></div></div>
|
|
<p>With normal comparison operators, comparison of unsigned types to
|
|
signed types will be done by converting the unsigned type to a signed type
|
|
before comparing. Unfortunately this is not always possible. Most C++
|
|
compilers will emit an warning message when this is possible but won't
|
|
check that an error is made in the conversion. This function guarantees a
|
|
correct result regardless of the types of the arguments. It's used in the
|
|
implementation of the safe numerics library. It has been made publicly
|
|
accessible in because it might be useful in other contexts.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm354477302976"></a>Type requirements</h4></div></div></div>
|
|
<div class="informaltable"><table class="table">
|
|
<colgroup>
|
|
<col align="left">
|
|
<col align="left">
|
|
</colgroup>
|
|
<thead><tr>
|
|
<th align="left">Type</th>
|
|
<th align="left">Requirements</th>
|
|
</tr></thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">T</code></td>
|
|
<td align="left"><a class="link" href="numeric.html" title="Numeric<T>">Numeric</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">U </code></td>
|
|
<td align="left"><a class="link" href="numeric.html" title="Numeric<T>">Numeric</a></td>
|
|
</tr>
|
|
</tbody>
|
|
</table></div>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm354477293424"></a>Header</h4></div></div></div>
|
|
<p><a href="../../include/safe_compare.hpp" target="_top"><code class="filename">#include
|
|
<boost/numeric/safe_compare.hpp> </code></a></p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm354477291360"></a>Example of use</h4></div></div></div>
|
|
<pre class="programlisting">#include <boost/numeric/safe_compare.hpp>
|
|
|
|
void f(){
|
|
unsigned char i = 129;
|
|
unsigned int j = 1;
|
|
assert(j < i); // program traps because expression is false. But this is a surprise because 1 < 129
|
|
assert( safe_compare::less_than(j,i)); // expression is true as we would expect
|
|
}
|
|
|
|
// safe_compare is used to implement comparison operators for safe types. So we can do this:
|
|
void g(){
|
|
safe<unsigned char> int i = 0x129;
|
|
safe<int> j = 1;
|
|
assert(j < i); // program works as expected
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2012 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
|
|
Software License</a></p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="safe_cast.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="exception_safety.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|