Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/integer/integer_traits.html @ 47

Last change on this file since 47 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.0 KB
Line 
1<html>
2
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5
6<title>integer_traits: Compile-Time Limits for Integral Types</title>
7</head>
8
9<body bgcolor="#FFFFFF" text="#000000">
10
11<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" width="277" height="86">Compile-Time Integral
12Type Limits</h1>
13
14<p>
15The C++ Standard Library &lt;limits&gt; header supplies a class template
16numeric_limits&lt;&gt; with specializations for each fundamental
17type.</p>
18<p>
19For integer types, the interesting members of std::numeric_limits&lt;&gt; are:
20<pre>   static const bool is_specialized; // will be true for integers
21   static T min() throw();
22   static T max() throw();
23   static const int digits;     // for integers, # value bits
24   static const int digits10;     
25   static const bool is_signed;
26   static const bool is_integer; // will be true for integers</pre>
27For many uses, these are sufficient.  But min() and max() are problematical because they are not constant expressions
28(std::5.19), yet some usages require constant expressions.
29<p>
30The template class <code>integer_traits</code> addresses this
31problem.
32
33
34<h2>Header <code><a href="../../boost/integer_traits.hpp">integer_traits.hpp</a></code> Synopsis</h2>
35
36<pre>namespace boost {
37  template&lt;class T&gt;
38  class integer_traits : public std::numeric_limits&lt;T&gt;
39  {
40    static const bool is_integral = false;
41  };
42
43  // specializations for all integral types
44}</pre>
45
46
47<h2>Description</h2>
48
49Template class <code>integer_traits</code> is derived from
50<code>std::numeric_limits</code>.  In general, it adds the single
51<code>bool</code> member <code>is_integral</code> with the
52compile-time constant value <code>false</code>.  However, for all
53integral types <code>T</code> (std::3.9.1/7 [basic.fundamental]),
54there are specializations provided with the following compile-time
55constants defined:
56<p>
57<table border=1>
58<tr><th>member</th><th>type</th><th>value</th></tr>
59<tr><td><code>is_integral</code></td><td>bool</td><td><code>true</code></td></tr>
60<tr><td><code>const_min</code></td><td><code>T</code></td><td>equivalent
61to <code>std::numeric_limits&lt;T&gt;::min()</code></td></tr>
62<tr><td><code>const_max</code></td><td><code>T</code></td><td>equivalent
63to <code>std::numeric_limits&lt;T&gt;::max()</code></td></tr>
64</table>
65
66<p>
67
68<em>Note:</em> A flag <code>is_integral</code> is provided, because a
69user-defined integer class should specialize
70<code>std::numeric_limits&lt;&gt;::is_integer = true</code>,
71nonetheless compile-time constants <code>const_min</code> and
72<code>const_max</code> cannot be provided for that user-defined class.
73
74<h2>
75
76Test Program</h2>
77
78<p>
79
80The program <code><a href="integer_traits_test.cpp">integer_traits_test.cpp</a></code>
81exercises the <code>integer_traits</code> class.
82
83<h2>Acknowledgements</h2>
84
85Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer
86traits idea on the boost mailing list in August 1999.
87<hr>
88<a href="../../people/jens_maurer.htm">
89Jens Maurer</a>, 2000-02-20
Note: See TracBrowser for help on using the repository browser.