1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> 4<title>exception_policy<AE, IDB, UB, UV></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="exception.html" title="exception"> 10<link rel="next" href="promotion_policies.html" title="Promotion Policies"> 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="exception.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="promotion_policies.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_numeric.exception_policies"></a>exception_policy<AE, IDB, UB, UV></h3></div></div></div> 23<div class="toc"><dl class="toc"> 24<dt><span class="section"><a href="exception_policies.html#idm130202951248">Description</a></span></dt> 25<dt><span class="section"><a href="exception_policies.html#idm130202949952">Template Parameters</a></span></dt> 26<dt><span class="section"><a href="exception_policies.html#idm130202934048">Model of</a></span></dt> 27<dt><span class="section"><a href="exception_policies.html#idm130202932336">Inherited Valid Expressions</a></span></dt> 28<dt><span class="section"><a href="exception_policies.html#idm130202930224">Function Objects</a></span></dt> 29<dt><span class="section"><a href="exception_policies.html#idm130202892272">Policies Provided by the library</a></span></dt> 30<dt><span class="section"><a href="exception_policies.html#idm130202855952">Header</a></span></dt> 31</dl></div> 32<div class="section"> 33<div class="titlepage"><div><div><h4 class="title"> 34<a name="idm130202951248"></a>Description</h4></div></div></div> 35<p>Create a valid exception policy from 4 function objects. This 36 specifies the actions to be taken for different types of invalid 37 results.</p> 38</div> 39<div class="section"> 40<div class="titlepage"><div><div><h4 class="title"> 41<a name="idm130202949952"></a>Template Parameters</h4></div></div></div> 42<div class="informaltable"><table class="table"> 43<colgroup> 44<col align="left"> 45<col align="left"> 46<col align="left"> 47</colgroup> 48<thead><tr> 49<th align="left">Parameter</th> 50<th align="left">Type Requirements</th> 51<th align="left">Invoked when:</th> 52</tr></thead> 53<tbody> 54<tr> 55<td align="left"><code class="computeroutput">AE</code></td> 56<td align="left">Function object callable with the expression AE(e, 57 message)</td> 58<td align="left"><p>The operation cannot produce valid arithmetic result 59 such as overflows, divide by zero, etc.</p></td> 60</tr> 61<tr> 62<td align="left"><code class="computeroutput">UB</code></td> 63<td align="left">Function object callable with the expression UB(e, 64 message)</td> 65<td align="left"><p>The result is undefined by the C++ 66 standard</p></td> 67</tr> 68<tr> 69<td align="left"><code class="computeroutput">IDB</code></td> 70<td align="left">Function object callable with the expression IDB(e,</td> 71<td align="left"><p>The result depends upon implementation defined 72 behavior according to the C++ standard</p></td> 73</tr> 74<tr> 75<td align="left"><code class="computeroutput">UV</code></td> 76<td align="left">Function object callable with the expression UV(e, 77 message)</td> 78<td align="left"><p>A variable is not initialized</p></td> 79</tr> 80</tbody> 81</table></div> 82</div> 83<div class="section"> 84<div class="titlepage"><div><div><h4 class="title"> 85<a name="idm130202934048"></a>Model of</h4></div></div></div> 86<p><a class="link" href="promotion_policy.html" title="PromotionPolicy<PP>">ExceptionPolicy</a></p> 87</div> 88<div class="section"> 89<div class="titlepage"><div><div><h4 class="title"> 90<a name="idm130202932336"></a>Inherited Valid Expressions</h4></div></div></div> 91<p>This class implements all the valid operations from the type 92 requirements <a class="link" href="promotion_policy.html" title="PromotionPolicy<PP>">ExceptionPolicy</a>. Aside 93 from these, there are no other operations implemented.</p> 94</div> 95<div class="section"> 96<div class="titlepage"><div><div><h4 class="title"> 97<a name="idm130202930224"></a>Function Objects</h4></div></div></div> 98<p>In order to create an exception policy, one needs some function 99 objects. The library includes some appropriate examples of these:</p> 100<div class="informaltable"><table class="table"> 101<colgroup> 102<col align="left"> 103<col align="left"> 104</colgroup> 105<thead><tr> 106<th align="left">Name</th> 107<th align="left">Description</th> 108</tr></thead> 109<tbody> 110<tr> 111<td align="left"><code class="computeroutput">ignore_exception</code></td> 112<td align="left">Ignore any runtime exception and just return - thus 113 propagating the error. This is what would happen with unsafe data 114 types</td> 115</tr> 116<tr> 117<td align="left"><code class="computeroutput">throw_exception</code></td> 118<td align="left">throw an exception of type std::system_error</td> 119</tr> 120<tr> 121<td align="left"><code class="computeroutput">trap_exception</code></td> 122<td align="left">Invoke a function which is undefined. Compilers will 123 include this function if and only if there is a possibility of a 124 runtime error. Conversely, This will create a compile time error 125 if there is any possibility that the operation will fail at 126 runtime. Use the action to guarantee that your application will 127 never produce an invalid result. Any operation invoke</td> 128</tr> 129</tbody> 130</table></div> 131<p>But of course one is free to provide his own. Here is an example of 132 a function object which would could be used exception conditions.</p> 133<pre class="programlisting"><span class="comment">// log an exception condition but continue processing as though nothing has happened</span> 134<span class="comment">// this would emulate the behavior of an unsafe type.</span> 135<span class="keyword">struct</span> <span class="identifier">log_runtime_exception</span> <span class="special">{</span> 136 <span class="identifier">log_runtime_exception</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">safe_numerics_error</span> <span class="special">&</span> <span class="identifier">e</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span> <span class="identifier">message</span><span class="special">)</span><span class="special">{</span> 137 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Caught system_error with code "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">code</span><span class="special">(</span><span class="special">)</span> 138 <span class="special"><<</span> <span class="string">" meaning "</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="char">'\n'</span><span class="special">;</span> 139 <span class="special">}</span> 140<span class="special">}</span><span class="special">;</span> 141</pre> 142</div> 143<div class="section"> 144<div class="titlepage"><div><div><h4 class="title"> 145<a name="idm130202892272"></a>Policies Provided by the library</h4></div></div></div> 146<p>The above function object can be composed into an exception policy 147 by this class. The library provides common policies all ready to use. In 148 the table below, the word "loose" is used to indicate that implementation 149 defined and undefined behavior is not considered an exceptional condition, 150 while "strict" means the opposite. The word "exception" means that a 151 runtime exception will be thrown. The word "trap" means that the mere 152 possibility of an error condition will result in a compile time 153 error.</p> 154<div class="informaltable"><table class="table"> 155<colgroup> 156<col align="left"> 157<col align="left"> 158</colgroup> 159<thead><tr> 160<th align="left">Name</th> 161<th align="left">Description</th> 162</tr></thead> 163<tbody> 164<tr> 165<td align="left"> 166<a name="safe_numerics.exception_policies.loose_exception_policy"></a><a class="link" href="exception_policies.html#safe_numerics.exception_policies.loose_exception_policy">loose_exception_policy</a> 167</td> 168<td align="left">Throws runtime exception on any arithmetic error. 169 Undefined and implementation defined behavior is permitted as 170 long as it does not produce an arithmetic error.</td> 171</tr> 172<tr> 173<td align="left"> 174<a name="safe_numerics.exception_policies.loose_trap_policy"></a><code class="computeroutput"><a class="link" href="exception_policies.html#safe_numerics.exception_policies.loose_trap_policy">loose_trap_policy</a></code> 175</td> 176<td align="left">Invoke a compile time error in any case where it's 177 possible to result in an arithmetic error.</td> 178</tr> 179<tr> 180<td align="left"> 181<a name="safe_numerics.exception_policies.strict_exception_policy"></a><code class="computeroutput"><a class="link" href="exception_policies.html#safe_numerics.exception_policies.strict_exception_policy">strict_exception_policy</a></code> 182</td> 183<td align="left">Throws runtime exception on any arithmetic error. Any 184 undefined or implementation defined behavior also results in 185 throwing an exception.</td> 186</tr> 187<tr> 188<td align="left"> 189<a name="safe_numerics.exception_policies.strict_trap_policy"></a><code class="computeroutput"><a class="link" href="exception_policies.html#safe_numerics.exception_policies.strict_trap_policy">strict_trap_policy</a></code> 190</td> 191<td align="left">Invoke a compile time error in any case where it's 192 possible to result in an arithmetic error, undefined behavior or 193 implementation defined behavior</td> 194</tr> 195<tr> 196<td align="left"> 197<a name="safe_numerics.exception_policies.default_exception_policy"></a><code class="computeroutput"><a class="link" href="exception_policies.html#safe_numerics.exception_policies.default_exception_policy">default_exception_policy</a></code> 198</td> 199<td align="left">an alias for <code class="computeroutput"><a class="link" href="exception_policies.html#safe_numerics.exception_policies.strict_exception_policy">strict_exception_policy</a></code> 200</td> 201</tr> 202</tbody> 203</table></div> 204<p>If none of the above suit your needs, you're free to 205 create your own. Here is one where use the logging function object defined 206 above as a component in a loose exception policy which logs any arithmetic 207 errors and ignores any other types of errors.</p> 208<pre class="programlisting"><span class="comment">// logging policy</span> 209<span class="comment">// log arithmetic errors but ignore them and continue to execute</span> 210<span class="comment">// implementation defined and undefined behavior is just executed</span> 211<span class="comment">// without logging.</span> 212 213<span class="keyword">using</span> <span class="identifier">logging_exception_policy</span> <span class="special">=</span> <span class="identifier">exception_policy</span><span class="special"><</span> 214 <span class="identifier">log_runtime_exception</span><span class="special">,</span> <span class="comment">// arithmetic error</span> 215 <span class="identifier">ignore_exception</span><span class="special">,</span> <span class="comment">// implementation defined behavior</span> 216 <span class="identifier">ignore_exception</span><span class="special">,</span> <span class="comment">// undefined behavior</span> 217 <span class="identifier">ignore_exception</span> <span class="comment">// uninitialized value</span> 218<span class="special">></span><span class="special">;</span> 219</pre> 220</div> 221<div class="section"> 222<div class="titlepage"><div><div><h4 class="title"> 223<a name="idm130202855952"></a>Header</h4></div></div></div> 224<p><a href="../../include/concept/exception_policy.hpp" target="_top"><code class="computeroutput">#include 225 <boost/numeric/safe_numerics/concept/exception_policy.hpp></code></a></p> 226</div> 227</div> 228<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 229<td align="left"></td> 230<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 231 Software License</a></p> 232</div></td> 233</tr></table> 234<hr> 235<div class="spirit-nav"> 236<a accesskey="p" href="exception.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="promotion_policies.html"><img src="images/next.png" alt="Next"></a> 237</div> 238</body> 239</html> 240