corrected misc documentation errors

This commit is contained in:
Robert Ramey
2014-08-31 15:37:28 -07:00
parent 0e9e73eaf6
commit 7fbaff67ba
19 changed files with 273 additions and 175 deletions

0
doc/boostbook/makehtml.sh Normal file → Executable file
View File

View File

@@ -2,7 +2,7 @@
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_cast">
<title>safe_cast</title>
<title>safe_cast&lt;T, U&gt;</title>
<section>
<title>Synopsis</title>

View File

@@ -2,7 +2,7 @@
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_compare">
<title>safe_compare</title>
<title>safe_compare&lt;T, U&gt;</title>
<section>
<title>Synopsis</title>

View File

@@ -205,7 +205,7 @@
Table of contents
=============================================================================*/
.toc
div.toc
{
margin: 1pc 4% 0pc 4%;
padding: 0.1pc 1pc 0.1pc 1pc;
@@ -466,7 +466,7 @@
}
/* Table of contents */
.toc
div.toc
{
border: 1px solid #DCDCDC;
}
@@ -534,7 +534,7 @@
}
/* Table of contents */
.toc
div.toc
{
border: 1px solid gray;
}

View File

@@ -7,7 +7,7 @@
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="safe.html" title="safe&lt;T&gt;">
<link rel="next" href="safe_cast.html" title="safe_cast">
<link rel="next" href="safe_cast.html" title="safe_cast&lt;T, U&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
@@ -26,8 +26,8 @@
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.functions"></a>Functions</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_cast.html">safe_cast</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare</a></span></dt>
<dt><span class="section"><a href="safe_cast.html">safe_cast&lt;T, U&gt;</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare&lt;T, U&gt;</a></span></dt>
<dt><span class="section"><a href="rationale/overflow.html">overflow</a></span></dt>
</dl></div>
</div>

View File

@@ -27,7 +27,7 @@
</h3></div></div>
<div><p class="copyright">Copyright &#169; 2012 Robert Ramey</p></div>
<div><div class="legalnotice">
<a name="id698095"></a><p><a href="???" target="_top">Subject to Boost Software License</a></p>
<a name="idm412407782640"></a><p><a href="???" target="_top">Subject to Boost Software License</a></p>
</div></div>
</div></div>
<div class="toc">
@@ -46,13 +46,13 @@
<dt><span class="section"><a href="types.html">Types</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="safe_unsigned_range.html">safe_unsigned_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;MIN,&gt;</a></span></dt>
<dt><span class="section"><a href="safe.html">safe&lt;T&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="functions.html">Functions</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="safe_cast.html">safe_cast</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare</a></span></dt>
<dt><span class="section"><a href="safe_cast.html">safe_cast&lt;T, U&gt;</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare&lt;T, U&gt;</a></span></dt>
<dt><span class="section"><a href="rationale/overflow.html">overflow</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="rationale.html">Rationale</a></span></dt>
@@ -63,7 +63,7 @@
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: , at </small></p></td>
<td align="left"><p><small>Last revised: , </small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>

View File

@@ -32,13 +32,13 @@
tests for my embedded systems projects on my PC. Still, I had a few
issues.</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>I was a lot of code in one header - 6400 lines. Very unwieldy to
understand and modify.</p></li>
<li class="listitem"><p>It was a lot of code in one header - 6400 lines. Very unwieldy
to understand and modify.</p></li>
<li class="listitem"><p>I couldn't find separate documentation other than that in the
header file.</p></li>
<li class="listitem"><p>I didn't use <a href="www.boost.org" target="_top">Boost</a>
<li class="listitem"><p>It didn't use <a href="www.boost.org" target="_top">Boost</a>
conventions for naming.</p></li>
<li class="listitem"><p>I required porting to different compilers.</p></li>
<li class="listitem"><p>It required porting to different compilers.</p></li>
<li class="listitem"><p>It had a very long license associated with it.</p></li>
<li class="listitem"><p>The package I downloaded didn't have a test suite</p></li>
<li class="listitem"><p>I believe the original <a href="http://safeint.codeplex.com/releases/view/73792" target="_top">SafeInt</a>

View File

@@ -26,16 +26,16 @@
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.numeric"></a>Numeric&lt;T&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="numeric.html#id699580">Description</a></span></dt>
<dt><span class="section"><a href="numeric.html#id741994">Notation</a></span></dt>
<dt><span class="section"><a href="numeric.html#id742071">Associated Types</a></span></dt>
<dt><span class="section"><a href="numeric.html#id742130">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="numeric.html#id753392">Header</a></span></dt>
<dt><span class="section"><a href="numeric.html#id753413">Models</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407744816">Description</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407739600">Notation</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407731680">Associated Types</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407725856">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407636528">Header</a></span></dt>
<dt><span class="section"><a href="numeric.html#idm412407634528">Models</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id699580"></a>Description</h4></div></div></div>
<a name="idm412407744816"></a>Description</h4></div></div></div>
<p>A type is Numeric if it has the properties of a number.</p>
<p>More specifically, a type T is Numeric if there exists
specialization of <code class="computeroutput">std::numeric_limits&lt;T&gt;</code>. See the
@@ -50,9 +50,9 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id741994"></a>Notation</h4></div></div></div>
<a name="idm412407739600"></a>Notation</h4></div></div></div>
<div class="table">
<a name="id742000"></a><p class="title"><b>Table&#160;1.&#160;Notation</b></p>
<a name="idm412407738960"></a><p class="title"><b>Table&#160;1.&#160;Notation</b></p>
<div class="table-contents"><table class="table" summary="Notation">
<colgroup>
<col align="left">
@@ -74,9 +74,9 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id742071"></a>Associated Types</h4></div></div></div>
<a name="idm412407731680"></a>Associated Types</h4></div></div></div>
<div class="table">
<a name="id742077"></a><p class="title"><b>Table&#160;2.&#160;Associated Types</b></p>
<a name="idm412407731040"></a><p class="title"><b>Table&#160;2.&#160;Associated Types</b></p>
<div class="table-contents"><table class="table" summary="Associated Types">
<colgroup>
<col align="left">
@@ -95,13 +95,13 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id742130"></a>Valid Expressions</h4></div></div></div>
<a name="idm412407725856"></a>Valid Expressions</h4></div></div></div>
<p>In addition to the expressions defined in <a href="http://www.sgi.com/tech/stl/Assignable.html" target="_top">Assignable</a> the
following expressions must be valid.</p>
<p>Any operations which result in integers which cannot be represented
as some Numeric type will throw an exception.</p>
<div class="table">
<a name="id742150"></a><p class="title"><b>Table&#160;3.&#160;General</b></p>
<a name="idm412407723840"></a><p class="title"><b>Table&#160;3.&#160;General</b></p>
<div class="table-contents"><table class="table" summary="General">
<colgroup>
<col align="left">
@@ -131,7 +131,7 @@
</div>
<p><br class="table-break"></p>
<div class="table">
<a name="id742245"></a><p class="title"><b>Table&#160;4.&#160;Unary Operators</b></p>
<a name="idm412407714368"></a><p class="title"><b>Table&#160;4.&#160;Unary Operators</b></p>
<div class="table-contents"><table class="table" summary="Unary Operators">
<colgroup>
<col align="left">
@@ -183,7 +183,7 @@
</table></div>
</div>
<br class="table-break"><div class="table">
<a name="id697780"></a><p class="title"><b>Table&#160;5.&#160;Binary Operators</b></p>
<a name="idm412407695584"></a><p class="title"><b>Table&#160;5.&#160;Binary Operators</b></p>
<div class="table-contents"><table class="table" summary="Binary Operators">
<colgroup>
<col align="left">
@@ -199,12 +199,12 @@
<tr>
<td align="left"><code class="computeroutput">t - u</code></td>
<td align="left">V</td>
<td align="left">Invert sign</td>
<td align="left">subtract u from t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t + u</code></td>
<td align="left">V</td>
<td align="left">unary plus - a no op</td>
<td align="left">add u to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t * u</code></td>
@@ -339,13 +339,13 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753392"></a>Header</h4></div></div></div>
<a name="idm412407636528"></a>Header</h4></div></div></div>
<p><a href="../../include/concept/numeric.hpp" target="_top"><code class="computeroutput">#include
&lt;safe_numerics/include/concepts/numeric.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753413"></a>Models</h4></div></div></div>
<a name="idm412407634528"></a>Models</h4></div></div></div>
<p><code class="computeroutput">int, safe_signed_integer&lt;int&gt;,
safe_signed_range&lt;int&gt;, etc.</code></p>
</div>

View File

@@ -26,15 +26,15 @@
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.rationale"></a>Rationale</h2></div></div></div>
<div class="qandaset">
<a name="id754861"></a><dl>
<dt>1. <a href="rationale.html#id754866">Why does a binary operation on two
safe&lt;int&gt; values not necessarily return another
<a name="idm412404321168"></a><dl>
<dt>1. <a href="rationale.html#idm412404320672">Why does a binary operation on two
safe&lt;int&gt; values not return another
safe type ?</a>
</dt>
<dt>2. <a href="rationale.html#id754917">Why is there no policy driven design for handling
<dt>2. <a href="rationale.html#idm412404314976">Why is there no policy driven design for handling
overflows</a>
</dt>
<dt>3. <a href="rationale.html#id754946">Why is Boost.Convert not used.</a>
<dt>3. <a href="rationale.html#idm412404312016">Why is Boost.Convert not used.</a>
</dt>
</dl>
<table border="0" width="100%" summary="Q and A Set">
@@ -43,10 +43,10 @@
<tbody>
<tr class="question">
<td align="left" valign="top">
<a name="id754866"></a><a name="id754868"></a><p><b>1.</b></p>
<a name="idm412404320672"></a><a name="idm412404320416"></a><p><b>1.</b></p>
</td>
<td align="left" valign="top"><p>Why does a binary operation on two
<code class="computeroutput">safe&lt;int&gt;</code> values not necessarily return another
<code class="computeroutput">safe&lt;int&gt;</code> values not return another
<code class="computeroutput">safe</code> type ?</p></td>
</tr>
<tr class="answer">
@@ -54,7 +54,7 @@
<td align="left" valign="top">
<p>There are a number of</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>it was hard to implement.</p></li>
<li class="listitem"><p>it was too hard to implement.</p></li>
<li class="listitem"><p>it doesn't really seem necessary. We always do SOMETHING
with result of the operation. This will result in an assignment
or a conversion to some other type where the result will be
@@ -64,7 +64,7 @@
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id754917"></a><a name="id754920"></a><p><b>2.</b></p>
<a name="idm412404314976"></a><a name="idm412404314720"></a><p><b>2.</b></p>
</td>
<td align="left" valign="top">
<p>Why is there no policy driven design for handling
@@ -78,7 +78,7 @@ unsigned int x = t1 - t2; // which policy should be invoked?</pre>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id754946"></a><a name="id754948"></a><p><b>3.</b></p>
<a name="idm412404312016"></a><a name="idm412404311760"></a><p><b>3.</b></p>
</td>
<td align="left" valign="top">
<p>Why is Boost.Convert not used.</p>

View File

@@ -6,7 +6,7 @@
<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_compare.html" title="safe_compare">
<link rel="prev" href="../safe_compare.html" title="safe_compare&lt;T, U&gt;">
<link rel="next" href="../rationale.html" title="Rationale">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
@@ -26,22 +26,22 @@
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.rationale.overflow"></a>overflow</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="../overflow.html#id754741">Synopsis</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754761">Description</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754796">Header</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754817">Example of use</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754831">See Also</a></span></dt>
<dt><span class="section"><a href="../overflow.html#idm412404334512">Synopsis</a></span></dt>
<dt><span class="section"><a href="../overflow.html#idm412404332368">Description</a></span></dt>
<dt><span class="section"><a href="../overflow.html#idm412404328608">Header</a></span></dt>
<dt><span class="section"><a href="../overflow.html#idm412404326400">Example of use</a></span></dt>
<dt><span class="section"><a href="../overflow.html#idm412404324496">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754741"></a>Synopsis</h4></div></div></div>
<a name="idm412404334512"></a>Synopsis</h4></div></div></div>
<p>This function is invoked by the library whenever it is not possible
to produce a result for an arithmetic operation.</p>
<pre class="programlisting">void overflow(char const * const msg);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754761"></a>Description</h4></div></div></div>
<a name="idm412404332368"></a>Description</h4></div></div></div>
<p>If evironment supports C++ exceptions, this function throws the
exception .</p>
<p>If the environment does not support C++ exceptions, the user should
@@ -55,13 +55,13 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754796"></a>Header</h4></div></div></div>
<a name="idm412404328608"></a>Header</h4></div></div></div>
<p><a href="../include/safe_numerics/overflow" target="_top"><code class="computeroutput">#include
&lt;boost/safe_numerics/overflow.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754817"></a>Example of use</h4></div></div></div>
<a name="idm412404326400"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;cstdio&gt;
void overflow(char const * const msg){
@@ -72,7 +72,7 @@ void overflow(char const * const msg){
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754831"></a>See Also</h4></div></div></div>
<a name="idm412404324496"></a>See Also</h4></div></div></div>
<p>See <a class="link" href="rationale/overflow.html" title="overflow">rationale</a> for more
information on this function</p>
</div>

View File

@@ -6,7 +6,7 @@
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="types.html" title="Types">
<link rel="prev" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">
<link rel="prev" href="safe_signed_range.html" title="safe_signed_range&lt;boost::intmax_t MIN, boost::intmax_tMAX&gt;">
<link rel="next" href="functions.html" title="Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
@@ -26,26 +26,49 @@
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe"></a>safe&lt;T&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe.html#id753980">Description</a></span></dt>
<dt><span class="section"><a href="safe.html#id753999">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe.html#id754085">Model of</a></span></dt>
<dt><span class="section"><a href="safe.html#id754099">Derived From</a></span></dt>
<dt><span class="section"><a href="safe.html#id754139">Notation</a></span></dt>
<dt><span class="section"><a href="safe.html#id754225">Header</a></span></dt>
<dt><span class="section"><a href="safe.html#id754246">Example of use</a></span></dt>
<dt><span class="section"><a href="safe.html#id754272">Notes</a></span></dt>
<dt><span class="section"><a href="safe.html#id754284">See Also</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404421072">Description</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404419200">Notation</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404410400">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404401264">Model of</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404398944">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404387424">Header</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404385424">Example of use</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404382672">Notes</a></span></dt>
<dt><span class="section"><a href="safe.html#idm412404381440">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753980"></a>Description</h4></div></div></div>
<a name="idm412404421072"></a>Description</h4></div></div></div>
<p>A <code class="computeroutput">safe&lt;T&gt;</code> can be used anywhere a type T is used.
When T is used in operation which overflows, a exception is thrown</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753999"></a>Template Parameters</h4></div></div></div>
<p></p>
<a name="idm412404419200"></a>Notation</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Symbol</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">T, U, V</code></td>
<td align="left">Types which model the <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a> concept</td>
</tr>
<tr>
<td align="left">t, u, v</td>
<td align="left">objects of types T and U respectively</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm412404410400"></a>Template Parameters</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
@@ -59,58 +82,60 @@
</tr></thead>
<tbody><tr>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></td>
<td align="left"><p>The underlying integer type</p></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">boost::is_integer&lt;T&gt;::value
== true</a></td>
<td align="left"><p>The underlying intrinsic integer type</p></td>
</tr></tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754085"></a>Model of</h4></div></div></div>
<a name="idm412404401264"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
<p>If the resulting type of the operation t op u is V, the resulting
type of the operations st op su, t op su and st op u will be
safe&lt;V&gt;;</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754099"></a>Derived From</h4></div></div></div>
<p><code class="computeroutput"><a class="link" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">safe_signed_range&lt;T&gt;</a></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</p>
<p><code class="computeroutput"><a class="link" href="safe_unsigned_range.html" title="safe_unsigned_range&lt;MIN, MAX&gt;">safe_unsigned_range&lt;T&gt;</a></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754139"></a>Notation</h4></div></div></div>
<a name="idm412404398944"></a>Valid Expressions</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Symbol</th>
<th align="left">Parameter</th>
<th align="left">Result</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">A type that models the <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a> concept</td>
<td align="left"><code class="computeroutput">st op u</code></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">safe&lt;decltype(t
op u)&gt;</a></td>
<td align="left"><p>op is any valid binary operator for types T and
U</p></td>
</tr>
<tr>
<td align="left">t</td>
<td align="left">An object of type <code class="computeroutput">T</code>
</td>
<td align="left"><code class="computeroutput">T * U</code></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">typeof(T *
U)</a></td>
<td align="left"><p>The underlying integer type</p></td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754225"></a>Header</h4></div></div></div>
<a name="idm412404387424"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_integer.hpp" target="_top">#include
&lt;boost/safe_numerics/safe_integer.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754246"></a>Example of use</h4></div></div></div>
<a name="idm412404385424"></a>Example of use</h4></div></div></div>
<p>The following program will emit an error message on a machine where
int is only 16 bits but run without problem on a machine where int is 32
bits.</p>
@@ -131,13 +156,13 @@ void f(){
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754272"></a>Notes</h4></div></div></div>
<a name="idm412404382672"></a>Notes</h4></div></div></div>
<p>Footnotes (if any) that are referred to by other parts of the
page.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754284"></a>See Also</h4></div></div></div>
<a name="idm412404381440"></a>See Also</h4></div></div></div>
<p>Footnotes (if any) that are referred to by other parts of the
page.</p>
</div>

View File

@@ -1,13 +1,13 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_cast</title>
<title>safe_cast&lt;T, U&gt;</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="functions.html" title="Functions">
<link rel="next" href="safe_compare.html" title="safe_compare">
<link rel="next" href="safe_compare.html" title="safe_compare&lt;T, U&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
@@ -24,32 +24,32 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_cast"></a>safe_cast</h3></div></div></div>
<a name="safe_numerics.safe_cast"></a>safe_cast&lt;T, U&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_cast.html#id754315">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754330">Description</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754354">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754441">Preconditions</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754467">Complexity</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754483">Header</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754503">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404378160">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404376544">Description</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404373872">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404364368">Preconditions</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404361616">Complexity</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404359952">Header</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#idm412404357856">Example of use</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754315"></a>Synopsis</h4></div></div></div>
<a name="idm412404378160"></a>Synopsis</h4></div></div></div>
<pre class="programlisting">template&lt;class T, class U&gt;
T safe_cast(const U &amp; u);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754330"></a>Description</h4></div></div></div>
<a name="idm412404376544"></a>Description</h4></div></div></div>
<p>Converts one <a href="numeric_concept.xml" target="_top">Numeric</a> type
to another. Throws an <code class="computeroutput">std::out_of_range</code> exception if such a
conversion is not possible without changing the value.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754354"></a>Type requirements</h4></div></div></div>
<a name="idm412404373872"></a>Type requirements</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
@@ -73,27 +73,27 @@ T safe_cast(const U &amp; u);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754441"></a>Preconditions</h4></div></div></div>
<a name="idm412404364368"></a>Preconditions</h4></div></div></div>
<p>The value of u can be represented by the type <code class="computeroutput">T</code>. If
this is not true, an <code class="computeroutput">std::out_of_range</code> exception will be
thrown.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754467"></a>Complexity</h4></div></div></div>
<a name="idm412404361616"></a>Complexity</h4></div></div></div>
<p>[Example:]</p>
<p>O(N log(N)) comparisons (both average and worst-case), where N is
last - first. [2]</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754483"></a>Header</h4></div></div></div>
<a name="idm412404359952"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_cast.hpp" target="_top">#include
&lt;boost/numeric/safe_cast.hpp&gt; </a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754503"></a>Example of use</h4></div></div></div>
<a name="idm412404357856"></a>Example of use</h4></div></div></div>
<p>[A code fragment that illustrates how to use the function.]</p>
<pre class="programlisting">#include &lt;boost/numeric/safe_cast.hpp&gt;
#include &lt;boost/numeric/safe_integer.hpp&gt;

View File

@@ -1,12 +1,12 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_compare</title>
<title>safe_compare&lt;T, U&gt;</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">
<link rel="prev" href="safe_cast.html" title="safe_cast&lt;T, U&gt;">
<link rel="next" href="rationale/overflow.html" title="overflow">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
@@ -24,27 +24,39 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_compare"></a>safe_compare</h3></div></div></div>
<a name="safe_numerics.safe_compare"></a>safe_compare&lt;T, U&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_compare.html#id754544">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754567">Description</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754586">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754679">Header</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754704">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#idm412404354128">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#idm412404352032">Description</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#idm412404349632">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#idm412404340080">Header</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#idm412404338016">Example of use</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754544"></a>Synopsis</h4></div></div></div>
<p>safe_compare is really two functions:.</p>
<a name="idm412404354128"></a>Synopsis</h4></div></div></div>
<p>safe_compare is several functions:.</p>
<pre class="programlisting">template&lt;class T, class U&gt;
bool safe_compare::less_than(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);</pre>
bool safe_compare::less_than_equal(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::greater_than_equal(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::equa(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::not_equal(const T &amp; lhs, const U &amp; rhs);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754567"></a>Description</h4></div></div></div>
<a name="idm412404352032"></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++
@@ -54,10 +66,7 @@ bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754586"></a>Type requirements</h4></div></div></div>
<p>[This section lists the requirements that must be satisfied by the
function's template parameters. If the function has no template
parameters, this section can be skipped. Example:]</p>
<a name="idm412404349632"></a>Type requirements</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
@@ -81,15 +90,13 @@ bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754679"></a>Header</h4></div></div></div>
<p>[A link to the source code where the function header is
defined.]</p>
<a name="idm412404340080"></a>Header</h4></div></div></div>
<p><a href="../../include/safe_compare.hpp" target="_top"><code class="filename">#include
&lt;boost/numeric/safe_compare.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754704"></a>Example of use</h4></div></div></div>
<a name="idm412404338016"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;boost/numeric/safe_compare.hpp&gt;
void f(){

View File

@@ -1,7 +1,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_signed_range&lt;MIN, MAX&gt;</title>
<title>safe_signed_range&lt;boost::intmax_t MIN, boost::intmax_tMAX&gt;</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">
@@ -24,25 +24,26 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_signed_range"></a>safe_signed_range&lt;MIN, MAX&gt;</h3></div></div></div>
<a name="safe_numerics.safe_signed_range"></a>safe_signed_range&lt;boost::intmax_t MIN,
boost::intmax_tMAX&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_signed_range.html#id753773">Description</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753791">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753908">Model of</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753922">Header</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753942">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753956">See Also</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404445456">Description</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404443504">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404431568">Model of</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404428512">Header</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404426528">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#idm412404424208">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753773"></a>Description</h4></div></div></div>
<a name="idm412404445456"></a>Description</h4></div></div></div>
<p>This type holds a integer in the range [MIN, MAX]. It will throw a
<code class="computeroutput">std::out_of_range</code> exception for operation which would result
in assigning an integer value outside of this range.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753791"></a>Template Parameters</h4></div></div></div>
<a name="idm412404443504"></a>Template Parameters</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
@@ -75,18 +76,21 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753908"></a>Model of</h4></div></div></div>
<a name="idm412404431568"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
<p>The usage of this type in an arithmetic expression will result in
another type fulfilling the <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a> concept. </p>
<p>Operations on safe_signed_range will result in the same</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753922"></a>Header</h4></div></div></div>
<a name="idm412404428512"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_range.hpp" target="_top">#include
&lt;boost/safe_numerics/safe_range.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753942"></a>Example of use</h4></div></div></div>
<a name="idm412404426528"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
@@ -94,12 +98,19 @@ void f(){
i = 0; // error
i = 9; // ok
i *= 9; // throws overflow exception
std::int8_t j = 4;
auto k = i + j;
// since i can vary between 7 and 24 and j can vary between 0 and 255
// the smallest unsigned integer which can hold the result std::int16_t
// j will be of type std::int16_t
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753956"></a>See Also</h4></div></div></div>
<a name="idm412404424208"></a>See Also</h4></div></div></div>
<p><code class="computeroutput">std::out_of_range</code></p>
<p><code class="computeroutput">safe_unsigned_range</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>

View File

@@ -7,7 +7,7 @@
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="types.html" title="Types">
<link rel="prev" href="types.html" title="Types">
<link rel="next" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">
<link rel="next" href="safe_signed_range.html" title="safe_signed_range&lt;boost::intmax_t MIN, boost::intmax_tMAX&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
@@ -26,23 +26,23 @@
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_unsigned_range"></a>safe_unsigned_range&lt;MIN, MAX&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_unsigned_range.html#id753452">Description</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753471">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753587">Model of</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753704">Header</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753724">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753746">See Also</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412407630816">Description</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412407628864">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412404454992">Model of</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412404452224">Header</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412404450240">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#idm412404448752">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753452"></a>Description</h4></div></div></div>
<a name="idm412407630816"></a>Description</h4></div></div></div>
<p>This type holds a integer in the range [MIN, MAX]. It will throw a
<code class="computeroutput">std::out_of_range</code> exception for any operation which would
result in assigning an integer value outside of this range.</p>
result in assigning an integer value outside of this range. </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753471"></a>Template Parameters</h4></div></div></div>
<a name="idm412407628864"></a>Template Parameters</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
@@ -75,18 +75,22 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753587"></a>Model of</h4></div></div></div>
<a name="idm412404454992"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
<p>The usage of this type in an arithmetic expression with another
unsigned type will result in another unsigned type fulfilling the <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a> concept. This will be the
smallest unsigned integer type of sufficient size to hold the result of
the operation. </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753704"></a>Header</h4></div></div></div>
<a name="idm412404452224"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_range.hpp" target="_top">#include
&lt;safe/numeric/safe_range.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753724"></a>Example of use</h4></div></div></div>
<a name="idm412404450240"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
@@ -95,12 +99,20 @@ void f(){
i = 9; // ok
i *= 9; // throws out_of_range exception
i = -1; // throws out_of_range exception
std::uint8_t j = 4;
auto k = i + j;
// since i can vary between 7 and 24 and j can vary between 0 and 255
// the smallest unsigned integer which can hold the result std::uint16_t
// j will be of type std::uint16_t
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753746"></a>See Also</h4></div></div></div>
<a name="idm412404448752"></a>See Also</h4></div></div></div>
<p><code class="computeroutput">std::out_of_range</code></p>
<p><code class="computeroutput">safe_signed_range</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>

View File

@@ -36,8 +36,21 @@
<p>This program demonstrates this problem. The solution is to replace
instances of <code class="computeroutput">char</code> type with <code class="computeroutput">safe&lt;char&gt;</code>
type.</p>
<pre class="programlisting">void example1(){
// problem: undetected erroneous expression evaluation
<pre class="programlisting">#include &lt;cassert&gt;
#include &lt;stdexcept&gt;
#include &lt;iostream&gt;
#include "../include/safe_integer.hpp"
//#include "../include/safe_compare.hpp"
void detected_msg(bool detected){
std::cout &lt;&lt; (detected ? "error detected!" : "error NOT detected! ") &lt;&lt; std::endl;
}
int main(int argc, const char * argv[]){
std::cout &lt;&lt; "example 1:";
std::cout &lt;&lt; "undetected erroneous expression evaluation" &lt;&lt; std::endl;
std::cout &lt;&lt; "Not using safe numerics" &lt;&lt; std::endl;
try{
char x = 127;
char y = 2;
@@ -49,13 +62,14 @@
// but assert fails to detect it since C++ implicitly
// converts variables to int before evaluating he expression!
assert(z != x + y);
std::cout &lt;&lt; static_cast&lt;int&gt;(z) &lt;&lt; " != " &lt;&lt; x + y;
std::cout &lt;&lt; static_cast&lt;int&gt;(z) &lt;&lt; " != " &lt;&lt; x + y &lt;&lt; std::endl;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace char with safe&lt;char&gt;
std::cout &lt;&lt; "Using safe numerics" &lt;&lt; std::endl;
try{
using namespace boost::numeric;
safe&lt;char&gt; x = 127;
@@ -67,9 +81,10 @@
}
catch(std::range_error &amp; e){
// which can catch here
std::cout &lt;&lt; e.what();
std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
detected_msg(true);
}
return 0;
}
</pre>
</div>

View File

@@ -27,30 +27,44 @@
<a name="safe_numerics.tutorial.2"></a>Problem:Undetected overflow</h3></div></div></div>
<p>A variation of the above is when a value is incremented/decremented
beyond it's domain. This is a common problem with for loops.</p>
<pre class="programlisting">void example3(){
// problem: undetected overflow in data type
<pre class="programlisting">#include &lt;cassert&gt;
#include &lt;stdexcept&gt;
#include &lt;iostream&gt;
#include "../include/safe_integer.hpp"
//#include "../include/safe_compare.hpp"
void detected_msg(bool detected){
std::cout &lt;&lt; (detected ? "error detected!" : "error NOT detected! ") &lt;&lt; std::endl;
}
int main(int argc, const char * argv[]){
std::cout &lt;&lt; "example 3: ";
std::cout &lt;&lt; "implicit conversions change data values" &lt;&lt; std::endl;
std::cout &lt;&lt; "Not using safe numerics" &lt;&lt; std::endl;
try{
int x = INT_MAX;
int x = -1000;
// the following silently produces an incorrect result
++x;
//std::cout &lt;&lt; x &lt;&lt; " != " &lt;&lt; -1;
char y = x;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace int with safe&lt;int&gt;
// solution: replace int with safe&lt;int&gt; and char with safe&lt;char&gt;
std::cout &lt;&lt; "Using safe numerics" &lt;&lt; std::endl;
try{
using namespace boost::numeric;
safe&lt;int&gt; x = INT_MAX;
// throws exception when result is past maximum possible
++x;
safe&lt;int&gt; x = -1000;
// throws exception when conversion change data value
safe&lt;char&gt; y = x;
assert(false); // never arrive here
}
catch(std::range_error &amp; e){
std::cout &lt;&lt; e.what();
std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
detected_msg(true);
}
return 0;
}
</pre>
</div>

View File

@@ -45,13 +45,25 @@
</ul></div>
<p>This solution is the same as the above, Just replace instances of
the <code class="computeroutput">int </code>with <code class="computeroutput">safe&lt;int&gt;</code>.</p>
<pre class="programlisting">void example2(){
// problem: undetected overflow in data type
<pre class="programlisting">#include &lt;cassert&gt;
#include &lt;stdexcept&gt;
#include &lt;iostream&gt;
#include "../include/safe_integer.hpp"
//#include "../include/safe_compare.hpp"
void detected_msg(bool detected){
std::cout &lt;&lt; (detected ? "error detected!" : "error NOT detected! ") &lt;&lt; std::endl;
}
int main(int argc, const char * argv[]){
std::cout &lt;&lt; "example 2:";
std::cout &lt;&lt; "undetected overflow in data type" &lt;&lt; std::endl;
try{
int x = INT_MAX;
// the following silently produces an incorrect result
++x;
//std::cout &lt;&lt; x &lt;&lt; " != " &lt;&lt; -1;
std::cout &lt;&lt; x &lt;&lt; " != " &lt;&lt; INT_MAX &lt;&lt; " + 1" &lt;&lt; std::endl;
detected_msg(false);
}
catch(...){
@@ -69,6 +81,7 @@
std::cout &lt;&lt; e.what();
detected_msg(true);
}
return 0;
}
</pre>
</div>

View File

@@ -27,7 +27,8 @@
<a name="safe_numerics.types"></a>Types</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_unsigned_range.html">safe_unsigned_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;boost::intmax_t MIN,
boost::intmax_tMAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe.html">safe&lt;T&gt;</a></span></dt>
</dl></div>
</div>