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.75.2">
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="cstdint.html" title="Standard Integer Types">
10 <link rel="next" href="integer.html" title="Integer Type Selection">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="cstdint.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>
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>
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>
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>
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.
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:
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.
49 </span><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.
50 </span><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.
51 </span><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.
52 </span><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.
53 </span><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.
54 </span><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.
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
62 The template class <code class="literal">integer_traits</code> addresses this problem.
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">//
76 </span> <span class="comment">// These members are defined only if T is a built-in
77 </span> <span class="comment">// integal type:
78 </span> <span class="comment">//
79 </span> <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>
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>
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
97 <div class="informaltable"><table class="table">
124 <code class="literal">is_integral</code>
134 <code class="literal">true</code>
141 <code class="literal">const_min</code>
146 <code class="literal">T</code>
151 equivalent to <code class="literal">std::numeric_limits<T>::min()</code>
158 <code class="literal">const_max</code>
163 <code class="literal">T</code>
168 equivalent to <code class="literal">std::numeric_limits<T>::max()</code>
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.
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>
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.
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>
196 Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer
197 traits idea on the boost mailing list in August 1999.
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 Dawes, Daryle Walker, Gennaro Prota,
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>)
211 <div class="spirit-nav">
212 <a accesskey="p" href="cstdint.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>