• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4<title>safe_signed_range&lt;MIN, MAX, PP, EP&gt; and safe_unsigned_range&lt;MIN, MAX, PP, EP&gt;</title>
5<link rel="stylesheet" href="boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="index.html" title="Safe Numerics">
8<link rel="up" href="types.html" title="Types">
9<link rel="prev" href="safe.html" title="safe&lt;T, PP, EP&gt;">
10<link rel="next" href="safe_literal.html" title="safe_signed_literal&lt;Value, PP , EP&gt; and safe_unsigned_literal&lt;Value, PP, EP&gt;">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
15<td><h2>Safe Numerics</h2></td>
16</tr></table>
17<div class="spirit-nav">
18<a accesskey="p" href="safe.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_literal.html"><img src="images/next.png" alt="Next"></a>
19</div>
20<div class="section">
21<div class="titlepage"><div><div><h3 class="title">
22<a name="safe_numerics.safe_range"></a>safe_signed_range&lt;MIN, MAX, PP, EP&gt; and
23  safe_unsigned_range&lt;MIN, MAX, PP, EP&gt;</h3></div></div></div>
24<div class="toc"><dl class="toc">
25<dt><span class="section"><a href="safe_range.html#idm130203244016">Description</a></span></dt>
26<dt><span class="section"><a href="safe_range.html#idm130203241136">Notation</a></span></dt>
27<dt><span class="section"><a href="safe_range.html#idm130203233776">Associated Types</a></span></dt>
28<dt><span class="section"><a href="safe_range.html#idm130203226688">Template Parameters</a></span></dt>
29<dt><span class="section"><a href="safe_range.html#idm130203205424">Model of</a></span></dt>
30<dt><span class="section"><a href="safe_range.html#idm130203186512">Valid Expressions</a></span></dt>
31<dt><span class="section"><a href="safe_range.html#idm130203184224">Example of use</a></span></dt>
32<dt><span class="section"><a href="safe_range.html#idm130203099968">Header</a></span></dt>
33</dl></div>
34<div class="section">
35<div class="titlepage"><div><div><h4 class="title">
36<a name="idm130203244016"></a>Description</h4></div></div></div>
37<p>This type holds a signed or unsigned integer in the closed range
38    [MIN, MAX]. A <code class="computeroutput">safe_signed_range&lt;MIN, MAX, PP, EP&gt;</code> or
39    <code class="computeroutput">safe_unsigned_range&lt;MIN, MAX, PP, EP&gt;</code> can be used
40    anywhere an arithmetic type is permitted. Any expression which uses either
41    of these types is guaranteed to return an arithmetically correct value or
42    to trap in some way.</p>
43</div>
44<div class="section">
45<div class="titlepage"><div><div><h4 class="title">
46<a name="idm130203241136"></a>Notation</h4></div></div></div>
47<div class="informaltable"><table class="table">
48<colgroup>
49<col align="left">
50<col align="left">
51</colgroup>
52<thead><tr>
53<th align="left">Symbol</th>
54<th align="left">Description</th>
55</tr></thead>
56<tbody><tr>
57<td align="left"><code class="computeroutput">MIN, MAX</code></td>
58<td align="left">Minimum and maximum values that the range can
59            represent.</td>
60</tr></tbody>
61</table></div>
62</div>
63<div class="section">
64<div class="titlepage"><div><div><h4 class="title">
65<a name="idm130203233776"></a>Associated Types</h4></div></div></div>
66<div class="informaltable"><table class="table">
67<colgroup>
68<col align="left">
69<col align="left">
70</colgroup>
71<tbody>
72<tr>
73<td align="left"><code class="computeroutput">PP</code></td>
74<td align="left">Promotion Policy. A type which specifies the result type of
75            an expression using safe types.</td>
76</tr>
77<tr>
78<td align="left"><code class="computeroutput">EP</code></td>
79<td align="left">Exception Policy. A type containing members which are
80            called when a correct result cannot be returned</td>
81</tr>
82</tbody>
83</table></div>
84</div>
85<div class="section">
86<div class="titlepage"><div><div><h4 class="title">
87<a name="idm130203226688"></a>Template Parameters</h4></div></div></div>
88<div class="informaltable"><table class="table">
89<colgroup>
90<col align="left">
91<col align="left">
92<col align="left">
93</colgroup>
94<thead><tr>
95<th align="left">Parameter</th>
96<th align="left">Requirements</th>
97<th align="left">Description</th>
98</tr></thead>
99<tbody>
100<tr>
101<td align="left"><code class="computeroutput">MIN</code></td>
102<td align="left">must be a non-negative literal</td>
103<td align="left">The minimum non-negative integer value that this type may
104            hold</td>
105</tr>
106<tr>
107<td align="left"><code class="computeroutput">MAX</code></td>
108<td align="left">must be a non-negative literal</td>
109<td align="left">The maximum non-negative integer value that this type may
110            hold</td>
111</tr>
112<tr>
113<td align="left">&#160;</td>
114<td align="left">MIN &lt;= MAX</td>
115<td align="left">must be a valid closed range</td>
116</tr>
117<tr>
118<td align="left"><code class="computeroutput">PP</code></td>
119<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">PromotionPolicy&lt;PP&gt;</a></td>
120<td align="left"><p>Default value is <a class="link" href="promotion_policies/native.html" title="native"><code class="computeroutput">boost::numeric::native</code></a></p></td>
121</tr>
122<tr>
123<td align="left"><code class="computeroutput">EP</code></td>
124<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Exception
125            Policy&lt;EP&gt;</a></td>
126<td align="left"><p>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>
127</tr>
128</tbody>
129</table></div>
130</div>
131<div class="section">
132<div class="titlepage"><div><div><h4 class="title">
133<a name="idm130203205424"></a>Model of</h4></div></div></div>
134<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Integer</a></p>
135<p><a class="link" href="safe_numeric_concept.html" title="SafeNumeric&lt;T&gt;">SafeNumeric</a></p>
136</div>
137<div class="section">
138<div class="titlepage"><div><div><h4 class="title">
139<a name="idm130203186512"></a>Valid Expressions</h4></div></div></div>
140<p>Implements all expressions and only those expressions defined by the
141    <a class="link" href="safe_numeric_concept.html" title="SafeNumeric&lt;T&gt;">SafeNumeric</a> type
142    requirements. Thus, the result type of such an expression will be another
143    safe type. The actual type of the result of such an expression will depend
144    upon the specific promotion policy template parameter.</p>
145</div>
146<div class="section">
147<div class="titlepage"><div><div><h4 class="title">
148<a name="idm130203184224"></a>Example of use</h4></div></div></div>
149<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">type_traits</span><span class="special">&gt;</span>
150<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
151<span class="preprocessor">#include</span> <span class="special">&lt;</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_range</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
152
153<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">utility</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
154
155<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>
156
157<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="special">)</span><span class="special">{</span>
158    <span class="identifier">safe_unsigned_range</span><span class="special">&lt;</span><span class="number">7</span><span class="special">,</span> <span class="number">24</span><span class="special">&gt;</span> <span class="identifier">i</span><span class="special">;</span>
159    <span class="comment">// since the range is included in [0,255], the underlying type of i </span>
160    <span class="comment">// will be an unsigned char.</span>
161    <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>  <span class="comment">// throws out_of_range exception</span>
162    <span class="identifier">i</span> <span class="special">=</span> <span class="number">9</span><span class="special">;</span>  <span class="comment">// ok</span>
163    <span class="identifier">i</span> <span class="special">*=</span> <span class="number">9</span><span class="special">;</span> <span class="comment">// throws out_of_range exception</span>
164    <span class="identifier">i</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">;</span> <span class="comment">// throws out_of_range exception</span>
165    <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint8_t</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">4</span><span class="special">;</span>
166    <span class="keyword">auto</span> <span class="identifier">k</span> <span class="special">=</span> <span class="identifier">i</span> <span class="special">+</span> <span class="identifier">j</span><span class="special">;</span>
167
168    <span class="comment">// if either or both types are safe types, the result is a safe type</span>
169    <span class="comment">// determined by promotion policy.  In this instance</span>
170    <span class="comment">// the range of i is [7, 24] and the range of j is [0,255].</span>
171    <span class="comment">// so the type of k will be a safe type with a range of [7,279]</span>
172    <span class="identifier">static_assert</span><span class="special">(</span>
173        <span class="identifier">is_safe</span><span class="special">&lt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">k</span><span class="special">)</span><span class="special">&gt;</span><span class="special">::</span><span class="identifier">value</span>
174        <span class="special">&amp;&amp;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">k</span><span class="special">)</span><span class="special">&gt;</span><span class="special">::</span><span class="identifier">min</span><span class="special">(</span><span class="special">)</span> <span class="special">==</span> <span class="number">7</span>
175        <span class="special">&amp;&amp;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">k</span><span class="special">)</span><span class="special">&gt;</span><span class="special">::</span><span class="identifier">max</span><span class="special">(</span><span class="special">)</span> <span class="special">==</span> <span class="number">279</span><span class="special">,</span>
176        <span class="string">"k is a safe range of [7,279]"</span>
177    <span class="special">)</span><span class="special">;</span>
178<span class="special">}</span>
179
180<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span><span class="special">{</span><span class="special">}</span>
181</pre>
182</div>
183<div class="section">
184<div class="titlepage"><div><div><h4 class="title">
185<a name="idm130203099968"></a>Header</h4></div></div></div>
186<p><code class="filename"><a href="../../include/safe_range.hpp" target="_top">#include
187    &lt;boost/numeric/safe_numerics/safe_range.hpp&gt;</a></code></p>
188</div>
189</div>
190<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
191<td align="left"></td>
192<td align="right"><div class="copyright-footer">Copyright &#169; 2012-2018 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
193      Software License</a></p>
194</div></td>
195</tr></table>
196<hr>
197<div class="spirit-nav">
198<a accesskey="p" href="safe.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_literal.html"><img src="images/next.png" alt="Next"></a>
199</div>
200</body>
201</html>
202