1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> 4<title>native</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="../promotion_policies.html" title="Promotion Policies"> 9<link rel="prev" href="../promotion_policies.html" title="Promotion Policies"> 10<link rel="next" href="automatic.html" title="automatic"> 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="../promotion_policies.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../promotion_policies.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="automatic.html"><img src="../images/next.png" alt="Next"></a> 19</div> 20<div class="section"> 21<div class="titlepage"><div><div><h4 class="title"> 22<a name="safe_numerics.promotion_policies.native"></a>native</h4></div></div></div> 23<div class="section"> 24<div class="titlepage"><div><div><h5 class="title"> 25<a name="idm130202851568"></a>Description</h5></div></div></div> 26<p>This type contains the functions to return a safe type corresponding 27 to the C++ type resulting from a given arithmetic operation.</p> 28<p>Usage of this policy with safe types will produce the exact same 29 arithmetic results that using normal unsafe integer types will. Hence this 30 policy is suitable as a drop-in replacement for these unsafe types. Its 31 main function is to trap incorrect arithmetic results when using C++ for 32 integer arithmetic.</p> 33</div> 34<div class="section"> 35<div class="titlepage"><div><div><h5 class="title"> 36<a name="idm130202849536"></a>Model of</h5></div></div></div> 37<p><a class="link" href="promotion_policy.html" title="PromotionPolicy<PP>">PromotionPolicy</a></p> 38<p>As an example of how this works consider C++ rules from section 5 of 39 the standard - "usual arithmetic conversions".</p> 40<pre class="programlisting"><span class="keyword">void</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">y</span><span class="special">)</span><span class="special">{</span> 41 <span class="keyword">auto</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span> <span class="comment">// z will be of type "int"</span> 42 <span class="keyword">return</span> <span class="identifier">z</span><span class="special">;</span> 43<span class="special">}</span></pre> 44<p>According to these rules, z will be of type int. Depending on the 45 values of x and y, z may or may not contain the correct arithmetic result 46 of the operation x + y.</p> 47<pre class="programlisting"><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">native</span><span class="special">></span><span class="special">;</span> 48<span class="keyword">void</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">safe_int</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">safe_int</span> <span class="identifier">y</span><span class="special">)</span><span class="special">{</span> 49 <span class="keyword">auto</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span> <span class="comment">// z will be of type "safe_int"</span> 50 <span class="keyword">return</span> <span class="identifier">z</span><span class="special">;</span> 51<span class="special">}</span></pre> 52</div> 53<div class="section"> 54<div class="titlepage"><div><div><h5 class="title"> 55<a name="idm130202813168"></a>Example of use</h5></div></div></div> 56<p>The following example illustrates the <code class="computeroutput">native</code> type being 57 passed as a template parameter for the type <code class="computeroutput">safe<int></code>. 58 This example is slightly contrived in that <code class="computeroutput">safe<int></code> 59 has <code class="computeroutput">native</code> as its default promotion parameter so explicitly 60 using <code class="computeroutput">native</code> is not necessary.</p> 61<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cassert</span><span class="special">></span> 62<span class="preprocessor">#include</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">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> 63<span class="preprocessor">#include</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">safe_numerics</span><span class="special">/</span><span class="identifier">native</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 64<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span><span class="special">{</span> 65 <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> 66 <span class="comment">// use native promotion policy where C++ standard arithmetic </span> 67 <span class="comment">// might lead to incorrect results</span> 68 <span class="keyword">using</span> <span class="identifier">safe_int8</span> <span class="special">=</span> <span class="identifier">safe</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int8_t</span><span class="special">,</span> <span class="identifier">native</span><span class="special">></span><span class="special">;</span> 69 <span class="keyword">try</span><span class="special">{</span> 70 <span class="identifier">safe_int8</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">127</span><span class="special">;</span> 71 <span class="identifier">safe_int8</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> 72 <span class="identifier">safe_int8</span> <span class="identifier">z</span><span class="special">;</span> 73 <span class="comment">// rather than producing an invalid result an exception is thrown</span> 74 <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span> 75 <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">)</span><span class="special">;</span> <span class="comment">// never arrive here</span> 76 <span class="special">}</span> 77 <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> 78 <span class="comment">// which we can catch here</span> 79 <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> 80 <span class="special">}</span> 81 82 <span class="comment">// When result is an int, C++ promotion rules guarentee </span> 83 <span class="comment">// that there will be no incorrect result.</span> 84 <span class="comment">// In such cases, there is no runtime overhead from using safe types.</span> 85 <span class="identifier">safe_int8</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">127</span><span class="special">;</span> 86 <span class="identifier">safe_int8</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> 87 <span class="identifier">safe</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">native</span><span class="special">></span> <span class="identifier">z</span><span class="special">;</span> <span class="comment">// z can now hold the result of the addition of any two 8 bit numbers</span> 88 <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span> <span class="comment">// is guaranteed correct without any runtime overhead or exception.</span> 89 90 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 91<span class="special">}</span></pre> 92</div> 93<div class="section"> 94<div class="titlepage"><div><div><h5 class="title"> 95<a name="idm130202727600"></a>Notes</h5></div></div></div> 96<p>See Chapter 5, Expressions, C++ Standard</p> 97</div> 98<div class="section"> 99<div class="titlepage"><div><div><h5 class="title"> 100<a name="idm130202726384"></a>Header</h5></div></div></div> 101<p><code class="computeroutput"><a href="../../include/native.hpp" target="_top"><code class="computeroutput">#include 102 <boost/numeric/safe_numerics/native.hpp> 103 </code></a></code></p> 104</div> 105</div> 106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 107<td align="left"></td> 108<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 109 Software License</a></p> 110</div></td> 111</tr></table> 112<hr> 113<div class="spirit-nav"> 114<a accesskey="p" href="../promotion_policies.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../promotion_policies.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="automatic.html"><img src="../images/next.png" alt="Next"></a> 115</div> 116</body> 117</html> 118