exception_policy.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>ExceptionPolicy&lt;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="concepts.html" title="Type Requirements">
  9. <link rel="prev" href="promotion_policy.html" title="PromotionPolicy&lt;PP&gt;">
  10. <link rel="next" href="types.html" title="Types">
  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_policy.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="types.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.exception_policy"></a>ExceptionPolicy&lt;EP&gt;</h3></div></div></div>
  23. <div class="toc"><dl class="toc">
  24. <dt><span class="section"><a href="exception_policy.html#idm130203635456">Description</a></span></dt>
  25. <dt><span class="section"><a href="exception_policy.html#idm130203634048">Notation</a></span></dt>
  26. <dt><span class="section"><a href="exception_policy.html#idm130203624640">Valid Expressions</a></span></dt>
  27. <dt><span class="section"><a href="exception_policy.html#idm130203608816">dispatch&lt;EP&gt;(const safe_numerics_error &amp; e, const char *
  28. msg)</a></span></dt>
  29. <dt><span class="section"><a href="exception_policy.html#idm130203580144">Models</a></span></dt>
  30. <dt><span class="section"><a href="exception_policy.html#idm130203567040">Header</a></span></dt>
  31. </dl></div>
  32. <div class="section">
  33. <div class="titlepage"><div><div><h4 class="title">
  34. <a name="idm130203635456"></a>Description</h4></div></div></div>
  35. <p>The exception policy specifies what is to occur when a safe
  36. operation cannot return a valid result. A type is an ExceptionPolicy if it
  37. has functions for handling exceptional events that occur in the course of
  38. safe numeric operations.</p>
  39. </div>
  40. <div class="section">
  41. <div class="titlepage"><div><div><h4 class="title">
  42. <a name="idm130203634048"></a>Notation</h4></div></div></div>
  43. <div class="informaltable"><table class="table">
  44. <colgroup>
  45. <col align="left">
  46. <col align="left">
  47. </colgroup>
  48. <tbody>
  49. <tr>
  50. <td align="left"><code class="computeroutput">EP</code></td>
  51. <td align="left">A type that fulfills the requirements of an
  52. ExceptionPolicy</td>
  53. </tr>
  54. <tr>
  55. <td align="left">e</td>
  56. <td align="left">A code from <a class="link" href="exception.html#safe_numerics.safe_numerics_error" title="enum class safe_numerics_error"><code class="computeroutput">safe_numerics_error</code></a>
  57. </td>
  58. </tr>
  59. <tr>
  60. <td align="left">message</td>
  61. <td align="left">A const char * which refers to a text message about the
  62. cause of an exception</td>
  63. </tr>
  64. </tbody>
  65. </table></div>
  66. </div>
  67. <div class="section">
  68. <div class="titlepage"><div><div><h4 class="title">
  69. <a name="idm130203624640"></a>Valid Expressions</h4></div></div></div>
  70. <p>Whenever an operation yield an invalid result, one of the following
  71. functions will be invoked.</p>
  72. <div class="informaltable"><table class="table">
  73. <colgroup>
  74. <col align="left">
  75. <col align="left">
  76. <col align="left">
  77. </colgroup>
  78. <thead><tr>
  79. <th align="left">Expression</th>
  80. <th align="left">Return Value</th>
  81. <th align="left">Invoked when:</th>
  82. </tr></thead>
  83. <tbody>
  84. <tr>
  85. <td align="left"><code class="computeroutput">EP::on_arithmetic_error(e, message)</code></td>
  86. <td align="left">void</td>
  87. <td align="left">The operation cannot produce valid arithmetic result such
  88. as overflows, divide by zero, etc.</td>
  89. </tr>
  90. <tr>
  91. <td align="left"><code class="computeroutput">EP::on_undefined_behavior(e,
  92. message)</code></td>
  93. <td align="left">void</td>
  94. <td align="left">The result is undefined by the C++ standard</td>
  95. </tr>
  96. <tr>
  97. <td align="left"><code class="computeroutput">EP::on_implementation_defined_behavior(e,
  98. message)</code></td>
  99. <td align="left">void</td>
  100. <td align="left">The result depends upon implementation defined behavior
  101. according to the C++ standard</td>
  102. </tr>
  103. <tr>
  104. <td align="left"><code class="computeroutput">EP::on_uninitialized_value(e,
  105. message)</code></td>
  106. <td align="left">void</td>
  107. <td align="left">A variable is not initialized</td>
  108. </tr>
  109. </tbody>
  110. </table></div>
  111. </div>
  112. <div class="section">
  113. <div class="titlepage"><div><div><h4 class="title">
  114. <a name="idm130203608816"></a>dispatch&lt;EP&gt;(const safe_numerics_error &amp; e, const char *
  115. msg)</h4></div></div></div>
  116. <p>This function is used to invoke the exception handling policy for a
  117. particular exception code.</p>
  118. <div class="section">
  119. <div class="titlepage"><div><div><h5 class="title">
  120. <a name="idm130203607584"></a>Synopsis</h5></div></div></div>
  121. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">EP</span><span class="special">&gt;</span>
  122. <span class="keyword">constexpr</span> <span class="keyword">void</span>
  123. <span class="identifier">dispatch</span><span class="special">&lt;</span><span class="identifier">EP</span><span class="special">&gt;</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">safe_numerics_error</span> <span class="special">&amp;</span> <span class="identifier">e</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">msg</span><span class="special">)</span><span class="special">;</span></pre>
  124. </div>
  125. <div class="section">
  126. <div class="titlepage"><div><div><h5 class="title">
  127. <a name="idm130203589824"></a>Example of use</h5></div></div></div>
  128. <pre class="programlisting"><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">exception_policies</span><span class="special">.</span><span class="identifier">hpp</span><span class="string">"
  129. dispatch&lt;boost::numeric::loose_exception_policy&gt;(
  130. boost::numeric::safe_numerics_error::positive_overflow_error,
  131. "</span><span class="identifier">operation</span> <span class="identifier">resulted</span> <span class="identifier">in</span> <span class="identifier">overflow</span><span class="string">"
  132. );</span></pre>
  133. </div>
  134. </div>
  135. <div class="section">
  136. <div class="titlepage"><div><div><h4 class="title">
  137. <a name="idm130203580144"></a>Models</h4></div></div></div>
  138. <p>The library header <a href="../../include/exception_policies.hpp" target="_top"><code class="computeroutput">&lt;boost/numerics/safe_numerics/exception_policies.hpp&gt;
  139. </code></a>contains a number of pre-made exception policies:</p>
  140. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  141. <li class="listitem">
  142. <p><code class="computeroutput">boost::numeric::loose_exception_policy</code></p>
  143. <p>Throw on arithmetic errors, ignore other errors. Some
  144. applications ignore these issues and still work and we don't want to
  145. update them.</p>
  146. </li>
  147. <li class="listitem">
  148. <p><code class="computeroutput">boost::numeric::loose_trap_policy</code></p>
  149. <p>Same as above in that it doesn't check for various undefined
  150. behaviors but traps at compile time for hard arithmetic errors. This
  151. policy would be suitable for older embedded systems which depend on
  152. bit manipulation operations to work.</p>
  153. </li>
  154. <li class="listitem">
  155. <p><code class="computeroutput">boost::numeric::strict_exception_policy</code></p>
  156. <p>Permit just about anything, throw at runtime on any kind of
  157. error. Recommended for new code. Check everything at compile time if
  158. possible and runtime if necessary. Trap or Throw as appropriate.
  159. Should guarantee code to be portable across architectures.</p>
  160. </li>
  161. <li class="listitem">
  162. <p><code class="computeroutput">boost::numeric::strict_trap_policy</code></p>
  163. <p>Same as above but requires code to be written in such a way as
  164. to make it impossible for errors to occur. This naturally will require
  165. extra coding effort but might be justified for embedded and/or safety
  166. critical systems.</p>
  167. </li>
  168. <li class="listitem">
  169. <p><code class="computeroutput">boost::numeric::default_exception_policy</code></p>
  170. <p>Alias for <code class="computeroutput">strict_exception_policy</code>, One would use
  171. this first. After experimentation, one might switch to one of the
  172. above policies or perhaps use a custom policy.</p>
  173. </li>
  174. </ul></div>
  175. </div>
  176. <div class="section">
  177. <div class="titlepage"><div><div><h4 class="title">
  178. <a name="idm130203567040"></a>Header</h4></div></div></div>
  179. <p><a href="../../include/concept/exception_policy.hpp" target="_top"><code class="computeroutput">#include
  180. &lt;boost/numeric/safe_numerics/concepts/exception_policy.hpp&gt;
  181. </code></a></p>
  182. </div>
  183. </div>
  184. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  185. <td align="left"></td>
  186. <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
  187. Software License</a></p>
  188. </div></td>
  189. </tr></table>
  190. <hr>
  191. <div class="spirit-nav">
  192. <a accesskey="p" href="promotion_policy.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="types.html"><img src="images/next.png" alt="Next"></a>
  193. </div>
  194. </body>
  195. </html>