]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Integer Traits</title> | |
5 | <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.78.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="../index.html" title="Boost.Integer"> | |
10 | <link rel="next" href="integer.html" title="Integer Type Selection"> | |
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="../index.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="integer.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.traits"></a><a class="link" href="traits.html" title="Integer Traits">Integer Traits</a> | |
28 | </h2></div></div></div> | |
29 | <div class="toc"><dl class="toc"> | |
30 | <dt><span class="section"><a href="traits.html#boost_integer.traits.motivation">Motivation</a></span></dt> | |
31 | <dt><span class="section"><a href="traits.html#boost_integer.traits.synopsis">Synopsis</a></span></dt> | |
32 | <dt><span class="section"><a href="traits.html#boost_integer.traits.description">Description</a></span></dt> | |
33 | <dt><span class="section"><a href="traits.html#boost_integer.traits.test_program">Test Program</a></span></dt> | |
34 | <dt><span class="section"><a href="traits.html#boost_integer.traits.acknowledgements">Acknowledgements</a></span></dt> | |
35 | </dl></div> | |
36 | <div class="section"> | |
37 | <div class="titlepage"><div><div><h3 class="title"> | |
38 | <a name="boost_integer.traits.motivation"></a><a class="link" href="traits.html#boost_integer.traits.motivation" title="Motivation">Motivation</a> | |
39 | </h3></div></div></div> | |
40 | <p> | |
41 | The C++ Standard Library <limits> header supplies a class template | |
42 | <code class="computeroutput"><span class="identifier">numeric_limits</span><span class="special"><></span></code> | |
43 | with specializations for each fundamental type. | |
44 | </p> | |
45 | <p> | |
46 | For integer types, the interesting members of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><></span></code> are: | |
47 | </p> | |
48 | <pre class="programlisting"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_specialized</span><span class="special">;</span> <span class="comment">// Will be true for integer types.</span> | |
49 | <span class="keyword">static</span> <span class="identifier">T</span> <span class="identifier">min</span><span class="special">()</span> <span class="keyword">throw</span><span class="special">();</span> <span class="comment">// Smallest representable value.</span> | |
50 | <span class="keyword">static</span> <span class="identifier">T</span> <span class="identifier">max</span><span class="special">()</span> <span class="keyword">throw</span><span class="special">();</span> <span class="comment">// Largest representable value.</span> | |
51 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">digits</span><span class="special">;</span> <span class="comment">// For integers, the number of value bits.</span> | |
52 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">digits10</span><span class="special">;</span> <span class="comment">// The number of base 10 digits that can be represented.</span> | |
53 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_signed</span><span class="special">;</span> <span class="comment">// True if the type is signed.</span> | |
54 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_integer</span><span class="special">;</span> <span class="comment">// Will be true for all integer types.</span> | |
55 | </pre> | |
56 | <p> | |
57 | For many uses, these are sufficient. But min() and max() are problematical | |
58 | because they are not constant expressions (std::5.19), yet some usages require | |
59 | constant expressions. | |
60 | </p> | |
61 | <p> | |
62 | The template class <code class="literal">integer_traits</code> addresses this problem. | |
63 | </p> | |
64 | </div> | |
65 | <div class="section"> | |
66 | <div class="titlepage"><div><div><h3 class="title"> | |
67 | <a name="boost_integer.traits.synopsis"></a><a class="link" href="traits.html#boost_integer.traits.synopsis" title="Synopsis">Synopsis</a> | |
68 | </h3></div></div></div> | |
69 | <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> | |
70 | <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
71 | <span class="keyword">class</span> <span class="identifier">integer_traits</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> | |
72 | <span class="special">{</span> | |
73 | <span class="keyword">public</span><span class="special">:</span> | |
74 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_integral</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> | |
75 | <span class="comment">//</span> | |
76 | <span class="comment">// These members are defined only if T is a built-in</span> | |
77 | <span class="comment">// integal type:</span> | |
78 | <span class="comment">//</span> | |
79 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">T</span> <span class="identifier">const_min</span> <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span> | |
80 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">T</span> <span class="identifier">const_max</span> <span class="special">=</span> <span class="emphasis"><em>implementation-defined</em></span><span class="special">;</span> | |
81 | <span class="special">};</span> | |
82 | <span class="special">}</span> | |
83 | </pre> | |
84 | </div> | |
85 | <div class="section"> | |
86 | <div class="titlepage"><div><div><h3 class="title"> | |
87 | <a name="boost_integer.traits.description"></a><a class="link" href="traits.html#boost_integer.traits.description" title="Description">Description</a> | |
88 | </h3></div></div></div> | |
89 | <p> | |
90 | Template class <code class="literal">integer_traits</code> is derived from <code class="literal">std::numeric_limits</code>. | |
91 | The primary specialization adds the single <code class="literal">bool</code> member | |
92 | <code class="literal">is_integral</code> with the compile-time constant value <code class="literal">false</code>. | |
93 | However, for all integral types <code class="literal">T</code> (std::3.9.1/7 [basic.fundamental]), | |
94 | there are specializations provided with the following compile-time constants | |
95 | defined: | |
96 | </p> | |
97 | <div class="informaltable"><table class="table"> | |
98 | <colgroup> | |
99 | <col> | |
100 | <col> | |
101 | <col> | |
102 | </colgroup> | |
103 | <thead><tr> | |
104 | <th> | |
105 | <p> | |
106 | member | |
107 | </p> | |
108 | </th> | |
109 | <th> | |
110 | <p> | |
111 | type | |
112 | </p> | |
113 | </th> | |
114 | <th> | |
115 | <p> | |
116 | value | |
117 | </p> | |
118 | </th> | |
119 | </tr></thead> | |
120 | <tbody> | |
121 | <tr> | |
122 | <td> | |
123 | <p> | |
124 | <code class="literal">is_integral</code> | |
125 | </p> | |
126 | </td> | |
127 | <td> | |
128 | <p> | |
129 | bool | |
130 | </p> | |
131 | </td> | |
132 | <td> | |
133 | <p> | |
134 | <code class="literal">true</code> | |
135 | </p> | |
136 | </td> | |
137 | </tr> | |
138 | <tr> | |
139 | <td> | |
140 | <p> | |
141 | <code class="literal">const_min</code> | |
142 | </p> | |
143 | </td> | |
144 | <td> | |
145 | <p> | |
146 | <code class="literal">T</code> | |
147 | </p> | |
148 | </td> | |
149 | <td> | |
150 | <p> | |
151 | equivalent to <code class="literal">std::numeric_limits<T>::min()</code> | |
152 | </p> | |
153 | </td> | |
154 | </tr> | |
155 | <tr> | |
156 | <td> | |
157 | <p> | |
158 | <code class="literal">const_max</code> | |
159 | </p> | |
160 | </td> | |
161 | <td> | |
162 | <p> | |
163 | <code class="literal">T</code> | |
164 | </p> | |
165 | </td> | |
166 | <td> | |
167 | <p> | |
168 | equivalent to <code class="literal">std::numeric_limits<T>::max()</code> | |
169 | </p> | |
170 | </td> | |
171 | </tr> | |
172 | </tbody> | |
173 | </table></div> | |
174 | <p> | |
175 | Note: The <span class="emphasis"><em>is_integral</em></span> flag is provided, because a user-defined | |
176 | integer class should specialize <code class="literal">std::numeric_limits<>::is_integer | |
177 | = true</code>, while compile-time constants <code class="literal">const_min</code> | |
178 | and <code class="literal">const_max</code> are not provided for that user-defined class, | |
179 | unless boost::integer_traits is also specialized. | |
180 | </p> | |
181 | </div> | |
182 | <div class="section"> | |
183 | <div class="titlepage"><div><div><h3 class="title"> | |
184 | <a name="boost_integer.traits.test_program"></a><a class="link" href="traits.html#boost_integer.traits.test_program" title="Test Program">Test Program</a> | |
185 | </h3></div></div></div> | |
186 | <p> | |
187 | The program <code class="literal"><a href="../../../test/integer_traits_test.cpp" target="_top">integer_traits_test.cpp</a></code> | |
188 | exercises the <code class="literal">integer_traits</code> class. | |
189 | </p> | |
190 | </div> | |
191 | <div class="section"> | |
192 | <div class="titlepage"><div><div><h3 class="title"> | |
193 | <a name="boost_integer.traits.acknowledgements"></a><a class="link" href="traits.html#boost_integer.traits.acknowledgements" title="Acknowledgements">Acknowledgements</a> | |
194 | </h3></div></div></div> | |
195 | <p> | |
196 | Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer | |
197 | traits idea on the boost mailing list in August 1999. | |
198 | </p> | |
199 | </div> | |
200 | </div> | |
201 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
202 | <td align="left"></td> | |
203 | <td align="right"><div class="copyright-footer">Copyright © 2001-2009 Beman | |
204 | Dawes, Daryle Walker, Gennaro Prota, John Maddock<p> | |
205 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
206 | 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>) | |
207 | </p> | |
208 | </div></td> | |
209 | </tr></table> | |
210 | <hr> | |
211 | <div class="spirit-nav"> | |
212 | <a accesskey="p" href="../index.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="integer.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> | |
213 | </div> | |
214 | </body> | |
215 | </html> |