333 lines
29 KiB
HTML
333 lines
29 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>safe<T, PP, EP></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="types.html" title="Types">
|
|
<link rel="prev" href="types.html" title="Types">
|
|
<link rel="next" href="safe_range.html" title="safe_signed_range<MIN, MAX, PP, EP> and safe_unsigned_range<MIN, MAX, PP, EP>">
|
|
</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="types.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.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="safe_range.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"></a>safe<T, PP, EP></h3></div></div></div>
|
|
<div class="toc"><dl class="toc">
|
|
<dt><span class="section"><a href="safe.html#idm130203562112">Description</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203560112">Model of</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203556080">Notation</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203548784">Associated Types</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203541744">Template Parameters</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203523280">Valid Expressions</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203517392">Examples of use</a></span></dt>
|
|
<dt><span class="section"><a href="safe.html#idm130203247744">Header</a></span></dt>
|
|
</dl></div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203562112"></a>Description</h4></div></div></div>
|
|
<p>A <code class="computeroutput">safe<T, PP , EP></code> can be used anywhere a type T
|
|
can be used. Any expression which uses this type is guaranteed to return
|
|
an arithmetically correct value or to trap in some way.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203560112"></a>Model of</h4></div></div></div>
|
|
<p><a class="link" href="numeric.html" title="Numeric<T>">Integer</a></p>
|
|
<p><a class="link" href="safe_numeric_concept.html" title="SafeNumeric<T>">SafeNumeric</a></p>
|
|
<p>This type inherits all the notation, associated types and template
|
|
parameters and valid expressions of <a class="link" href="safe_numeric_concept.html" title="SafeNumeric<T>">SafeNumeric</a> types. The
|
|
following specify additional features of this type.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203556080"></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</code></td>
|
|
<td align="left">Underlying type from which a safe type is being
|
|
derived</td>
|
|
</tr></tbody>
|
|
</table></div>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203548784"></a>Associated Types</h4></div></div></div>
|
|
<div class="informaltable"><table class="table">
|
|
<colgroup>
|
|
<col align="left">
|
|
<col align="left">
|
|
</colgroup>
|
|
<tbody>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">PP</code></td>
|
|
<td align="left">A type which specifies the result type of an expression
|
|
using safe types.</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="left"><code class="computeroutput">EP</code></td>
|
|
<td align="left">A type containing members which are called when a correct
|
|
result cannot be returned</td>
|
|
</tr>
|
|
</tbody>
|
|
</table></div>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203541744"></a>Template Parameters</h4></div></div></div>
|
|
<div class="informaltable"><table class="table">
|
|
<colgroup>
|
|
<col>
|
|
<col align="left">
|
|
<col align="left">
|
|
</colgroup>
|
|
<thead><tr>
|
|
<th align="left">Parameter</th>
|
|
<th align="left">Type Requirements</th>
|
|
<th align="left">Description</th>
|
|
</tr></thead>
|
|
<tbody>
|
|
<tr>
|
|
<td><code class="computeroutput">T</code></td>
|
|
<td align="left"><a href="http://en.cppreference.com/w/cpp/types/is_integral" target="_top">Integer<T></a></td>
|
|
<td align="left"><p>The underlying type. Currently only integer types are
|
|
supported</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code class="computeroutput">PP</code></td>
|
|
<td align="left"><a class="link" href="numeric.html" title="Numeric<T>">PromotionPolicy<PP></a></td>
|
|
<td align="left"><p>Optional promotion policy. Default value is <a class="link" href="promotion_policies/native.html" title="native"><code class="computeroutput">boost::numeric::native</code></a></p></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code class="computeroutput">EP</code></td>
|
|
<td align="left"><a class="link" href="numeric.html" title="Numeric<T>">Exception
|
|
Policy<EP></a></td>
|
|
<td align="left"><p>Optional exception policy. Default value is <a class="link" href="exception_policies.html#safe_numerics.exception_policies.default_exception_policy"><code class="computeroutput">boost::numeric::default_exception_policy</code></a></p></td>
|
|
</tr>
|
|
</tbody>
|
|
</table></div>
|
|
<p>See examples below.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203523280"></a>Valid Expressions</h4></div></div></div>
|
|
<p>Implements all expressions and only those expressions defined by the
|
|
<a class="link" href="safe_numeric_concept.html" title="SafeNumeric<T>">SafeNumeric</a><T>
|
|
type requirements. Note that all these expressions are
|
|
<code class="computeroutput">constexpr</code>. Thus, the result type of such an expression will
|
|
be another safe type. The actual type of the result of such an expression
|
|
will depend upon the specific promotion policy template parameter.</p>
|
|
<p>When a binary operand is applied to two instances of safe<T, PP,
|
|
EP>on of the following must be true:</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem"><p>The promotion policies of the two operands must be the same or
|
|
one of them must be void</p></li>
|
|
<li class="listitem"><p>The exception policies of the two operands must be the same or
|
|
one of them must be void</p></li>
|
|
</ul></div>
|
|
<p>If either of the above is not true, a compile error will
|
|
result.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203517392"></a>Examples of use</h4></div></div></div>
|
|
<p>The most common usage would be safe<T> which uses the default
|
|
promotion and exception policies. This type is meant to be a "drop-in"
|
|
replacement of the intrinsic integer types. That is, expressions involving
|
|
these types will be evaluated into result types which reflect the standard
|
|
rules for evaluation of C++ expressions. Should it occur that such
|
|
evaluation cannot return a correct result, an exception will be
|
|
thrown.</p>
|
|
<p>There are two aspects of the operation of this type which can be
|
|
customized with a policy. The first is the result type of an arithmetic
|
|
operation. C++ defines the rules which define this result type in terms of
|
|
the constituent types of the operation. Here we refer to these rules as
|
|
"type promotion" rules. These rules will sometimes result in a type which
|
|
cannot hold the actual arithmetic result of the operation. This is the
|
|
main motivation for making this library in the first place. One way to
|
|
deal with this problem is to substitute our own type promotion rules for
|
|
the C++ ones.</p>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="safe_numerics.drop_in_replacement"></a>As a Drop-in replacement for standard integer types.</h5></div></div></div>
|
|
<p>The following program will throw an exception and emit an error
|
|
message at runtime if any of several events result in an incorrect
|
|
arithmetic result. Behavior of this program could vary according to the
|
|
machine architecture in question.</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">exception</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">;</span>
|
|
<span class="identifier">safe</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">j</span><span class="special">;</span>
|
|
<span class="keyword">try</span> <span class="special">{</span>
|
|
<span class="identifier">safe</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cin</span> <span class="special">>></span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// could overflow !</span>
|
|
<span class="identifier">j</span> <span class="special">=</span> <span class="identifier">i</span> <span class="special">*</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// could overflow</span>
|
|
<span class="special">}</span>
|
|
<span class="keyword">catch</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="special">&</span> <span class="identifier">e</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">(</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">j</span><span class="special">;</span>
|
|
<span class="special">}</span></pre>
|
|
<p>The term "drop-in replacement" reveals the aspiration of
|
|
this library. In most cases, this aspiration is realized. In the
|
|
following example, the normal implicit conversions function the same for
|
|
safe integers as they do for built-in integers. </p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">safe_numerics</span><span class="special">;</span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="keyword">return</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
|
|
<span class="keyword">using</span> <span class="identifier">safe_t</span> <span class="special">=</span> <span class="identifier">safe</span><span class="special"><</span><span class="keyword">long</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">const</span> <span class="keyword">long</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">97</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 - implicit conversion to int</span>
|
|
<span class="keyword">const</span> <span class="identifier">safe_t</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">97</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">// Also OK - checked implicit conversion to int</span>
|
|
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
|
|
<span class="special">}</span></pre>
|
|
<p>When
|
|
the <code class="computeroutput">safe<long></code> is implicitly converted to an
|
|
<code class="computeroutput">int</code> when calling <code class="computeroutput">f</code>, the value is checked to be
|
|
sure that it is within the legal range of an int and will invoke an
|
|
exception if it cannot. We can easily verify this by altering the
|
|
exception handling policy in the above example to
|
|
<code class="computeroutput">loose_trap_policy</code>. This will invoke a compile time error on
|
|
any conversion might invoke a runtime exception.</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">safe_numerics</span><span class="special">;</span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="keyword">return</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
|
|
<span class="keyword">using</span> <span class="identifier">safe_t</span> <span class="special">=</span> <span class="identifier">safe</span><span class="special"><</span><span class="keyword">long</span><span class="special">,</span> <span class="identifier">native</span><span class="special">,</span> <span class="identifier">loose_trap_policy</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">const</span> <span class="keyword">long</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">97</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 - implicit conversion to int</span>
|
|
<span class="keyword">const</span> <span class="identifier">safe_t</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">97</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">// Would be OK, but will invoke compile time error</span>
|
|
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
|
|
<span class="special">}</span></pre>
|
|
<p>But this raises it's own questions. We can see that in this
|
|
example, the program can never fail:</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem"><p>The value 97 is assigned to y</p></li>
|
|
<li class="listitem"><p>y is converted to an int</p></li>
|
|
<li class="listitem"><p>and used as an argument to f</p></li>
|
|
</ul></div>
|
|
<p>The conversion can never fail because the value of 97 can always
|
|
fit into an int. But the library code can't detect this and emits the
|
|
checking code even though it's not necessary.</p>
|
|
<p>This can be addressed by using a <a class="link" href="safe_literal.html" title="safe_signed_literal<Value, PP , EP> and safe_unsigned_literal<Value, PP, EP>"><code class="computeroutput">safe_literal</code></a>. A
|
|
safe literal can contain one and only one value. All the functions in
|
|
this library are marked <code class="computeroutput">constexpr</code>. So it can be determined
|
|
at compile time that conversion to an <code class="computeroutput">int</code> can never fail
|
|
and no runtime checking code need be emitted. Making this small change
|
|
will permit the above example to run with zero runtime overhead while
|
|
guaranteeing that no error can ever occur.</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer_literal</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">safe_numerics</span><span class="special">;</span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="keyword">return</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
|
|
<span class="keyword">template</span><span class="special"><</span><span class="identifier">intmax_t</span> <span class="identifier">N</span><span class="special">></span>
|
|
<span class="keyword">using</span> <span class="identifier">safe_literal</span> <span class="special">=</span> <span class="identifier">safe_signed_literal</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span> <span class="identifier">native</span><span class="special">,</span> <span class="identifier">loose_trap_policy</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">const</span> <span class="keyword">long</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">97</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 - implicit conversion to int</span>
|
|
<span class="keyword">const</span> <span class="identifier">safe_literal</span><span class="special"><</span><span class="number">97</span><span class="special">></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">// OK - y is a type with min/max = 97;</span>
|
|
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
|
|
<span class="special">}</span></pre>
|
|
<p>With this trivial example, such efforts would hardly be deemed
|
|
necessary. But in a more complex case, perhaps including compile time
|
|
arithmetic expressions, it could be much more difficult to verify that
|
|
the constant is valid and/or no checking code is needed. And there is
|
|
also possibility that over the life time of the application, the compile
|
|
time constants might change, thus rendering any ad hoc analyse obsolete.
|
|
Using <a class="link" href="safe_literal.html" title="safe_signed_literal<Value, PP , EP> and safe_unsigned_literal<Value, PP, EP>"><code class="computeroutput">safe_literal</code></a>
|
|
will future-proof your code against well-meaning, but code-breaking
|
|
updates.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h5 class="title">
|
|
<a name="idm130203320752"></a>Adjust type promotion rules.</h5></div></div></div>
|
|
<p>Another way to avoid arithmetic errors like overflow is to promote
|
|
types to larger sizes before doing the arithmetic.</p>
|
|
<p>Stepping back, we can see that many of the cases of invalid
|
|
arithmetic wouldn't exist if the result types were larger. So we can
|
|
avoid these problems by replacing the C++ type promotion rules for
|
|
expressions with our own rules. This can be done by specifying a
|
|
promotion policy <code class="computeroutput"><code class="computeroutput">automatic</code></code>. The policy stores
|
|
the result of an expression in the smallest size type that can
|
|
accommodate the largest value that an expression can yield. No checking
|
|
for exceptions is necessary. The following example illustrates
|
|
this.</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">[</span><span class="special">]</span><span class="special">)</span><span class="special">{</span>
|
|
<span class="keyword">using</span> <span class="identifier">safe_int</span> <span class="special">=</span> <span class="identifier">safe</span><span class="special"><</span>
|
|
<span class="keyword">int</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">automatic</span><span class="special">,</span>
|
|
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">default_exception_policy</span>
|
|
<span class="special">></span><span class="special">;</span>
|
|
<span class="identifier">safe_int</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cin</span> <span class="special">>></span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// might throw exception</span>
|
|
<span class="keyword">auto</span> <span class="identifier">j</span> <span class="special">=</span> <span class="identifier">i</span> <span class="special">*</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// won't ever trap - result type can hold the maximum value of i * i</span>
|
|
<span class="identifier">static_assert</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">is_safe</span><span class="special"><</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">j</span><span class="special">)</span><span class="special">></span><span class="special">::</span><span class="identifier">value</span><span class="special">)</span><span class="special">;</span> <span class="comment">// result is another safe type</span>
|
|
<span class="identifier">static_assert</span><span class="special">(</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">i</span> <span class="special">*</span> <span class="identifier">i</span><span class="special">)</span><span class="special">></span><span class="special">::</span><span class="identifier">max</span><span class="special">(</span><span class="special">)</span> <span class="special">>=</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">safe_int</span><span class="special">></span><span class="special">::</span><span class="identifier">max</span><span class="special">(</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">safe_int</span><span class="special">></span><span class="special">::</span><span class="identifier">max</span><span class="special">(</span><span class="special">)</span>
|
|
<span class="special">)</span><span class="special">;</span> <span class="comment">// always true</span>
|
|
|
|
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
|
|
<span class="special">}</span></pre>
|
|
</div>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130203247744"></a>Header</h4></div></div></div>
|
|
<p><code class="filename"><a href="../../include/safe_integer.hpp" target="_top">#include
|
|
<boost/numeric/safe_numerics/safe_integer.hpp></a></code></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 © 2012-2018 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="types.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.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="safe_range.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|