Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > by-pkgid > ef9bad9e14fc2a68cb7c992c11d75f5e > files > 2889

libboost1-devel-1.31.0-1mdk.i586.rpm

<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="../../c++boost.gif" alt="c++boost.gif (8819 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>
<a href="../../people/jens_maurer.htm">
Jens Maurer</a>, 2000-02-20