integer/integer_traits.html
Beman Dawes 0d058f42cc Add missing copyright and license
[SVN r40835]
2007-11-06 13:41:19 +00:00

95 lines
3.3 KiB
HTML
Raw Blame History

<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><EFBFBD> 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>