File: integer_traits.html

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (94 lines) | stat: -rw-r--r-- 3,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>integer_traits: Compile-Time Limits for Integral Types</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" width="277" height="86">Compile-Time Integral
Type Limits</h1>

<p>
The C++ Standard Library &lt;limits&gt; header supplies a class template
numeric_limits&lt;&gt; with specializations for each fundamental
type.</p>
<p>
For integer types, the interesting members of std::numeric_limits&lt;&gt; are:
<pre>   static const bool is_specialized; // will be true for integers
   static T min() throw();
   static T max() throw();
   static const int digits;     // for integers, # value bits
   static const int digits10;     
   static const bool is_signed;
   static const bool is_integer; // will be true for integers</pre>
For many uses, these are sufficient.  But min() and max() are problematical because they are not constant expressions
(std::5.19), yet some usages require constant expressions.
<p>
The template class <code>integer_traits</code> addresses this
problem.


<h2>Header <code><a href="../../boost/integer_traits.hpp">integer_traits.hpp</a></code> Synopsis</h2>

<pre>namespace boost {
  template&lt;class T&gt;
  class integer_traits : public std::numeric_limits&lt;T&gt;
  {
    static const bool is_integral = false;
  };

  // specializations for all integral types
}</pre>


<h2>Description</h2>

Template class <code>integer_traits</code> is derived from
<code>std::numeric_limits</code>.  In general, it adds the single
<code>bool</code> member <code>is_integral</code> with the
compile-time constant value <code>false</code>.  However, for all
integral types <code>T</code> (std::3.9.1/7 [basic.fundamental]),
there are specializations provided with the following compile-time
constants defined:
<p>
<table border=1>
<tr><th>member</th><th>type</th><th>value</th></tr>
<tr><td><code>is_integral</code></td><td>bool</td><td><code>true</code></td></tr>
<tr><td><code>const_min</code></td><td><code>T</code></td><td>equivalent
to <code>std::numeric_limits&lt;T&gt;::min()</code></td></tr>
<tr><td><code>const_max</code></td><td><code>T</code></td><td>equivalent
to <code>std::numeric_limits&lt;T&gt;::max()</code></td></tr>
</table>

<p>

<em>Note:</em> A flag <code>is_integral</code> is provided, because a
user-defined integer class should specialize
<code>std::numeric_limits&lt;&gt;::is_integer = true</code>,
nonetheless compile-time constants <code>const_min</code> and
<code>const_max</code> cannot be provided for that user-defined class.

<h2>

Test Program</h2>

<p>

The program <code><a href="integer_traits_test.cpp">integer_traits_test.cpp</a></code>
exercises the <code>integer_traits</code> class.

<h2>Acknowledgements</h2>

Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer
traits idea on the boost mailing list in August 1999.
<hr>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->06 November 2007<!--webbot bot="Timestamp" endspan i-checksum="40336" --></p>
<p> Copyright Beman Dawes 2000</p>

<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>