]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Literal Types and constexpr Support</title> | |
5 | <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision"> | |
8 | <link rel="up" href="../tut.html" title="Tutorial"> | |
9 | <link rel="prev" href="primetest.html" title="Primality Testing"> | |
10 | <link rel="next" href="import_export.html" title="Importing and Exporting Data to and from cpp_int and cpp_bin_float"> | |
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 alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> | |
15 | <td align="center"><a href="../../../../../../index.html">Home</a></td> | |
16 | <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> | |
17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> | |
18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> | |
19 | <td align="center"><a href="../../../../../../more/index.htm">More</a></td> | |
20 | </tr></table> | |
21 | <hr> | |
22 | <div class="spirit-nav"> | |
23 | <a accesskey="p" href="primetest.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="import_export.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h3 class="title"> | |
27 | <a name="boost_multiprecision.tut.lits"></a><a class="link" href="lits.html" title="Literal Types and constexpr Support">Literal Types and <code class="computeroutput"><span class="keyword">constexpr</span></code> Support</a> | |
28 | </h3></div></div></div> | |
29 | <div class="note"><table border="0" summary="Note"> | |
30 | <tr> | |
31 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> | |
32 | <th align="left">Note</th> | |
33 | </tr> | |
34 | <tr><td align="left" valign="top"><p> | |
35 | The features described in this section make heavy use of C++11 language | |
36 | features, currently (as of May 2013) only GCC-4.7 and later, and Clang | |
37 | 3.3 and later have the support required to make these features work. | |
38 | </p></td></tr> | |
39 | </table></div> | |
40 | <p> | |
41 | There is limited support for <code class="computeroutput"><span class="keyword">constexpr</span></code> | |
42 | and user-defined literals in the library, currently the <code class="computeroutput"><span class="identifier">number</span></code> | |
43 | front end supports <code class="computeroutput"><span class="keyword">constexpr</span></code> | |
44 | on default construction and all forwarding constructors, but not on any of | |
45 | the non-member operators. So if some type <code class="computeroutput"><span class="identifier">B</span></code> | |
46 | is a literal type, then <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">B</span><span class="special">></span></code> | |
47 | is also a literal type, and you will be able to compile-time-construct such | |
48 | a type from any literal that <code class="computeroutput"><span class="identifier">B</span></code> | |
49 | is compile-time-constructible from. However, you will not be able to perform | |
50 | compile-time arithmetic on such types. | |
51 | </p> | |
52 | <p> | |
53 | Currently the only backend type provided by the library that is also a literal | |
54 | type are instantiations of <code class="computeroutput"><span class="identifier">cpp_int_backend</span></code> | |
55 | where the Allocator parameter is type <code class="computeroutput"><span class="keyword">void</span></code>, | |
56 | and the Checked parameter is <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">unchecked</span></code>. | |
57 | </p> | |
58 | <p> | |
59 | For example: | |
60 | </p> | |
61 | <pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> | |
62 | ||
63 | <span class="keyword">constexpr</span> <span class="identifier">int128_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="comment">// OK, fixed precision int128_t has no allocator.</span> | |
64 | <span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xFFFFFFFF00000000uLL</span><span class="special">;</span> <span class="comment">// OK, fixed precision uint1024_t has no allocator.</span> | |
65 | ||
66 | <span class="keyword">constexpr</span> <span class="identifier">checked_uint128_t</span> <span class="identifier">k</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">;</span> <span class="comment">// Error, checked type is not a literal type as we need runtime error checking.</span> | |
67 | <span class="keyword">constexpr</span> <span class="identifier">cpp_int</span> <span class="identifier">l</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> <span class="comment">// Error, type is not a literal as it performs memory management.</span> | |
68 | </pre> | |
69 | <p> | |
70 | There is also limited support for user defined-literals - these are limited | |
71 | to unchecked, fixed precision <code class="computeroutput"><span class="identifier">cpp_int</span></code>'s | |
72 | which are specified in hexadecimal notation. The suffixes supported are: | |
73 | </p> | |
74 | <div class="informaltable"><table class="table"> | |
75 | <colgroup> | |
76 | <col> | |
77 | <col> | |
78 | </colgroup> | |
79 | <thead><tr> | |
80 | <th> | |
81 | <p> | |
82 | Suffix | |
83 | </p> | |
84 | </th> | |
85 | <th> | |
86 | <p> | |
87 | Meaning | |
88 | </p> | |
89 | </th> | |
90 | </tr></thead> | |
91 | <tbody> | |
92 | <tr> | |
93 | <td> | |
94 | <p> | |
95 | _cppi | |
96 | </p> | |
97 | </td> | |
98 | <td> | |
99 | <p> | |
100 | Specifies a value of type: <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_int_backend</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">></span> <span class="special">></span></code>, | |
101 | where N is chosen to contain just enough digits to hold the number | |
102 | specified. | |
103 | </p> | |
104 | </td> | |
105 | </tr> | |
106 | <tr> | |
107 | <td> | |
108 | <p> | |
109 | _cppui | |
110 | </p> | |
111 | </td> | |
112 | <td> | |
113 | <p> | |
114 | Specifies a value of type: <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_int_backend</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">unsigned_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">></span> <span class="special">></span></code>, | |
115 | where N is chosen to contain just enough digits to hold the number | |
116 | specified. | |
117 | </p> | |
118 | </td> | |
119 | </tr> | |
120 | <tr> | |
121 | <td> | |
122 | <p> | |
123 | _cppi<span class="emphasis"><em>N</em></span> | |
124 | </p> | |
125 | </td> | |
126 | <td> | |
127 | <p> | |
128 | Specifies a value of type <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_int_backend</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">></span> <span class="special">></span></code>. | |
129 | </p> | |
130 | </td> | |
131 | </tr> | |
132 | <tr> | |
133 | <td> | |
134 | <p> | |
135 | _cppui<span class="emphasis"><em>N</em></span> | |
136 | </p> | |
137 | </td> | |
138 | <td> | |
139 | <p> | |
140 | Specifies a value of type <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_int_backend</span><span class="special"><</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">N</span><span class="special">,</span><span class="identifier">signed_magnitude</span><span class="special">,</span><span class="identifier">unchecked</span><span class="special">,</span><span class="keyword">void</span><span class="special">></span> <span class="special">></span></code>. | |
141 | </p> | |
142 | </td> | |
143 | </tr> | |
144 | </tbody> | |
145 | </table></div> | |
146 | <p> | |
147 | In each case, use of these suffixes with hexadecimal values produces a <code class="computeroutput"><span class="keyword">constexpr</span></code> result. | |
148 | </p> | |
149 | <p> | |
150 | Examples: | |
151 | </p> | |
152 | <pre class="programlisting"><span class="comment">//</span> | |
153 | <span class="comment">// Any use of user defined literals requires that we import the literal-operators</span> | |
154 | <span class="comment">// into current scope first:</span> | |
155 | <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">literals</span><span class="special">;</span> | |
156 | <span class="comment">//</span> | |
157 | <span class="comment">// To keep things simple in the example, we'll make our types used visible to this scope as well:</span> | |
158 | <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> | |
159 | <span class="comment">//</span> | |
160 | <span class="comment">// The value zero as a number<cpp_int_backend<4,4,signed_magnitude,unchecked,void> >:</span> | |
161 | <span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">0x0</span><span class="identifier">_cppi</span><span class="special">;</span> | |
162 | <span class="comment">// The type of each constant has 4 bits per hexadecimal digit,</span> | |
163 | <span class="comment">// so this is of type uint256_t (ie number<cpp_int_backend<256,256,unsigned_magnitude,unchecked,void> >):</span> | |
164 | <span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui</span><span class="special">;</span> | |
165 | <span class="comment">//</span> | |
166 | <span class="comment">// Smaller values can be assigned to larger values:</span> | |
167 | <span class="identifier">int256_t</span> <span class="identifier">c</span> <span class="special">=</span> <span class="number">0x1234</span><span class="identifier">_cppi</span><span class="special">;</span> <span class="comment">// OK</span> | |
168 | <span class="comment">//</span> | |
169 | <span class="comment">// However, this does not currently work in constexpr contexts:</span> | |
170 | <span class="keyword">constexpr</span> <span class="identifier">int256_t</span> <span class="identifier">d</span> <span class="special">=</span> <span class="number">0x1</span><span class="identifier">_cppi</span><span class="special">;</span> <span class="comment">// Compiler error</span> | |
171 | <span class="comment">//</span> | |
172 | <span class="comment">// Constants can be padded out with leading zeros to generate wider types:</span> | |
173 | <span class="keyword">constexpr</span> <span class="identifier">uint256_t</span> <span class="identifier">e</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">x0000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFF_cppui</span><span class="special">;</span> <span class="comment">// OK</span> | |
174 | <span class="comment">//</span> | |
175 | <span class="comment">// However, specific width types are best produced with specific-width suffixes,</span> | |
176 | <span class="comment">// ones supported by default are `_cpp[u]i128`, `_cpp[u]i256`, `_cpp[u]i512`, `_cpp[u]i1024`.</span> | |
177 | <span class="comment">//</span> | |
178 | <span class="keyword">constexpr</span> <span class="identifier">int128_t</span> <span class="identifier">f</span> <span class="special">=</span> <span class="number">0x1234</span><span class="identifier">_cppi128</span><span class="special">;</span> <span class="comment">// OK, always produces an int128_t as the result.</span> | |
179 | <span class="keyword">constexpr</span> <span class="identifier">uint1024_t</span> <span class="identifier">g</span> <span class="special">=</span> <span class="number">0</span><span class="identifier">xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccc_cppui1024</span><span class="special">;</span> | |
180 | <span class="comment">//</span> | |
181 | <span class="comment">// If other specific width types are required, then there is a macro for generating the operators</span> | |
182 | <span class="comment">// for these. The macro can be used at namespace scope only:</span> | |
183 | <span class="comment">//</span> | |
184 | <span class="identifier">BOOST_MP_DEFINE_SIZED_CPP_INT_LITERAL</span><span class="special">(</span><span class="number">2048</span><span class="special">);</span> | |
185 | <span class="comment">//</span> | |
186 | <span class="comment">// Now we can create 2048-bit literals as well:</span> | |
187 | <span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="identifier">h</span> <span class="special">=</span> <span class="number">0xff</span><span class="identifier">_cppi2048</span><span class="special">;</span> <span class="comment">// h is of type number<cpp_int_backend<2048,2048,signed_magnitude,unchecked,void> ></span> | |
188 | <span class="comment">//</span> | |
189 | <span class="comment">// Finally negative values are handled via the unary minus operator:</span> | |
190 | <span class="comment">//</span> | |
191 | <span class="keyword">constexpr</span> <span class="identifier">int1024_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="special">-</span><span class="number">0</span><span class="identifier">xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui1024</span><span class="special">;</span> | |
192 | <span class="comment">//</span> | |
193 | <span class="comment">// Which means this also works:</span> | |
194 | <span class="keyword">constexpr</span> <span class="identifier">int1024_t</span> <span class="identifier">j</span> <span class="special">=</span> <span class="special">-</span><span class="identifier">g</span><span class="special">;</span> <span class="comment">// OK: unary minus operator is constexpr.</span> | |
195 | </pre> | |
196 | </div> | |
197 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
198 | <td align="left"></td> | |
199 | <td align="right"><div class="copyright-footer">Copyright © 2002-2013 John Maddock and Christopher Kormanyos<p> | |
200 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
201 | file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) | |
202 | </p> | |
203 | </div></td> | |
204 | </tr></table> | |
205 | <hr> | |
206 | <div class="spirit-nav"> | |
207 | <a accesskey="p" href="primetest.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="import_export.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
208 | </div> | |
209 | </body> | |
210 | </html> |