native.xml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.native">
  5. <title>native</title>
  6. <section>
  7. <title>Description</title>
  8. <para>This type contains the functions to return a safe type corresponding
  9. to the C++ type resulting from a given arithmetic operation.</para>
  10. <para>Usage of this policy with safe types will produce the exact same
  11. arithmetic results that using normal unsafe integer types will. Hence this
  12. policy is suitable as a drop-in replacement for these unsafe types. Its
  13. main function is to trap incorrect arithmetic results when using C++ for
  14. integer arithmetic.</para>
  15. </section>
  16. <section>
  17. <title>Model of</title>
  18. <para><link
  19. linkend="safe_numerics.promotion_policy">PromotionPolicy</link></para>
  20. <para>As an example of how this works consider C++ rules from section 5 of
  21. the standard - "usual arithmetic conversions".</para>
  22. <para><programlisting>void int f(int x, int y){
  23. auto z = x + y; // z will be of type "int"
  24. return z;
  25. }</programlisting></para>
  26. <para>According to these rules, z will be of type int. Depending on the
  27. values of x and y, z may or may not contain the correct arithmetic result
  28. of the operation x + y.</para>
  29. <programlisting>using safe_int = safe&lt;int, native&gt;;
  30. void int f(safe_int x, safe_int y){
  31. auto z = x + y; // z will be of type "safe_int"
  32. return z;
  33. }</programlisting>
  34. </section>
  35. <section>
  36. <title>Example of use</title>
  37. <para>The following example illustrates the <code>native</code> type being
  38. passed as a template parameter for the type <code>safe&lt;int&gt;</code>.
  39. This example is slightly contrived in that <code>safe&lt;int&gt;</code>
  40. has <code>native</code> as its default promotion parameter so explicitly
  41. using <code>native</code> is not necessary.</para>
  42. <programlisting>#include &lt;cassert&gt;
  43. #include &lt;boost/numeric/safe_numerics/safe_integer.hpp&gt;
  44. #include &lt;boost/numeric/safe_numerics/native.hpp&gt;
  45. int main(){
  46. using namespace boost::numeric;
  47. // use native promotion policy where C++ standard arithmetic
  48. // might lead to incorrect results
  49. using safe_int8 = safe&lt;std::int8_t, native&gt;;
  50. try{
  51. safe_int8 x = 127;
  52. safe_int8 y = 2;
  53. safe_int8 z;
  54. // rather than producing an invalid result an exception is thrown
  55. z = x + y;
  56. assert(false); // never arrive here
  57. }
  58. catch(std::exception &amp; e){
  59. // which we can catch here
  60. std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
  61. }
  62. // When result is an int, C++ promotion rules guarentee
  63. // that there will be no incorrect result.
  64. // In such cases, there is no runtime overhead from using safe types.
  65. safe_int8 x = 127;
  66. safe_int8 y = 2;
  67. safe&lt;int, native&gt; z; // z can now hold the result of the addition of any two 8 bit numbers
  68. z = x + y; // is guaranteed correct without any runtime overhead or exception.
  69. return 0;
  70. }</programlisting>
  71. </section>
  72. <section>
  73. <title>Notes</title>
  74. <para>See Chapter 5, Expressions, C++ Standard</para>
  75. </section>
  76. <section>
  77. <title>Header</title>
  78. <para><code><ulink
  79. url="../../include/boost/safe_numerics/native.hpp"><code>#include
  80. &lt;boost/numeric/safe_numerics/native.hpp&gt;
  81. </code></ulink></code></para>
  82. </section>
  83. </section>