[12] | 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 |
---|
| 12 | Type Limits</h1> |
---|
| 13 | |
---|
| 14 | <p> |
---|
| 15 | The C++ Standard Library <limits> header supplies a class template |
---|
| 16 | numeric_limits<> with specializations for each fundamental |
---|
| 17 | type.</p> |
---|
| 18 | <p> |
---|
| 19 | For integer types, the interesting members of std::numeric_limits<> 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> |
---|
| 27 | For 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> |
---|
| 30 | The template class <code>integer_traits</code> addresses this |
---|
| 31 | problem. |
---|
| 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<class T> |
---|
| 38 | class integer_traits : public std::numeric_limits<T> |
---|
| 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 | |
---|
| 49 | Template 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 |
---|
| 52 | compile-time constant value <code>false</code>. However, for all |
---|
| 53 | integral types <code>T</code> (std::3.9.1/7 [basic.fundamental]), |
---|
| 54 | there are specializations provided with the following compile-time |
---|
| 55 | constants 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 |
---|
| 61 | to <code>std::numeric_limits<T>::min()</code></td></tr> |
---|
| 62 | <tr><td><code>const_max</code></td><td><code>T</code></td><td>equivalent |
---|
| 63 | to <code>std::numeric_limits<T>::max()</code></td></tr> |
---|
| 64 | </table> |
---|
| 65 | |
---|
| 66 | <p> |
---|
| 67 | |
---|
| 68 | <em>Note:</em> A flag <code>is_integral</code> is provided, because a |
---|
| 69 | user-defined integer class should specialize |
---|
| 70 | <code>std::numeric_limits<>::is_integer = true</code>, |
---|
| 71 | nonetheless 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 | |
---|
| 76 | Test Program</h2> |
---|
| 77 | |
---|
| 78 | <p> |
---|
| 79 | |
---|
| 80 | The program <code><a href="integer_traits_test.cpp">integer_traits_test.cpp</a></code> |
---|
| 81 | exercises the <code>integer_traits</code> class. |
---|
| 82 | |
---|
| 83 | <h2>Acknowledgements</h2> |
---|
| 84 | |
---|
| 85 | Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer |
---|
| 86 | traits idea on the boost mailing list in August 1999. |
---|
| 87 | <hr> |
---|
| 88 | <a href="../../people/jens_maurer.htm"> |
---|
| 89 | Jens Maurer</a>, 2000-02-20 |
---|