safe_literal.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>safe_signed_literal&lt;Value, PP , EP&gt; and safe_unsigned_literal&lt;Value, PP, 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="types.html" title="Types">
  9. <link rel="prev" href="safe_range.html" title="safe_signed_range&lt;MIN, MAX, PP, EP&gt; and safe_unsigned_range&lt;MIN, MAX, PP, EP&gt;">
  10. <link rel="next" href="exception.html" title="exception">
  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="safe_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.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="exception.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.safe_literal"></a>safe_signed_literal&lt;Value, PP , EP&gt; and
  23. safe_unsigned_literal&lt;Value, PP, EP&gt;</h3></div></div></div>
  24. <div class="toc"><dl class="toc">
  25. <dt><span class="section"><a href="safe_literal.html#idm130203096240">Description</a></span></dt>
  26. <dt><span class="section"><a href="safe_literal.html#idm130203094736">Model of</a></span></dt>
  27. <dt><span class="section"><a href="safe_literal.html#idm130203090832">Associated Types</a></span></dt>
  28. <dt><span class="section"><a href="safe_literal.html#idm130203083840">Template Parameters</a></span></dt>
  29. <dt><span class="section"><a href="safe_literal.html#idm130203066304">Inherited Valid Expressions</a></span></dt>
  30. <dt><span class="section"><a href="safe_literal.html#idm130203034032">Example of use</a></span></dt>
  31. <dt><span class="section"><a href="safe_literal.html#safe_numerics.safe_literal.make_safe_literal"><code class="computeroutput">make_safe_literal(n, PP, EP) </code></a></span></dt>
  32. <dt><span class="section"><a href="safe_literal.html#idm130203017088">Header</a></span></dt>
  33. </dl></div>
  34. <div class="section">
  35. <div class="titlepage"><div><div><h4 class="title">
  36. <a name="idm130203096240"></a>Description</h4></div></div></div>
  37. <p>A safe type which holds a literal value. This is required to be able
  38. to initialize other safe types in such a way that an exception code is not
  39. generated. It is also useful when creating constexpr versions of safe
  40. types. It contains one immutable value known at compile time and hence can
  41. be used in any constexpr expression.</p>
  42. </div>
  43. <div class="section">
  44. <div class="titlepage"><div><div><h4 class="title">
  45. <a name="idm130203094736"></a>Model of</h4></div></div></div>
  46. <p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Integer</a></p>
  47. <p><a class="link" href="safe_numeric_concept.html" title="SafeNumeric&lt;T&gt;">SafeNumeric</a></p>
  48. <p>This type inherits all the notation, associated types and template
  49. parameters and valid expressions of <a class="link" href="safe_numeric_concept.html" title="SafeNumeric&lt;T&gt;">SafeNumeric</a> types. The
  50. following specify additional features of this type.</p>
  51. </div>
  52. <div class="section">
  53. <div class="titlepage"><div><div><h4 class="title">
  54. <a name="idm130203090832"></a>Associated Types</h4></div></div></div>
  55. <div class="informaltable"><table class="table">
  56. <colgroup>
  57. <col align="left">
  58. <col align="left">
  59. </colgroup>
  60. <tbody>
  61. <tr>
  62. <td align="left"><code class="computeroutput">PP</code></td>
  63. <td align="left">A type which specifies the result type of an expression
  64. using safe types.</td>
  65. </tr>
  66. <tr>
  67. <td align="left"><code class="computeroutput">EP</code></td>
  68. <td align="left">A type containing members which are called when a correct
  69. result cannot be returned</td>
  70. </tr>
  71. </tbody>
  72. </table></div>
  73. </div>
  74. <div class="section">
  75. <div class="titlepage"><div><div><h4 class="title">
  76. <a name="idm130203083840"></a>Template Parameters</h4></div></div></div>
  77. <div class="informaltable"><table class="table">
  78. <colgroup>
  79. <col align="left">
  80. <col align="left">
  81. <col align="left">
  82. </colgroup>
  83. <thead><tr>
  84. <th align="left">Parameter</th>
  85. <th align="left">Type Requirements</th>
  86. <th align="left">Description</th>
  87. </tr></thead>
  88. <tbody>
  89. <tr>
  90. <td align="left"><code class="computeroutput">Value</code></td>
  91. <td align="left"><code class="computeroutput"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Integer</a></code></td>
  92. <td align="left">value used to initialize the literal</td>
  93. </tr>
  94. <tr>
  95. <td align="left"><code class="computeroutput">PP</code></td>
  96. <td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">PromotionPolicy&lt;PP&gt;</a></td>
  97. <td align="left"><p>Optional promotion policy. Default value is
  98. <code class="computeroutput">void</code></p></td>
  99. </tr>
  100. <tr>
  101. <td align="left"><code class="computeroutput">EP</code></td>
  102. <td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Exception
  103. Policy&lt;EP&gt;</a></td>
  104. <td align="left"><p>Optional exception policy. Default value is
  105. <code class="computeroutput">void</code></p></td>
  106. </tr>
  107. </tbody>
  108. </table></div>
  109. </div>
  110. <div class="section">
  111. <div class="titlepage"><div><div><h4 class="title">
  112. <a name="idm130203066304"></a>Inherited Valid Expressions</h4></div></div></div>
  113. <p>safe literal types are immutable. Hence they only inherit those
  114. valid expressions which don't change the value. <span class="emphasis"><em>This excludes
  115. assignment, increment, and decrement and all unary operators except unary
  116. -, + and ~</em></span>. Other than that, they can be used anywhere a <a class="link" href="safe_numeric_concept.html" title="SafeNumeric&lt;T&gt;">SafeNumeric</a> type can
  117. be used. Note that the default promotion and exception policies are void.
  118. This is usually convenient since when a safe literal is used in a binary
  119. operation, this will inherit the policies of the other type. On the other
  120. hand, this can be inconvenient when operands of a binary expression are
  121. both safe literals. This will fail to compile since there are no
  122. designated promotion and exception policies. The way to address this to
  123. assign specific policies as in this example.</p>
  124. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
  125. <span class="keyword">using</span> <span class="identifier">compile_time_value</span> <span class="special">=</span> <span class="identifier">safe_signed_literal</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span><span class="special">;</span>
  126. <span class="keyword">constexpr</span> <span class="identifier">compile_time_value</span><span class="special">&lt;</span><span class="number">1000</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">;</span>
  127. <span class="keyword">constexpr</span> <span class="identifier">compile_time_value</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">;</span>
  128. <span class="comment">// should compile and execute without problem</span>
  129. <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">x</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
  130. <span class="comment">// all the following statements should fail to compile because there are </span>
  131. <span class="comment">// no promotion and exception policies specified.</span>
  132. <span class="keyword">constexpr</span> <span class="identifier">safe</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</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>
  133. </pre>
  134. </div>
  135. <div class="section">
  136. <div class="titlepage"><div><div><h4 class="title">
  137. <a name="idm130203034032"></a>Example of use</h4></div></div></div>
  138. <pre class="programlisting"><a href="../../include/safe_integer_literal.hpp" target="_top"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">numeric</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer_literal</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></a>
  139. <span class="keyword">constexpr</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">safe_signed_literal</span><span class="special">&lt;</span><span class="number">42</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">;</span>
  140. </pre>
  141. </div>
  142. <div class="section">
  143. <div class="titlepage"><div><div><h4 class="title">
  144. <a name="safe_numerics.safe_literal.make_safe_literal"></a><code class="computeroutput">make_safe_literal(n, PP, EP) </code>
  145. </h4></div></div></div>
  146. <p>This is a macro which returns an instance of a safe literal type.
  147. This instance will hold the value n. The type of the value returned will
  148. be the smallest safe type which can hold the value <code class="computeroutput">n</code>.</p>
  149. </div>
  150. <div class="section">
  151. <div class="titlepage"><div><div><h4 class="title">
  152. <a name="idm130203017088"></a>Header</h4></div></div></div>
  153. <p><a href="../../include/safe_integer_literal.hpp" target="_top">#include
  154. &lt;boost/numeric/safe_numerics/safe_integer_literal.hpp&gt;</a></p>
  155. </div>
  156. </div>
  157. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  158. <td align="left"></td>
  159. <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
  160. Software License</a></p>
  161. </div></td>
  162. </tr></table>
  163. <hr>
  164. <div class="spirit-nav">
  165. <a accesskey="p" href="safe_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.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="exception.html"><img src="images/next.png" alt="Next"></a>
  166. </div>
  167. </body>
  168. </html>