• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
127<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>
128<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>
129
130<span class="comment">// should compile and execute without problem</span>
131
132<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>
133
134<span class="comment">// all the following statements should fail to compile because there are </span>
135<span class="comment">// no promotion and exception policies specified.</span>
136<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>
137</pre>
138</div>
139<div class="section">
140<div class="titlepage"><div><div><h4 class="title">
141<a name="idm130203034032"></a>Example of use</h4></div></div></div>
142<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>
143
144<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>
145</pre>
146</div>
147<div class="section">
148<div class="titlepage"><div><div><h4 class="title">
149<a name="safe_numerics.safe_literal.make_safe_literal"></a><code class="computeroutput">make_safe_literal(n, PP, EP) </code>
150</h4></div></div></div>
151<p>This is a macro which returns an instance of a safe literal type.
152    This instance will hold the value n. The type of the value returned will
153    be the smallest safe type which can hold the value <code class="computeroutput">n</code>.</p>
154</div>
155<div class="section">
156<div class="titlepage"><div><div><h4 class="title">
157<a name="idm130203017088"></a>Header</h4></div></div></div>
158<p><a href="../../include/safe_integer_literal.hpp" target="_top">#include
159    &lt;boost/numeric/safe_numerics/safe_integer_literal.hpp&gt;</a></p>
160</div>
161</div>
162<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
163<td align="left"></td>
164<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
165      Software License</a></p>
166</div></td>
167</tr></table>
168<hr>
169<div class="spirit-nav">
170<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>
171</div>
172</body>
173</html>
174