safe_numerics/doc/html/safe_numeric_concept.html
Robert Ramey 135fe9f1fd merged in changes from John MacFarlane
and documentation improvements from Andre ...
2016-09-08 21:57:15 -07:00

248 lines
11 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>SafeNumeric&lt;T&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="concepts.html" title="Type Requirements">
<link rel="prev" href="integer.html" title="Integer&lt;T&gt;">
<link rel="next" href="promotion_policy.html" title="PromotionPolicy&lt;PP&gt;">
</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="integer.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="concepts.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="promotion_policy.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_numeric_concept"></a>SafeNumeric&lt;T&gt;</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543080640">Description</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543079248">Refinement of</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543077568">Notation</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543061136">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543003568">Complexity Guarantees</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543002224">Invariants</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472543000624">Header</a></span></dt>
<dt><span class="section"><a href="safe_numeric_concept.html#idm472542998384">Models</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543080640"></a>Description</h4></div></div></div>
<p>This holds an arithmetic value which can be used as a replacement
for built-in C++ arithmetic values. These types differ from their built-in
counter parts in that the are guaranteed not to produce invalid arithmetic
results.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543079248"></a>Refinement of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543077568"></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</code></td>
<td align="left">Types fulfilling <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a> type
requirements</td>
</tr>
<tr>
<td align="left">t, u</td>
<td align="left">objects of types T, U</td>
</tr>
<tr>
<td align="left">S, S1, S2</td>
<td align="left">A type fulfilling SafeNumeric type requirements</td>
</tr>
<tr>
<td align="left">s, s1, s2</td>
<td align="left">objects of types S</td>
</tr>
<tr>
<td align="left">op</td>
<td align="left">C++ infix operator</td>
</tr>
<tr>
<td align="left">prefix_op</td>
<td align="left">C++ prefix operator</td>
</tr>
<tr>
<td align="left">postfix_op</td>
<td align="left">C++ postfix operator</td>
</tr>
<tr>
<td align="left">assign_op</td>
<td align="left">C++ assignment operator</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543061136"></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">Expression</th>
<th align="left">Result Type</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">s op t</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>invoke safe C++ operator op and return another
SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t op s</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>invoke safe C++ operator op and return another
SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">s1 op s2</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>invoke safe C++ operator op and return another
SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">prefix_op S</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>invoke safe C++ operator op and return another
SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">S postfix_op</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>invoke safe C++ operator op and return another
SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">s assign_op t</code></td>
<td align="left">S1</td>
<td align="left"><p>convert t to type S1 and assign it to s1. If the
value t cannot be represented as an instance of type S1, it is
an error.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">S(t)</code></td>
<td align="left">unspecified S</td>
<td align="left"><p>construct a instance of S from a value of type T. f
the value t cannot be represented as an instance of type S1, it
is an error.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">S</code></td>
<td align="left">S</td>
<td align="left"><p>construct a uninitialized instance of
S.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">is_safe&lt;S&gt;</code></td>
<td align="left">
<code class="computeroutput">std::true_type</code> or
<code class="computeroutput">std::false_type</code>
</td>
<td align="left"><p>type trait to query whether any type T fulfills the
requirements for a SafeNumeric type.</p></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">static_cast&lt;T&gt;(s)</code></td>
<td align="left">T</td>
<td align="left"><p>convert the value of s to type T. If the value of s
cannot be correctly represented as a type T, it is an error.
Note that implicit casting from a safe type to a built-in
integer type is expressly prohibited and should invoke a compile
time error.</p></td>
</tr>
</tbody>
</table></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>Result of any binary operation where one or both of the operands
is a SafeNumeric type is also a SafeNumeric type.</p></li>
<li class="listitem"><p>All the expressions in the above table are
<code class="computeroutput">constexpr</code> expressions</p></li>
<li class="listitem"><p>Binary expressions which are not assignments require that
promotion and exception policies be identical.</p></li>
<li class="listitem">
<p>Safe Numeric operators will NOT perform standard numeric
conversions in order to convert to built-in types.</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span><span class="special">{</span>
<span class="keyword">long</span> <span class="identifier">x</span><span class="special">;</span>
<span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span><span class="special">;</span> <span class="comment">// OK - builtin implicit version</span>
<span class="identifier">safe</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">;</span>
<span class="identifier">f</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span><span class="special">;</span> <span class="comment">// compile time error</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span></pre>
<p>This behavior prevents a <code class="computeroutput">safe&lt;T&gt;</code> from
being a "drop-in" replacement for a <code class="computeroutput">T</code>.</p>
</li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543003568"></a>Complexity Guarantees</h4></div></div></div>
<p>There are no explicit complexity guarantees here. However, it would
be very surprising if any implementation were to be more complex that
O(0);</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543002224"></a>Invariants</h4></div></div></div>
<p>The fundamental requirement of a SafeNumeric type is that implements
all C++ operations permitted on it's base type in a way the prevents the
return of an incorrect arithmetic result. Various implementations of this
concept may handle circumstances which produce such results differently (
throw exception, compile time trap, etc..) no implementation should return
an arithmetically incorrect result.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472543000624"></a>Header</h4></div></div></div>
<p><a href="../../include/concept/exception_policy.hpp" target="_top"><code class="computeroutput">#include
&lt;safe_numerics/include/concepts/safe_numeric.hpp&gt;</code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm472542998384"></a>Models</h4></div></div></div>
<p>safe&lt;T&gt;</p>
<p>safe_signed_range&lt;-11, 11&gt;</p>
<p>safe_unsigned_range&lt;0, 11&gt;</p>
<p>safe_literal&lt;4&gt;</p>
</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 &#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="integer.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="concepts.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="promotion_policy.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>