Files
integer/integer_traits.html

95 lines
3.3 KiB
HTML
Raw Normal View History

2000-07-27 14:04:40 +00:00
<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
2000-07-27 14:04:40 +00:00
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>
Merged revisions 40815-40850 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r40816 | nasonov | 2007-11-05 14:22:48 -0800 (Mon, 05 Nov 2007) | 1 line #839 fixed: local variable shadow patch ........ r40817 | danmarsden | 2007-11-05 14:24:53 -0800 (Mon, 05 Nov 2007) | 1 line fixed spelling mistake in documentation ........ r40818 | noel_belcourt | 2007-11-05 14:39:49 -0800 (Mon, 05 Nov 2007) | 3 lines Commit patch submitted by John Maddock. ........ r40819 | grafik | 2007-11-05 14:48:41 -0800 (Mon, 05 Nov 2007) | 1 line Add missing action for --skip-tests option since it's a boolean flag. ........ r40820 | grafik | 2007-11-05 14:49:09 -0800 (Mon, 05 Nov 2007) | 1 line Oops, forgot a comma. ........ r40822 | djowel | 2007-11-05 17:44:49 -0800 (Mon, 05 Nov 2007) | 1 line fix for real number parsers with custom types that do not have a std::numeric_limits specialization. ........ r40824 | bemandawes | 2007-11-05 18:29:50 -0800 (Mon, 05 Nov 2007) | 1 line Make message test conditional for Windows, Linux, HP-UX, OSF, and VMS (Boris Gubenko) ........ r40825 | bemandawes | 2007-11-05 18:54:29 -0800 (Mon, 05 Nov 2007) | 1 line Remove prematurely committed portion of prior change ........ r40827 | djowel | 2007-11-06 02:09:38 -0800 (Tue, 06 Nov 2007) | 1 line doc updates to reflect structure changes ........ r40828 | djowel | 2007-11-06 03:40:27 -0800 (Tue, 06 Nov 2007) | 1 line Regenerating/reorganizing docs ........ r40829 | schoepflin | 2007-11-06 03:52:01 -0800 (Tue, 06 Nov 2007) | 2 lines Again removed markup which has been wrongly reintroduced by @40735. ........ r40830 | djowel | 2007-11-06 03:58:37 -0800 (Tue, 06 Nov 2007) | 1 line doc updates + some more header tweaks ........ r40831 | djowel | 2007-11-06 04:05:12 -0800 (Tue, 06 Nov 2007) | 1 line doc updates + some more header tweaks ........ r40832 | djowel | 2007-11-06 04:13:52 -0800 (Tue, 06 Nov 2007) | 1 line Regenerating/reorganizing docs ........ r40833 | djowel | 2007-11-06 04:17:26 -0800 (Tue, 06 Nov 2007) | 1 line todo update ........ r40834 | bemandawes | 2007-11-06 05:24:38 -0800 (Tue, 06 Nov 2007) | 1 line Add missing Boost.System entry ........ r40835 | bemandawes | 2007-11-06 05:41:19 -0800 (Tue, 06 Nov 2007) | 1 line Add missing copyright and license ........ r40836 | aaron_windsor | 2007-11-06 05:55:05 -0800 (Tue, 06 Nov 2007) | 1 line Renaming planar graph test files to under 32 characters each. ........ r40837 | bemandawes | 2007-11-06 06:22:00 -0800 (Tue, 06 Nov 2007) | 1 line Add license ........ r40838 | joaquin | 2007-11-06 06:33:59 -0800 (Tue, 06 Nov 2007) | 1 line updated according to latest regression tests results ........ r40840 | bemandawes | 2007-11-06 08:10:11 -0800 (Tue, 06 Nov 2007) | 1 line Add repository URL and revision number, plus other minor tweaks and fixes. ........ r40841 | igaztanaga | 2007-11-06 08:56:28 -0800 (Tue, 06 Nov 2007) | 1 line Corrected _CRT_SECURE_NO_DEPRECATE detection ........ r40842 | igaztanaga | 2007-11-06 08:56:48 -0800 (Tue, 06 Nov 2007) | 1 line Initialized native handle to avoid warnings in Visual2005 ........ r40843 | johnmaddock | 2007-11-06 08:58:46 -0800 (Tue, 06 Nov 2007) | 1 line Disabled static assertions for compilers that don't support them. ........ r40844 | johnmaddock | 2007-11-06 09:02:26 -0800 (Tue, 06 Nov 2007) | 1 line Still trying to get the Solaris error rates correct. ........ r40845 | johnmaddock | 2007-11-06 09:09:33 -0800 (Tue, 06 Nov 2007) | 1 line Still trying to get those Solaris error rates correct. ........ r40846 | anthonyw | 2007-11-06 09:15:50 -0800 (Tue, 06 Nov 2007) | 1 line use condition so we know when threads have unblocked, to avoid hard-coding a delay ........ r40847 | johnmaddock | 2007-11-06 09:21:28 -0800 (Tue, 06 Nov 2007) | 1 line Fix for Mac OS error rates. ........ r40848 | johnmaddock | 2007-11-06 10:32:57 -0800 (Tue, 06 Nov 2007) | 1 line Updates for new toolset Sun-5.9. ........ r40850 | hljin | 2007-11-06 11:15:03 -0800 (Tue, 06 Nov 2007) | 1 line GIL: removed an extra semi-colon and made some cosmetic changes ........ [SVN r40851]
2007-11-06 20:13:21 +00:00
<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>