Files
safe_numerics/doc/html/tutorial/4.html
Robert Ramey 1bc0b94e65 changes to implement the following:
a) made trap_exception work
b) updated manual and examples to show how to use library to eliminate runtime penalty
c) added in safe_literal
d) made corrections of various types
2015-12-21 23:14:06 -08:00

94 lines
4.9 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Implicit Conversions Change Data Values</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="../tutorial.html" title="Tutorial and Motivating Examples">
<link rel="prev" href="2.html" title="Arithmetic Operations can Overflow Silently">
<link rel="next" href="10.html" title="Mixing Data Types Can Create Subtle Errors">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
<td><h2>Safe Numerics</h2></td>
</tr></table>
<div class="spirit-nav">
<a accesskey="p" href="2.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="10.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.tutorial.4"></a>Implicit Conversions Change Data Values</h3></div></div></div>
<p>A simple assignment or arithmetic expression will convert all the
terms to the same type. Sometimes this can silently change values. For
example, when a signed data variable contains a negative type, assigning
to a unsigned type will be permitted by any C/C++ compiler but will be
treated as large unsigned value. Most modern compilers will emit a compile
time warning when this conversion is performed. The user may then decide
to change some data types or apply a <code class="computeroutput">static_cast</code>. This is
less than satisfactory for two reasons:</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>It may be unwieldy to change all the types to signed or
unsigned.</p></li>
<li class="listitem"><p>We may believe that our signed type will never contain a
negative value. <code class="computeroutput">static_cast</code> changes the data type - not
the data value. If we ignore the any compiler warnings or use a
<code class="computeroutput">static_cast</code> to suppress them, we'll fail to detect a
program error when it is committed. This is aways a risk with
casts.</p></li>
</ul></div>
<p>This solution is simple, Just replace instances of the <code class="computeroutput">int
</code>with <code class="computeroutput">safe&lt;int&gt;</code>.</p>
<pre class="programlisting">#include &lt;exception&gt;
#include &lt;iostream&gt;
#include "../include/safe_integer.hpp"
int main(int argc, const char * argv[]){
std::cout &lt;&lt; "example 4: ";
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;
// problem: implicit conversions change data values
try{
int x = -1000;
// the following silently produces an incorrect result
char y = x;
std::cout &lt;&lt; x &lt;&lt; " != " &lt;&lt; (int)y &lt;&lt; std::endl;
std::cout &lt;&lt; "error NOT detected!" &lt;&lt; std::endl;
}
catch(std::exception){
std::cout &lt;&lt; "error detected!" &lt;&lt; std::endl; // never arrive here
}
// 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 = -1000;
// throws exception when conversion change data value
safe&lt;char&gt; y1(x);
safe&lt;char&gt; y2 = x;
safe&lt;char&gt; y3 = {x};
std::cout &lt;&lt; "error NOT detected!" &lt;&lt; std::endl;
}
catch(std::exception &amp; e){
std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
std::cout &lt;&lt; "error detected!" &lt;&lt; std::endl;
}
return 0;
}
</pre>
</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 &#169; 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="2.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="10.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>