automatic.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>automatic</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="native.html" title="native">
  10. <link rel="next" href="cpp.html" title="cpp&lt;int C, int S, int I, int L, int LL&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="native.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="cpp.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.automatic"></a>automatic</h4></div></div></div>
  23. <div class="section">
  24. <div class="titlepage"><div><div><h5 class="title">
  25. <a name="idm130202722656"></a>Description</h5></div></div></div>
  26. <p>This type contains the meta-functions to return a type with
  27. sufficient capacity to hold the result of a given binary arithmetic
  28. operation.</p>
  29. <p>The standard C/C++ procedure for executing arithmetic operations on
  30. operands of different types is:</p>
  31. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  32. <li class="listitem"><p>Convert operands to some common type using a somewhat
  33. elaborate elaborate rules defined in the C++ standard.</p></li>
  34. <li class="listitem"><p>Execute the operation.</p></li>
  35. <li class="listitem"><p>If the result of the operation cannot fit in the common type
  36. of the operands, discard the high order bits.</p></li>
  37. </ul></div>
  38. <p>The automatic promotion policy replaces the standard C/C++ procedure
  39. for the following one:</p>
  40. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  41. <li class="listitem">
  42. <p>Convert operands to some common type using to the following
  43. rules.</p>
  44. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
  45. <li class="listitem"><p>For addition. If the operands are both unsigned the
  46. common type will be unsigned. Otherwise it will be
  47. signed.</p></li>
  48. <li class="listitem"><p>For subtraction, the common type will be signed.</p></li>
  49. <li class="listitem"><p>For left/right shift, the sign of the result will be the
  50. sign of the left operand.</p></li>
  51. <li class="listitem"><p>For all other types of operants, if both operands are
  52. unsigned the common type will be unsigned. Otherwise, it will
  53. be signed.</p></li>
  54. </ul></div>
  55. </li>
  56. <li class="listitem"><p>Determine the smallest size of the signed or unsigned type
  57. which can be guaranteed hold the result.</p></li>
  58. <li class="listitem"><p>If this size exceeds the maximum size supported by the
  59. compiler, use the maximum size supported by the compiler.</p></li>
  60. <li class="listitem">
  61. <p>Execute the operation.</p>
  62. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
  63. <li class="listitem"><p>Convert each operand to the common type.</p></li>
  64. <li class="listitem"><p>If the result cannot be contained in the result type as
  65. above, invoke an error procedure.</p></li>
  66. <li class="listitem"><p>Otherwise, return the result in the common type</p></li>
  67. </ul></div>
  68. </li>
  69. </ul></div>
  70. <p>This type promotion policy is applicable only to safe types whose
  71. base type is an <a class="link" href="integer.html" title="Integer&lt;T&gt;">Integer</a>
  72. type.</p>
  73. </div>
  74. <div class="section">
  75. <div class="titlepage"><div><div><h5 class="title">
  76. <a name="idm130202706000"></a>Model of</h5></div></div></div>
  77. <p><a class="link" href="promotion_policy.html" title="PromotionPolicy&lt;PP&gt;">PromotionPolicy</a></p>
  78. </div>
  79. <div class="section">
  80. <div class="titlepage"><div><div><h5 class="title">
  81. <a name="idm130202704320"></a>Example of use</h5></div></div></div>
  82. <p>The following example illustrates the <code class="computeroutput">automatic</code> type
  83. being passed as a template parameter for the type
  84. <code class="computeroutput">safe&lt;int&gt;</code>.</p>
  85. <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">safe_integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  86. <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">automatic</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  87. <span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span><span class="special">{</span>
  88. <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>
  89. <span class="comment">// use automatic promotion policy where C++ standard arithmetic </span>
  90. <span class="comment">// might lead to incorrect results</span>
  91. <span class="keyword">using</span> <span class="identifier">safe_t</span> <span class="special">=</span> <span class="identifier">safe</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int8_t</span><span class="special">,</span> <span class="identifier">automatic</span><span class="special">&gt;</span><span class="special">;</span>
  92. <span class="comment">// In such cases, there is no runtime overhead from using safe types.</span>
  93. <span class="identifier">safe_t</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">127</span><span class="special">;</span>
  94. <span class="identifier">safe_t</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
  95. <span class="comment">// z is guaranteed correct without any runtime overhead or exception.</span>
  96. <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>
  97. <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
  98. <span class="special">}</span></pre>
  99. </div>
  100. <div class="section">
  101. <div class="titlepage"><div><div><h5 class="title">
  102. <a name="idm130202662272"></a>Header</h5></div></div></div>
  103. <p><code class="computeroutput"><a href="../../include/automatic.hpp" target="_top"><code class="computeroutput">#include
  104. &lt;boost/numeric/safe_numerics/automatic.hpp&gt;
  105. </code></a></code></p>
  106. </div>
  107. </div>
  108. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  109. <td align="left"></td>
  110. <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
  111. Software License</a></p>
  112. </div></td>
  113. </tr></table>
  114. <hr>
  115. <div class="spirit-nav">
  116. <a accesskey="p" href="native.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="cpp.html"><img src="../images/next.png" alt="Next"></a>
  117. </div>
  118. </body>
  119. </html>