automatic.xml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.promotion_policies.automatic">
  5. <title>automatic</title>
  6. <section>
  7. <title>Description</title>
  8. <para>This type contains the meta-functions to return a type with
  9. sufficient capacity to hold the result of a given binary arithmetic
  10. operation.</para>
  11. <para>The standard C/C++ procedure for executing arithmetic operations on
  12. operands of different types is:<itemizedlist>
  13. <listitem>
  14. <para>Convert operands to some common type using a somewhat
  15. elaborate elaborate rules defined in the C++ standard.</para>
  16. </listitem>
  17. <listitem>
  18. <para>Execute the operation.</para>
  19. </listitem>
  20. <listitem>
  21. <para>If the result of the operation cannot fit in the common type
  22. of the operands, discard the high order bits.</para>
  23. </listitem>
  24. </itemizedlist></para>
  25. <para>The automatic promotion policy replaces the standard C/C++ procedure
  26. for the following one:<itemizedlist>
  27. <listitem>
  28. <para>Convert operands to some common type using to the following
  29. rules.<itemizedlist>
  30. <listitem>
  31. <para>For addition. If the operands are both unsigned the
  32. common type will be unsigned. Otherwise it will be
  33. signed.</para>
  34. </listitem>
  35. <listitem>
  36. <para>For subtraction, the common type will be signed.</para>
  37. </listitem>
  38. <listitem>
  39. <para>For left/right shift, the sign of the result will be the
  40. sign of the left operand.</para>
  41. </listitem>
  42. <listitem>
  43. <para>For all other types of operants, if both operands are
  44. unsigned the common type will be unsigned. Otherwise, it will
  45. be signed.</para>
  46. </listitem>
  47. </itemizedlist></para>
  48. </listitem>
  49. <listitem>
  50. <para>Determine the smallest size of the signed or unsigned type
  51. which can be guaranteed hold the result.</para>
  52. </listitem>
  53. <listitem>
  54. <para>If this size exceeds the maximum size supported by the
  55. compiler, use the maximum size supported by the compiler.</para>
  56. </listitem>
  57. <listitem>
  58. <para>Execute the operation.<itemizedlist>
  59. <listitem>
  60. <para>Convert each operand to the common type.</para>
  61. </listitem>
  62. <listitem>
  63. <para>If the result cannot be contained in the result type as
  64. above, invoke an error procedure.</para>
  65. </listitem>
  66. <listitem>
  67. <para>Otherwise, return the result in the common type</para>
  68. </listitem>
  69. </itemizedlist></para>
  70. </listitem>
  71. </itemizedlist></para>
  72. <para>This type promotion policy is applicable only to safe types whose
  73. base type is an <link linkend="safe_numerics.integer">Integer</link>
  74. type.</para>
  75. </section>
  76. <section>
  77. <title>Model of</title>
  78. <para><link
  79. linkend="safe_numerics.promotion_policy">PromotionPolicy</link></para>
  80. </section>
  81. <section>
  82. <title>Example of use</title>
  83. <para>The following example illustrates the <code>automatic</code> type
  84. being passed as a template parameter for the type
  85. <code>safe&lt;int&gt;</code>.</para>
  86. <programlisting>#include &lt;boost/safe_numerics/safe_integer.hpp&gt;
  87. #include &lt;boost/safe_numerics/automatic.hpp&gt;
  88. int main(){
  89. using namespace boost::numeric;
  90. // use automatic promotion policy where C++ standard arithmetic
  91. // might lead to incorrect results
  92. using safe_t = safe&lt;std::int8_t, automatic&gt;;
  93. // In such cases, there is no runtime overhead from using safe types.
  94. safe_t x = 127;
  95. safe_t y = 2;
  96. // z is guaranteed correct without any runtime overhead or exception.
  97. auto z = x + y;
  98. return 0;
  99. }</programlisting>
  100. </section>
  101. <section>
  102. <title>Header</title>
  103. <para><code><ulink
  104. url="../../include/boost/safe_numerics/automatic.hpp"><code>#include
  105. &lt;boost/numeric/safe_numerics/automatic.hpp&gt;
  106. </code></ulink></code></para>
  107. </section>
  108. </section>