safe_literal.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.safe_literal">
  5. <title>safe_signed_literal&lt;Value, PP , EP&gt; and
  6. safe_unsigned_literal&lt;Value, PP, EP&gt;</title>
  7. <?dbhtml stop-chunking?>
  8. <section>
  9. <title>Description</title>
  10. <para>A safe type which holds a literal value. This is required to be able
  11. to initialize other safe types in such a way that an exception code is not
  12. generated. It is also useful when creating constexpr versions of safe
  13. types. It contains one immutable value known at compile time and hence can
  14. be used in any constexpr expression.</para>
  15. </section>
  16. <section>
  17. <title>Model of</title>
  18. <para><link linkend="safe_numerics.numeric">Integer</link></para>
  19. <para><link
  20. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link></para>
  21. <para>This type inherits all the notation, associated types and template
  22. parameters and valid expressions of <link
  23. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link> types. The
  24. following specify additional features of this type.</para>
  25. </section>
  26. <section>
  27. <title>Associated Types</title>
  28. <informaltable>
  29. <tgroup cols="2">
  30. <colspec align="left" colwidth="1*"/>
  31. <colspec align="left" colwidth="10*"/>
  32. <tbody>
  33. <row>
  34. <entry><code>PP</code></entry>
  35. <entry>A type which specifies the result type of an expression
  36. using safe types.</entry>
  37. </row>
  38. <row>
  39. <entry><code>EP</code></entry>
  40. <entry>A type containing members which are called when a correct
  41. result cannot be returned</entry>
  42. </row>
  43. </tbody>
  44. </tgroup>
  45. </informaltable>
  46. </section>
  47. <section>
  48. <title>Template Parameters</title>
  49. <informaltable>
  50. <tgroup cols="3">
  51. <colspec align="left" colwidth="1*"/>
  52. <colspec align="left" colwidth="1*"/>
  53. <colspec align="left" colwidth="3*"/>
  54. <thead>
  55. <row>
  56. <entry align="left">Parameter</entry>
  57. <entry align="left">Type Requirements</entry>
  58. <entry>Description</entry>
  59. </row>
  60. </thead>
  61. <tbody>
  62. <row>
  63. <entry><code>Value</code></entry>
  64. <entry><code><link
  65. linkend="safe_numerics.numeric">Integer</link></code></entry>
  66. <entry>value used to initialize the literal</entry>
  67. </row>
  68. <row>
  69. <entry><code>PP</code></entry>
  70. <entry><link linkend="safe_numerics.numeric"><link
  71. linkend="safe_numerics.promotion_policy">PromotionPolicy&lt;PP&gt;</link></link></entry>
  72. <entry><para>Optional promotion policy. Default value is
  73. <code>void</code></para></entry>
  74. </row>
  75. <row>
  76. <entry><code>EP</code></entry>
  77. <entry><link linkend="safe_numerics.numeric"><link
  78. linkend="safe_numerics.exception_policy">Exception
  79. Policy&lt;EP&gt;</link></link></entry>
  80. <entry><para>Optional exception policy. Default value is
  81. <code>void</code></para></entry>
  82. </row>
  83. </tbody>
  84. </tgroup>
  85. </informaltable>
  86. </section>
  87. <section>
  88. <title>Inherited Valid Expressions</title>
  89. <para>safe literal types are immutable. Hence they only inherit those
  90. valid expressions which don't change the value. <emphasis>This excludes
  91. assignment, increment, and decrement and all unary operators except unary
  92. -, + and ~</emphasis>. Other than that, they can be used anywhere a <link
  93. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link> type can
  94. be used. Note that the default promotion and exception policies are void.
  95. This is usually convenient since when a safe literal is used in a binary
  96. operation, this will inherit the policies of the other type. On the other
  97. hand, this can be inconvenient when operands of a binary expression are
  98. both safe literals. This will fail to compile since there are no
  99. designated promotion and exception policies. The way to address this to
  100. assign specific policies as in this example.</para>
  101. <para><programlisting>template&lt;typename T&gt;
  102. using compile_time_value = safe_signed_literal&lt;T&gt;;
  103. constexpr compile_time_value&lt;1000&gt; x;
  104. constexpr compile_time_value&lt;0&gt; y;
  105. // should compile and execute without problem
  106. std::cout &lt;&lt; x &lt;&lt; '\n';
  107. // all the following statements should fail to compile because there are
  108. // no promotion and exception policies specified.
  109. constexpr safe&lt;int&gt; z = x / y;
  110. </programlisting></para>
  111. </section>
  112. <section>
  113. <title>Example of use</title>
  114. <programlisting>#include &lt;boost/numeric/safe_numerics/safe_integer_literal.hpp&gt;
  115. constexpr boost::numeric::safe_signed_literal&lt;42&gt; x;
  116. </programlisting>
  117. </section>
  118. <section id="safe_numerics.safe_literal.make_safe_literal">
  119. <title><code>make_safe_literal(n, PP, EP) </code></title>
  120. <para>This is a macro which returns an instance of a safe literal type.
  121. This instance will hold the value n. The type of the value returned will
  122. be the smallest safe type which can hold the value <code>n</code>.</para>
  123. </section>
  124. <section>
  125. <title>Header</title>
  126. <para><ulink
  127. url="../../include/boost/safe_numerics/safe_integer_literal.hpp">#include
  128. &lt;boost/numeric/safe_numerics/safe_integer_literal.hpp&gt;</ulink></para>
  129. </section>
  130. </section>