integer.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Integer Type Selection</title>
  5. <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Boost.Integer">
  8. <link rel="up" href="../index.html" title="Boost.Integer">
  9. <link rel="prev" href="traits.html" title="Integer Traits">
  10. <link rel="next" href="gcd_lcm.html" title="Greatest Common Divisor and Least Common Multiple">
  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="traits.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="gcd_lcm.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="boost_integer.integer"></a><a class="link" href="integer.html" title="Integer Type Selection">Integer Type Selection</a>
  28. </h2></div></div></div>
  29. <div class="toc"><dl class="toc">
  30. <dt><span class="section"><a href="integer.html#boost_integer.integer.synopsis">Synopsis</a></span></dt>
  31. <dt><span class="section"><a href="integer.html#boost_integer.integer.easiest">Easiest-to-Manipulate
  32. Types</a></span></dt>
  33. <dt><span class="section"><a href="integer.html#boost_integer.integer.sized">Sized Types</a></span></dt>
  34. <dt><span class="section"><a href="integer.html#boost_integer.integer.example">Example</a></span></dt>
  35. <dt><span class="section"><a href="integer.html#boost_integer.integer.demonstration_program">Demonstration
  36. Program</a></span></dt>
  37. <dt><span class="section"><a href="integer.html#boost_integer.integer.rationale">Rationale</a></span></dt>
  38. <dt><span class="section"><a href="integer.html#boost_integer.integer.alternative">Alternative</a></span></dt>
  39. <dt><span class="section"><a href="integer.html#boost_integer.integer.credits">Credits</a></span></dt>
  40. </dl></div>
  41. <p>
  42. The <a href="../../../../../boost/integer.hpp" target="_top">&lt;boost/integer.hpp&gt;</a>
  43. type selection templates allow integer types to be selected based on desired
  44. characteristics such as number of bits or maximum value. This facility is particularly
  45. useful for solving generic programming problems.
  46. </p>
  47. <div class="section">
  48. <div class="titlepage"><div><div><h3 class="title">
  49. <a name="boost_integer.integer.synopsis"></a><a class="link" href="integer.html#boost_integer.integer.synopsis" title="Synopsis">Synopsis</a>
  50. </h3></div></div></div>
  51. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
  52. <span class="special">{</span>
  53. <span class="comment">// fast integers from least integers</span>
  54. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">LeastInt</span><span class="special">&gt;</span>
  55. <span class="keyword">struct</span> <span class="identifier">int_fast_t</span>
  56. <span class="special">{</span>
  57. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">type</span><span class="special">;</span>
  58. <span class="special">};</span>
  59. <span class="comment">// signed</span>
  60. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span>
  61. <span class="keyword">struct</span> <span class="identifier">int_t</span>
  62. <span class="special">{</span>
  63. <span class="comment">/* Member exact may or may not be defined depending upon Bits */</span>
  64. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">exact</span><span class="special">;</span>
  65. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
  66. <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
  67. <span class="special">};</span>
  68. <span class="comment">// unsigned</span>
  69. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="identifier">Bits</span><span class="special">&gt;</span>
  70. <span class="keyword">struct</span> <span class="identifier">uint_t</span>
  71. <span class="special">{</span>
  72. <span class="comment">/* Member exact may or may not be defined depending upon Bits */</span>
  73. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">exact</span><span class="special">;</span>
  74. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
  75. <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
  76. <span class="special">};</span>
  77. <span class="comment">// signed</span>
  78. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">long</span> <span class="keyword">long</span> <span class="identifier">MaxValue</span><span class="special">&gt;</span>
  79. <span class="keyword">struct</span> <span class="identifier">int_max_value_t</span>
  80. <span class="special">{</span>
  81. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
  82. <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
  83. <span class="special">};</span>
  84. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">long</span> <span class="keyword">long</span> <span class="identifier">MinValue</span><span class="special">&gt;</span>
  85. <span class="keyword">struct</span> <span class="identifier">int_min_value_t</span>
  86. <span class="special">{</span>
  87. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
  88. <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
  89. <span class="special">};</span>
  90. <span class="comment">// unsigned</span>
  91. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="identifier">Value</span><span class="special">&gt;</span>
  92. <span class="keyword">struct</span> <span class="identifier">uint_value_t</span>
  93. <span class="special">{</span>
  94. <span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined-type</em></span> <span class="identifier">least</span><span class="special">;</span>
  95. <span class="keyword">typedef</span> <span class="identifier">int_fast_t</span><span class="special">&lt;</span><span class="identifier">least</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">fast</span><span class="special">;</span>
  96. <span class="special">};</span>
  97. <span class="special">}</span> <span class="comment">// namespace boost</span>
  98. </pre>
  99. </div>
  100. <div class="section">
  101. <div class="titlepage"><div><div><h3 class="title">
  102. <a name="boost_integer.integer.easiest"></a><a class="link" href="integer.html#boost_integer.integer.easiest" title="Easiest-to-Manipulate Types">Easiest-to-Manipulate
  103. Types</a>
  104. </h3></div></div></div>
  105. <p>
  106. The <code class="literal">int_fast_t</code> class template maps its input type to the
  107. next-largest type that the processor can manipulate the easiest, or to itself
  108. if the input type is already an easy-to-manipulate type. For instance, processing
  109. a bunch of <code class="literal">char</code> objects may go faster if they were converted
  110. to <code class="literal">int</code> objects before processing. The input type, passed
  111. as the only template parameter, must be a built-in integral type, except
  112. <code class="literal">bool</code>. Unsigned integral types can be used, as well as
  113. signed integral types. The output type is given as the nested type <code class="literal">fast</code>.
  114. </p>
  115. <p>
  116. <span class="bold"><strong>Implementation Notes:</strong></span> By default, the output
  117. type is identical to the input type. Eventually, this code's implementation
  118. should be customized for each platform to give accurate mappings between
  119. the built-in types and the easiest-to-manipulate built-in types. Also, there
  120. is no guarantee that the output type actually is easier to manipulate than
  121. the input type.
  122. </p>
  123. </div>
  124. <div class="section">
  125. <div class="titlepage"><div><div><h3 class="title">
  126. <a name="boost_integer.integer.sized"></a><a class="link" href="integer.html#boost_integer.integer.sized" title="Sized Types">Sized Types</a>
  127. </h3></div></div></div>
  128. <p>
  129. The <code class="literal">int_t</code>, <code class="literal">uint_t</code>, <code class="literal">int_max_value_t</code>,
  130. <code class="literal">int_min_value_t</code>, and <code class="literal">uint_value_t</code> class
  131. templates find the most appropiate built-in integral type for the given template
  132. parameter. This type is given by the nested type <code class="literal">least</code>.
  133. The easiest-to-manipulate version of that type is given by the nested type
  134. <code class="literal">fast</code>. The following table describes each template's criteria.
  135. </p>
  136. <div class="table">
  137. <a name="boost_integer.integer.sized.criteria_for_the_sized_type_class_templates"></a><p class="title"><b>Table&#160;1.&#160;Criteria for the Sized Type Class Templates</b></p>
  138. <div class="table-contents"><table class="table" summary="Criteria for the Sized Type Class Templates">
  139. <colgroup>
  140. <col>
  141. <col>
  142. </colgroup>
  143. <thead><tr>
  144. <th>
  145. <p>
  146. Class Template
  147. </p>
  148. </th>
  149. <th>
  150. <p>
  151. Template Parameter Mapping
  152. </p>
  153. </th>
  154. </tr></thead>
  155. <tbody>
  156. <tr>
  157. <td>
  158. <p>
  159. <code class="literal">boost::int_t&lt;N&gt;::least</code>
  160. </p>
  161. </td>
  162. <td>
  163. <p>
  164. The smallest, built-in, signed integral type with at least <span class="emphasis"><em>N</em></span>
  165. bits, including the sign bit. The parameter should be a positive
  166. number. A compile-time error results if the parameter is larger
  167. than the number of bits in the largest integer type.
  168. </p>
  169. </td>
  170. </tr>
  171. <tr>
  172. <td>
  173. <p>
  174. <code class="literal">boost::int_t&lt;N&gt;::fast</code>
  175. </p>
  176. </td>
  177. <td>
  178. <p>
  179. The easiest-to-manipulate, built-in, signed integral type with
  180. at least <span class="emphasis"><em>N</em></span> bits, including the sign bit. The
  181. parameter should be a positive number. A compile-time error results
  182. if the parameter is larger than the number of bits in the largest
  183. integer type.
  184. </p>
  185. </td>
  186. </tr>
  187. <tr>
  188. <td>
  189. <p>
  190. <code class="literal">boost::int_t&lt;N&gt;::exact</code>
  191. </p>
  192. </td>
  193. <td>
  194. <p>
  195. A built-in, signed integral type with exactly <span class="emphasis"><em>N</em></span>
  196. bits, including the sign bit. The parameter should be a positive
  197. number. Note that the member <span class="emphasis"><em>exact</em></span> is defined
  198. <span class="bold"><strong>only</strong></span> if there exists a type with
  199. exactly <span class="emphasis"><em>N</em></span> bits.
  200. </p>
  201. </td>
  202. </tr>
  203. <tr>
  204. <td>
  205. <p>
  206. <code class="literal">boost::uint_t&lt;N&gt;::least</code>
  207. </p>
  208. </td>
  209. <td>
  210. <p>
  211. The smallest, built-in, unsigned integral type with at least <span class="emphasis"><em>N</em></span>
  212. bits. The parameter should be a positive number. A compile-time
  213. error results if the parameter is larger than the number of bits
  214. in the largest integer type.
  215. </p>
  216. </td>
  217. </tr>
  218. <tr>
  219. <td>
  220. <p>
  221. <code class="literal">boost::uint_t&lt;N&gt;::fast</code>
  222. </p>
  223. </td>
  224. <td>
  225. <p>
  226. The easiest-to-manipulate, built-in, unsigned integral type with
  227. at least <span class="emphasis"><em>N</em></span> bits. The parameter should be a
  228. positive number. A compile-time error results if the parameter
  229. is larger than the number of bits in the largest integer type.
  230. </p>
  231. </td>
  232. </tr>
  233. <tr>
  234. <td>
  235. <p>
  236. <code class="literal">boost::uint_t&lt;N&gt;::exact</code>
  237. </p>
  238. </td>
  239. <td>
  240. <p>
  241. A built-in, unsigned integral type with exactly <span class="emphasis"><em>N</em></span>
  242. bits. The parameter should be a positive number. A compile-time
  243. error results if the parameter is larger than the number of bits
  244. in the largest integer type. Note that the member <span class="emphasis"><em>exact</em></span>
  245. is defined <span class="bold"><strong>only</strong></span> if there exists
  246. a type with exactly N bits.
  247. </p>
  248. </td>
  249. </tr>
  250. <tr>
  251. <td>
  252. <p>
  253. <code class="literal">boost::int_max_value_t&lt;V&gt;::last</code>
  254. </p>
  255. </td>
  256. <td>
  257. <p>
  258. The smallest, built-in, signed integral type that can hold all
  259. the values in the inclusive range <span class="emphasis"><em>0 - V</em></span>. The
  260. parameter should be a positive number.
  261. </p>
  262. </td>
  263. </tr>
  264. <tr>
  265. <td>
  266. <p>
  267. <code class="literal">boost::int_max_value_t&lt;V&gt;::fast</code>
  268. </p>
  269. </td>
  270. <td>
  271. <p>
  272. The easiest-to-manipulate, built-in, signed integral type that
  273. can hold all the values in the inclusive range <span class="emphasis"><em>0 - V</em></span>.
  274. The parameter should be a positive number.
  275. </p>
  276. </td>
  277. </tr>
  278. <tr>
  279. <td>
  280. <p>
  281. <code class="literal">boost::int_min_value_t&lt;V&gt;::least</code>
  282. </p>
  283. </td>
  284. <td>
  285. <p>
  286. The smallest, built-in, signed integral type that can hold all
  287. the values in the inclusive range <span class="emphasis"><em>V - 0</em></span>. The
  288. parameter should be a negative number.
  289. </p>
  290. </td>
  291. </tr>
  292. <tr>
  293. <td>
  294. <p>
  295. <code class="literal">boost::int_min_value_t&lt;V&gt;::fast</code>
  296. </p>
  297. </td>
  298. <td>
  299. <p>
  300. The easiest-to-manipulate, built-in, signed integral type that
  301. can hold all the values in the inclusive range <span class="emphasis"><em>V - 0</em></span>.
  302. The parameter should be a negative number.
  303. </p>
  304. </td>
  305. </tr>
  306. <tr>
  307. <td>
  308. <p>
  309. <code class="literal">boost::uint_value_t&lt;V&gt;::least</code>
  310. </p>
  311. </td>
  312. <td>
  313. <p>
  314. The smallest, built-in, unsigned integral type that can hold all
  315. positive values up to and including <span class="emphasis"><em>V</em></span>. The
  316. parameter should be a positive number.
  317. </p>
  318. </td>
  319. </tr>
  320. <tr>
  321. <td>
  322. <p>
  323. <code class="literal">boost::uint_value_t&lt;V&gt;::fast</code>
  324. </p>
  325. </td>
  326. <td>
  327. <p>
  328. The easiest-to-manipulate, built-in, unsigned integral type that
  329. can hold all positive values up to and including <span class="emphasis"><em>V</em></span>.
  330. The parameter should be a positive number.
  331. </p>
  332. </td>
  333. </tr>
  334. </tbody>
  335. </table></div>
  336. </div>
  337. <br class="table-break">
  338. </div>
  339. <div class="section">
  340. <div class="titlepage"><div><div><h3 class="title">
  341. <a name="boost_integer.integer.example"></a><a class="link" href="integer.html#boost_integer.integer.example" title="Example">Example</a>
  342. </h3></div></div></div>
  343. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  344. <span class="comment">//...</span>
  345. <span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
  346. <span class="special">{</span>
  347. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int_t</span><span class="special">&lt;</span><span class="number">24</span><span class="special">&gt;::</span><span class="identifier">least</span> <span class="identifier">my_var</span><span class="special">;</span> <span class="comment">// my_var has at least 24-bits</span>
  348. <span class="comment">//...</span>
  349. <span class="comment">// This one is guaranteed not to be truncated:</span>
  350. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int_max_value_t</span><span class="special">&lt;</span><span class="number">1000</span><span class="special">&gt;::</span><span class="identifier">least</span> <span class="identifier">my1000</span> <span class="special">=</span> <span class="number">1000</span><span class="special">;</span>
  351. <span class="comment">//...</span>
  352. <span class="comment">// This one is guaranteed not to be truncated, and as fast</span>
  353. <span class="comment">// to manipulate as possible, its size may be greater than</span>
  354. <span class="comment">// that of my1000:</span>
  355. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">int_max_value_t</span><span class="special">&lt;</span><span class="number">1000</span><span class="special">&gt;::</span><span class="identifier">fast</span> <span class="identifier">my_fast1000</span> <span class="special">=</span> <span class="number">1000</span><span class="special">;</span>
  356. <span class="special">}</span>
  357. </pre>
  358. </div>
  359. <div class="section">
  360. <div class="titlepage"><div><div><h3 class="title">
  361. <a name="boost_integer.integer.demonstration_program"></a><a class="link" href="integer.html#boost_integer.integer.demonstration_program" title="Demonstration Program">Demonstration
  362. Program</a>
  363. </h3></div></div></div>
  364. <p>
  365. The program <a href="../../../test/integer_test.cpp" target="_top">integer_test.cpp</a>
  366. is a simplistic demonstration of the results from instantiating various examples
  367. of the sized type class templates.
  368. </p>
  369. </div>
  370. <div class="section">
  371. <div class="titlepage"><div><div><h3 class="title">
  372. <a name="boost_integer.integer.rationale"></a><a class="link" href="integer.html#boost_integer.integer.rationale" title="Rationale">Rationale</a>
  373. </h3></div></div></div>
  374. <p>
  375. The rationale for the design of the templates in this header includes:
  376. </p>
  377. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  378. <li class="listitem">
  379. Avoid recursion because of concern about C++'s limited guaranteed recursion
  380. depth (17).
  381. </li>
  382. <li class="listitem">
  383. Avoid macros on general principles.
  384. </li>
  385. <li class="listitem">
  386. Try to keep the design as simple as possible.
  387. </li>
  388. </ul></div>
  389. </div>
  390. <div class="section">
  391. <div class="titlepage"><div><div><h3 class="title">
  392. <a name="boost_integer.integer.alternative"></a><a class="link" href="integer.html#boost_integer.integer.alternative" title="Alternative">Alternative</a>
  393. </h3></div></div></div>
  394. <p>
  395. If the number of bits required is known beforehand, it may be more appropriate
  396. to use the types supplied in <a href="../../../../../boost/cstdint.hpp" target="_top">&lt;boost/cstdint.hpp&gt;</a>.
  397. </p>
  398. </div>
  399. <div class="section">
  400. <div class="titlepage"><div><div><h3 class="title">
  401. <a name="boost_integer.integer.credits"></a><a class="link" href="integer.html#boost_integer.integer.credits" title="Credits">Credits</a>
  402. </h3></div></div></div>
  403. <p>
  404. The author of most of the Boost integer type choosing templates is <a href="http://www.boost.org/people/beman_dawes.html" target="_top">Beman Dawes</a>. He
  405. gives thanks to Valentin Bonnard and <a href="http://www.boost.org/people/kevlin_henney.htm" target="_top">Kevlin
  406. Henney</a> for sharing their designs for similar templates. <a href="http://www.boost.org/people/daryle_walker.html" target="_top">Daryle
  407. Walker</a> designed the value-based sized templates.
  408. </p>
  409. </div>
  410. </div>
  411. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  412. <td align="left"></td>
  413. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2009 Beman
  414. Dawes, Daryle Walker, Gennaro Prota, John Maddock<p>
  415. Distributed under the Boost Software License, Version 1.0. (See accompanying
  416. 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>)
  417. </p>
  418. </div></td>
  419. </tr></table>
  420. <hr>
  421. <div class="spirit-nav">
  422. <a accesskey="p" href="traits.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="gcd_lcm.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  423. </div>
  424. </body>
  425. </html>