mirror of
https://github.com/boostorg/integer.git
synced 2025-06-26 04:21:47 +02:00
Compare commits
1 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
0101ecf6ef |
79
cstdint.htm
Normal file
79
cstdint.htm
Normal file
@ -0,0 +1,79 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Header boost/cstdint.hpp</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">Header
|
||||
boost/cstdint.hpp </h1>
|
||||
<p>The header <code><a href="../../boost/cstdint.hpp"><boost/cstdint.hpp></a></code>
|
||||
places the contents of the header <code><a href="../../boost/stdint.h"><boost/stdint.h></a></code>
|
||||
in namespace boost. That header consists entirely of typedef's useful for
|
||||
writing portable code that requires certain integer widths.</p>
|
||||
<p>The specifications are based on the ISO/IEC 9899:1999 C Language standard
|
||||
header stdint.h. The 64-bit types required by the C standard are not
|
||||
required in the boost header, and may not be supplied in all implementations,
|
||||
because <code>long long</code> is not [yet] included in the C++ standard.</p>
|
||||
<p>See <a href="cstdint_test.cpp">cstdint_test.cpp</a> for a test program.</p>
|
||||
<h2>Exact-width integer types</h2>
|
||||
<p>The typedef <code>int#_t</code>, with # replaced by the width, designates a
|
||||
signed integer type of exactly # bits; <code>int8_t</code> denotes an 8-bit
|
||||
signed integer type. Similarly, the typedef <code>uint#_t</code>
|
||||
designates and unsigned integer type of exactly # bits.</p>
|
||||
<p>These types are optional. However, if an implementation provides integer
|
||||
types with widths of 8, 16, 32, or 64 bits, it shall define the corresponding
|
||||
typedef names.</p>
|
||||
<h2>Minimum-width integer types</h2>
|
||||
<p>The typedef <code>int_least#_t</code>, with # replaced by the width,
|
||||
designates a signed integer type with a width of at least # bits, such that no
|
||||
signed integer type with lesser size has at least the specified width. Thus, <code>int_least32_t</code>
|
||||
denotes a signed integer type with a width of at least 32 bits. Similarly, the
|
||||
typedef name <code>uint_least#_t</code> designates an unsigned integer type with
|
||||
a width of at least # bits, such that no unsigned integer type with lesser size
|
||||
has at least the specified width.</p>
|
||||
<p>Required minimum-width integer types:</p>
|
||||
<ul>
|
||||
<li><code>int_least8_t</code></li>
|
||||
<li><code>int_least16_t</code></li>
|
||||
<li><code>int_least32_t</code></li>
|
||||
<li><code>uint_least8_t</code></li>
|
||||
<li><code>uint_least16_t</code></li>
|
||||
<li><code>uint_least32_t</code></li>
|
||||
</ul>
|
||||
<p>All other minimum-width integer types are optional.</p>
|
||||
<h2>Fastest minimum-width integer types</h2>
|
||||
<p>The typedef <code>int_fast#_t</code>, with # replaced by the width,
|
||||
designates the fastest signed integer type with a width of at least # bits.
|
||||
Similarly, the typedef name <code>uint_fast#_t</code> designates the fastest
|
||||
unsigned integer type with a width of at least # bits.</p>
|
||||
<p>There is no guarantee that these types are fastest for all purposes. In
|
||||
any case, however, they satisfy the signedness and width requirements.</p>
|
||||
<p>Required fastest minimum-width integer types:</p>
|
||||
<ul>
|
||||
<li><code>int_fast8_t</code></li>
|
||||
<li><code>int_fast16_t</code></li>
|
||||
<li><code>int_fast32_t</code></li>
|
||||
<li><code>uint_fast8_t</code></li>
|
||||
<li><code>uint_fast16_t</code></li>
|
||||
<li><code>uint_fast32_t</code></li>
|
||||
</ul>
|
||||
<p>All other fastest minimum-width integer types are optional.</p>
|
||||
<h2>Greatest-width integer types</h2>
|
||||
<p>The typedef <code>intmax_t </code>designates a signed integer type capable of
|
||||
representing any value of any signed integer type.</p>
|
||||
<p>The typedef <code>uintmax_t</code> designates an unsigned integer type
|
||||
capable of representing any value of any unsigned integer type.</p>
|
||||
<p>These types are required.</p>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan -->29 Jun 2000<!--webbot bot="Timestamp" endspan i-checksum="15060" -->
|
||||
</p>
|
||||
<p> </p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
74
cstdint_test.cpp
Normal file
74
cstdint_test.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
// boost cstdint.hpp test program ------------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes 2000. Permission to copy, use, modify, sell
|
||||
// and distribute this software is granted provided this copyright
|
||||
// notice appears in all copies. This software is provided "as is" without
|
||||
// express or implied warranty, and with no claim as to its suitability for
|
||||
// any purpose.
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 28 Jun 00 Initial version
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#error This test makes no sense with NDEBUG defined
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::int8_t int8 = -127;
|
||||
boost::int_least8_t int_least8 = -127;
|
||||
boost::int_fast8_t int_fast8 = -127;
|
||||
|
||||
boost::uint8_t uint8 = 255;
|
||||
boost::uint_least8_t uint_least8 = 255;
|
||||
boost::uint_fast8_t uint_fast8 = 255;
|
||||
|
||||
boost::int16_t int16 = -32767;
|
||||
boost::int_least16_t int_least16 = -32767;
|
||||
boost::int_fast16_t int_fast16 = -32767;
|
||||
|
||||
boost::uint16_t uint16 = 65535;
|
||||
boost::uint_least16_t uint_least16 = 65535;
|
||||
boost::uint_fast16_t uint_fast16 = 65535;
|
||||
|
||||
boost::int32_t int32 = -2147483647;
|
||||
boost::int_least32_t int_least32 = -2147483647;
|
||||
boost::int_fast32_t int_fast32 = -2147483647;
|
||||
|
||||
boost::uint32_t uint32 = 4294967295;
|
||||
boost::uint_least32_t uint_least32 = 4294967295;
|
||||
boost::uint_fast32_t uint_fast32 = 4294967295;
|
||||
|
||||
boost::intmax_t intmax = -2147483647;
|
||||
boost::uintmax_t uintmax = 4294967295;
|
||||
|
||||
assert( int8 == -127 );
|
||||
assert( int_least8 == -127 );
|
||||
assert( int_fast8 == -127 );
|
||||
assert( uint8 == 255 );
|
||||
assert( uint_least8 == 255 );
|
||||
assert( uint_fast8 == 255 );
|
||||
assert( int16 == -32767 );
|
||||
assert( int_least16 == -32767 );
|
||||
assert( int_fast16 == -32767 );
|
||||
assert( uint16 == 65535 );
|
||||
assert( uint_least16 == 65535 );
|
||||
assert( uint_fast16 == 65535 );
|
||||
assert( int32 == -2147483647 );
|
||||
assert( int_least32 == -2147483647 );
|
||||
assert( int_fast32 == -2147483647 );
|
||||
assert( uint32 == 4294967295 );
|
||||
assert( uint_least32 == 4294967295 );
|
||||
assert( uint_fast32 == 4294967295 );
|
||||
assert( intmax == -2147483647 );
|
||||
assert( uintmax == 4294967295 );
|
||||
|
||||
std::cout << "OK\n";
|
||||
return 0;
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 30 Jul 00 Add typename syntax fix (Jens Maurer)
|
||||
// 28 Aug 99 Initial version
|
||||
|
||||
#ifndef BOOST_INTEGER_HPP
|
||||
@ -45,7 +46,7 @@ namespace boost
|
||||
template< int Bits > // bits (including sign) required
|
||||
struct int_t
|
||||
{
|
||||
typedef int_least_helper
|
||||
typedef typename int_least_helper
|
||||
<
|
||||
(Bits-1 <= std::numeric_limits<long>::digits) +
|
||||
(Bits-1 <= std::numeric_limits<int>::digits) +
|
||||
@ -59,7 +60,7 @@ namespace boost
|
||||
template< int Bits > // bits required
|
||||
struct uint_t
|
||||
{
|
||||
typedef int_least_helper
|
||||
typedef typename int_least_helper
|
||||
<
|
||||
5 +
|
||||
(Bits <= std::numeric_limits<unsigned long>::digits) +
|
||||
@ -77,3 +78,4 @@ namespace boost
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_INTEGER_HPP
|
||||
|
||||
|
98
index.htm
Normal file
98
index.htm
Normal file
@ -0,0 +1,98 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Language" content="en-us">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Boost Integer Library</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
|
||||
<table border="1" bgcolor="#007F7F" cellpadding="2">
|
||||
<tr>
|
||||
<td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td>
|
||||
<td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td>
|
||||
<td><a href="../../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td>
|
||||
<td><a href="../../people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td>
|
||||
<td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td>
|
||||
<td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h1>Boost Integer Library</h1>
|
||||
|
||||
<table border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td align="center"><b><i>Header / Docs</i></b></td>
|
||||
<td align="center"><b><i>Contents</i></b></td>
|
||||
<td align="center"><b><i>Use</i></b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code><a href="../../boost/cstdint.hpp"><boost/cstdint.hpp><br>
|
||||
</a></code><a href="cstdint.htm"><br>
|
||||
documentation</a>
|
||||
</td>
|
||||
<td valign="top">Contents of <code><boost/stdint.h></code> wrapped in namespace boost.</td>
|
||||
<td valign="top">Supplies typedefs for standard integer types such as <code> int32_t</code> or <code>uint_least16_t</code>.
|
||||
Use in preference to <<code>stdint.h></code> or<code> <boost/stdint.h></code>
|
||||
because the names are safely placed in the boost namespace.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code><a href="../../boost/stdint.h"><boost/stdint.h></a></code></td>
|
||||
<td valign="top">Typedefs as defined in the C99 standard header <<code>stdint.h></code>.
|
||||
This implementation #includes the compiler
|
||||
supplied <<code>stdint.h></code>, if present.</td>
|
||||
<td valign="top"> Supplied
|
||||
for use in the implementation of <boost/cstdint.hpp> and to ease transition to the C99 standard.
|
||||
Other uses are not recommended because this header places its names in the global namespace. </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code><a href="../../boost/integer_traits.hpp"><boost/integer_traits.hpp></a></code><br>
|
||||
<br>
|
||||
<a href="integer_traits.html">documentation</a>
|
||||
</td>
|
||||
<td valign="top">Template class <code>boost::integer_traits</code>, derived from <code>std::numeric_limits</code>.
|
||||
Adds <code>const_min</code> and <code>const_max</code> members.</td>
|
||||
<td valign="top">Use to obtain the characteristics of a known integer type.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code><a href="../../boost/integer.hpp"><boost/integer.hpp></a><br>
|
||||
<br>
|
||||
</code><a href="integer.htm">documentation</a></td>
|
||||
<td valign="top">Templates for integer type selection based on properties such as
|
||||
maximum value or number of bits.</td>
|
||||
<td valign="top">Use to select the type an integer when some property such as maximum value or number of bits is known.
|
||||
Useful for generic programming. </td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Rationale</h2>
|
||||
<p>The organization of boost integer headers and classes is designed to take
|
||||
advantage of <code><stdint.h></code> types from in the 1999 C standard
|
||||
without resorting to undefined behavior in terms of
|
||||
the 1998 C++ standard. The header <code><boost/cstdint.hpp></code>
|
||||
makes the standard integer types safely available in namespace boost without placing any names in the
|
||||
global namespace or namespace std. As always, the intension is to complement rather than
|
||||
compete with the C++ Standard Library. Should some future C++ standard
|
||||
include <code><stdint.h></code> and <code><cstdint></code>, then <code><boost/stdint.h></code> and <code><boost/cstdint.hpp></code>
|
||||
will continue to function, but will become redundant and may be safely deprecated.</p>
|
||||
<p>Because these are boost headers, their names conform to boost header naming
|
||||
conventions rather than C++ Standard Library header naming conventions. An
|
||||
exception is <code><boost/stdint.h> </code>which uses a <b> .h</b> extension to indicate its C rather than C++ heritage.</p>
|
||||
<h2>Caveat emptor</h2>
|
||||
<p>As an
|
||||
implementation artifact, certain C <limits.h> macro names may possibly be
|
||||
visible to users of <boost/cstdint.hpp>. Don't use these macros; they are not part of
|
||||
any Boost specified interface.
|
||||
Use boost:: integer_traits<> or std::numeric_limits<> instead.</p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Revised: <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %b %Y" startspan -->05 Mar 2000<!--webbot bot="Timestamp" endspan i-checksum="14882" -->
|
||||
</p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
110
integer.htm
Normal file
110
integer.htm
Normal file
@ -0,0 +1,110 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Integer Type Selection Templates</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">Integer
|
||||
Type Selection Templates</h1>
|
||||
<p>The <code><a href="../../boost/integer.hpp"><boost/integer.hpp></a></code>
|
||||
type selection templates allow integer types to be selected based on desired
|
||||
characteristics such as number of bits or maximum value. This facility is
|
||||
particularly useful for solving generic programming problems.</p>
|
||||
|
||||
<p>The templates <b>int_t<></b> and <b>uint_t<></b> supply typedefs <b>least</b>
|
||||
and <b>fast</b>. The <b>least</b> type be the smallest type which holds at
|
||||
least the number of bits (including sign) specified. The <b>fast</b> type will
|
||||
be at least as large as the <b>least</b> type, but may possible be larger.
|
||||
There is no guarantee that the <b>fast</b> type will actually be faster than
|
||||
other possible types.</p>
|
||||
|
||||
<h2>Alternative</h2>
|
||||
|
||||
<p>If the number of bits required is known beforehand, it may be more
|
||||
appropriate to use the types supplied in <code><a href="../../boost/cstdint.hpp"><boost/cstdint.hpp></a></code>.</p>
|
||||
|
||||
<h2>Synopsis</h2>
|
||||
|
||||
<blockquote>
|
||||
<pre>namespace boost
|
||||
{
|
||||
// fast integers from least integers
|
||||
template< typename LeastInt > // Required: LeastInt is integral type, not bool
|
||||
struct int_fast_t { typedef LeastInt fast; }; // implementations may specialize
|
||||
|
||||
// signed
|
||||
template< int Bits > // bits (including sign) required, 0-32 valid
|
||||
struct int_t
|
||||
{
|
||||
typedef <i>implementation-supplied</i> least;
|
||||
typedef int_fast_t<least>::fast fast;
|
||||
};
|
||||
|
||||
// unsigned
|
||||
template< int Bits > // bits required, 0-32 valid
|
||||
struct uint_t
|
||||
{
|
||||
typedef <i>implementation-supplied</i> least;
|
||||
typedef int_fast_t<least>::fast fast;
|
||||
};
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
</blockquote>
|
||||
<p>[Templates to select type based on maximum value are under development.]
|
||||
</p>
|
||||
|
||||
<h2>Example</h2>
|
||||
|
||||
<blockquote>
|
||||
<pre>#include <boost/integer.hpp>
|
||||
using boost::int_t;
|
||||
|
||||
...
|
||||
int_t<24>::least my_var; </pre>
|
||||
|
||||
</blockquote>
|
||||
<h2>Demonstration Program</h2>
|
||||
|
||||
<p>The program <a href="integer_test.cpp">integer_test.cpp</a> is a not very
|
||||
smart demonstration of the results from instantiating various <b>int_t<></b>
|
||||
and <b>uint_t<></b> examples.</p>
|
||||
|
||||
<h2>Rationale</h2>
|
||||
|
||||
<p>The rationale for the design of the templates in this header includes:</p>
|
||||
|
||||
<ul>
|
||||
<li>Avoid recursion because of concern about C++'s limited guaranteed
|
||||
recursion depth (17).</li>
|
||||
<li>Avoid macros on general principles.</li>
|
||||
<li>Try to keep the design as simple as possible.</li>
|
||||
</ul>
|
||||
|
||||
<h2>Credits</h2>
|
||||
|
||||
<p>The author of the Boost integer type choosing templates is <a href="../../people/beman_dawes.html">Beman
|
||||
Dawes</a>. He thanks to <a href="../../people/valentin_bonnard.htm"> Valentin Bonnard</a> and
|
||||
<a href="../../people/kevlin_henney.htm"> Kevlin Henney</a> for sharing their designs for similar templates.</p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Revised August 31, 1999</p>
|
||||
|
||||
<p><EFBFBD> Copyright Beman Dawes 1999. Permission to copy, use, modify, sell
|
||||
and distribute this document is granted provided this copyright notice appears in all
|
||||
copies. This document is provided "as is" without express or implied warranty,
|
||||
and with no claim as to its suitability for any purpose.</p>
|
||||
|
||||
<p></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
176
integer_test.cpp
Normal file
176
integer_test.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
// boost integer.hpp test program ------------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes 1999. Permission to copy, use, modify, sell
|
||||
// and distribute this software is granted provided this copyright
|
||||
// notice appears in all copies. This software is provided "as is" without
|
||||
// express or implied warranty, and with no claim as to its suitability for
|
||||
// any purpose.
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 31 Aug 99 Initial version
|
||||
|
||||
// This program is misnamed in that it is really a demonstration rather than
|
||||
// a test. It doesn't detect failure, so isn't worthy of the name "test".
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/integer.hpp>
|
||||
|
||||
using namespace boost; // not the best practice, but useful for testing
|
||||
|
||||
namespace
|
||||
{
|
||||
void test( long ) { std::cout << "long\n"; }
|
||||
void test( int ) { std::cout << "int\n"; }
|
||||
void test( short ) { std::cout << "short\n"; }
|
||||
void test( signed char ) { std::cout << "signed char\n"; }
|
||||
void test( unsigned long ) { std::cout << "unsigned long\n"; }
|
||||
void test( unsigned int ) { std::cout << "unsigned int\n"; }
|
||||
void test( unsigned short ) { std::cout << "unsigned short\n"; }
|
||||
void test( unsigned char ) { std::cout << "unsigned char\n"; }
|
||||
} // unnamed namespace
|
||||
|
||||
// just to prove it works, specialize int_fast_t<short> to yield long
|
||||
namespace boost
|
||||
{
|
||||
template<> struct int_fast_t<short> { typedef long fast; };
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << 32 << ' '; test( int_t<32>::least() );
|
||||
std::cout << 31 << ' '; test( int_t<31>::least() );
|
||||
std::cout << 30 << ' '; test( int_t<30>::least() );
|
||||
std::cout << 29 << ' '; test( int_t<29>::least() );
|
||||
std::cout << 28 << ' '; test( int_t<28>::least() );
|
||||
std::cout << 27 << ' '; test( int_t<27>::least() );
|
||||
std::cout << 26 << ' '; test( int_t<26>::least() );
|
||||
std::cout << 25 << ' '; test( int_t<25>::least() );
|
||||
std::cout << 24 << ' '; test( int_t<24>::least() );
|
||||
std::cout << 23 << ' '; test( int_t<23>::least() );
|
||||
std::cout << 22 << ' '; test( int_t<22>::least() );
|
||||
std::cout << 21 << ' '; test( int_t<21>::least() );
|
||||
std::cout << 20 << ' '; test( int_t<20>::least() );
|
||||
std::cout << 19 << ' '; test( int_t<19>::least() );
|
||||
std::cout << 18 << ' '; test( int_t<18>::least() );
|
||||
std::cout << 17 << ' '; test( int_t<17>::least() );
|
||||
std::cout << 16 << ' '; test( int_t<16>::least() );
|
||||
std::cout << 15 << ' '; test( int_t<15>::least() );
|
||||
std::cout << 14 << ' '; test( int_t<14>::least() );
|
||||
std::cout << 13 << ' '; test( int_t<13>::least() );
|
||||
std::cout << 12 << ' '; test( int_t<12>::least() );
|
||||
std::cout << 11 << ' '; test( int_t<11>::least() );
|
||||
std::cout << 10 << ' '; test( int_t<10>::least() );
|
||||
std::cout << 9 << ' '; test( int_t<9>::least() );
|
||||
std::cout << 8 << ' '; test( int_t<8>::least() );
|
||||
std::cout << 7 << ' '; test( int_t<7>::least() );
|
||||
std::cout << 6 << ' '; test( int_t<6>::least() );
|
||||
std::cout << 5 << ' '; test( int_t<5>::least() );
|
||||
std::cout << 4 << ' '; test( int_t<4>::least() );
|
||||
std::cout << 3 << ' '; test( int_t<3>::least() );
|
||||
std::cout << 2 << ' '; test( int_t<2>::least() );
|
||||
std::cout << 1 << ' '; test( int_t<1>::least() );
|
||||
std::cout << 0 << ' '; test( int_t<0>::least() );
|
||||
std::cout << 32 << ' '; test( int_t<32>::fast() );
|
||||
std::cout << 31 << ' '; test( int_t<31>::fast() );
|
||||
std::cout << 30 << ' '; test( int_t<30>::fast() );
|
||||
std::cout << 29 << ' '; test( int_t<29>::fast() );
|
||||
std::cout << 28 << ' '; test( int_t<28>::fast() );
|
||||
std::cout << 27 << ' '; test( int_t<27>::fast() );
|
||||
std::cout << 26 << ' '; test( int_t<26>::fast() );
|
||||
std::cout << 25 << ' '; test( int_t<25>::fast() );
|
||||
std::cout << 24 << ' '; test( int_t<24>::fast() );
|
||||
std::cout << 23 << ' '; test( int_t<23>::fast() );
|
||||
std::cout << 22 << ' '; test( int_t<22>::fast() );
|
||||
std::cout << 21 << ' '; test( int_t<21>::fast() );
|
||||
std::cout << 20 << ' '; test( int_t<20>::fast() );
|
||||
std::cout << 19 << ' '; test( int_t<19>::fast() );
|
||||
std::cout << 18 << ' '; test( int_t<18>::fast() );
|
||||
std::cout << 17 << ' '; test( int_t<17>::fast() );
|
||||
std::cout << 16 << ' '; test( int_t<16>::fast() );
|
||||
std::cout << 15 << ' '; test( int_t<15>::fast() );
|
||||
std::cout << 14 << ' '; test( int_t<14>::fast() );
|
||||
std::cout << 13 << ' '; test( int_t<13>::fast() );
|
||||
std::cout << 12 << ' '; test( int_t<12>::fast() );
|
||||
std::cout << 11 << ' '; test( int_t<11>::fast() );
|
||||
std::cout << 10 << ' '; test( int_t<10>::fast() );
|
||||
std::cout << 9 << ' '; test( int_t<9>::fast() );
|
||||
std::cout << 8 << ' '; test( int_t<8>::fast() );
|
||||
std::cout << 7 << ' '; test( int_t<7>::fast() );
|
||||
std::cout << 6 << ' '; test( int_t<6>::fast() );
|
||||
std::cout << 5 << ' '; test( int_t<5>::fast() );
|
||||
std::cout << 4 << ' '; test( int_t<4>::fast() );
|
||||
std::cout << 3 << ' '; test( int_t<3>::fast() );
|
||||
std::cout << 2 << ' '; test( int_t<2>::fast() );
|
||||
std::cout << 1 << ' '; test( int_t<1>::fast() );
|
||||
std::cout << 0 << ' '; test( int_t<0>::fast() );
|
||||
std::cout << 32 << ' '; test( uint_t<32>::least() );
|
||||
std::cout << 31 << ' '; test( uint_t<31>::least() );
|
||||
std::cout << 30 << ' '; test( uint_t<30>::least() );
|
||||
std::cout << 29 << ' '; test( uint_t<29>::least() );
|
||||
std::cout << 28 << ' '; test( uint_t<28>::least() );
|
||||
std::cout << 27 << ' '; test( uint_t<27>::least() );
|
||||
std::cout << 26 << ' '; test( uint_t<26>::least() );
|
||||
std::cout << 25 << ' '; test( uint_t<25>::least() );
|
||||
std::cout << 24 << ' '; test( uint_t<24>::least() );
|
||||
std::cout << 23 << ' '; test( uint_t<23>::least() );
|
||||
std::cout << 22 << ' '; test( uint_t<22>::least() );
|
||||
std::cout << 21 << ' '; test( uint_t<21>::least() );
|
||||
std::cout << 20 << ' '; test( uint_t<20>::least() );
|
||||
std::cout << 19 << ' '; test( uint_t<19>::least() );
|
||||
std::cout << 18 << ' '; test( uint_t<18>::least() );
|
||||
std::cout << 17 << ' '; test( uint_t<17>::least() );
|
||||
std::cout << 16 << ' '; test( uint_t<16>::least() );
|
||||
std::cout << 15 << ' '; test( uint_t<15>::least() );
|
||||
std::cout << 14 << ' '; test( uint_t<14>::least() );
|
||||
std::cout << 13 << ' '; test( uint_t<13>::least() );
|
||||
std::cout << 12 << ' '; test( uint_t<12>::least() );
|
||||
std::cout << 11 << ' '; test( uint_t<11>::least() );
|
||||
std::cout << 10 << ' '; test( uint_t<10>::least() );
|
||||
std::cout << 9 << ' '; test( uint_t<9>::least() );
|
||||
std::cout << 8 << ' '; test( uint_t<8>::least() );
|
||||
std::cout << 7 << ' '; test( uint_t<7>::least() );
|
||||
std::cout << 6 << ' '; test( uint_t<6>::least() );
|
||||
std::cout << 5 << ' '; test( uint_t<5>::least() );
|
||||
std::cout << 4 << ' '; test( uint_t<4>::least() );
|
||||
std::cout << 3 << ' '; test( uint_t<3>::least() );
|
||||
std::cout << 2 << ' '; test( uint_t<2>::least() );
|
||||
std::cout << 1 << ' '; test( uint_t<1>::least() );
|
||||
std::cout << 0 << ' '; test( uint_t<0>::least() );
|
||||
std::cout << 32 << ' '; test( uint_t<32>::fast() );
|
||||
std::cout << 31 << ' '; test( uint_t<31>::fast() );
|
||||
std::cout << 30 << ' '; test( uint_t<30>::fast() );
|
||||
std::cout << 29 << ' '; test( uint_t<29>::fast() );
|
||||
std::cout << 28 << ' '; test( uint_t<28>::fast() );
|
||||
std::cout << 27 << ' '; test( uint_t<27>::fast() );
|
||||
std::cout << 26 << ' '; test( uint_t<26>::fast() );
|
||||
std::cout << 25 << ' '; test( uint_t<25>::fast() );
|
||||
std::cout << 24 << ' '; test( uint_t<24>::fast() );
|
||||
std::cout << 23 << ' '; test( uint_t<23>::fast() );
|
||||
std::cout << 22 << ' '; test( uint_t<22>::fast() );
|
||||
std::cout << 21 << ' '; test( uint_t<21>::fast() );
|
||||
std::cout << 20 << ' '; test( uint_t<20>::fast() );
|
||||
std::cout << 19 << ' '; test( uint_t<19>::fast() );
|
||||
std::cout << 18 << ' '; test( uint_t<18>::fast() );
|
||||
std::cout << 17 << ' '; test( uint_t<17>::fast() );
|
||||
std::cout << 16 << ' '; test( uint_t<16>::fast() );
|
||||
std::cout << 15 << ' '; test( uint_t<15>::fast() );
|
||||
std::cout << 14 << ' '; test( uint_t<14>::fast() );
|
||||
std::cout << 13 << ' '; test( uint_t<13>::fast() );
|
||||
std::cout << 12 << ' '; test( uint_t<12>::fast() );
|
||||
std::cout << 11 << ' '; test( uint_t<11>::fast() );
|
||||
std::cout << 10 << ' '; test( uint_t<10>::fast() );
|
||||
std::cout << 9 << ' '; test( uint_t<9>::fast() );
|
||||
std::cout << 8 << ' '; test( uint_t<8>::fast() );
|
||||
std::cout << 7 << ' '; test( uint_t<7>::fast() );
|
||||
std::cout << 6 << ' '; test( uint_t<6>::fast() );
|
||||
std::cout << 5 << ' '; test( uint_t<5>::fast() );
|
||||
std::cout << 4 << ' '; test( uint_t<4>::fast() );
|
||||
std::cout << 3 << ' '; test( uint_t<3>::fast() );
|
||||
std::cout << 2 << ' '; test( uint_t<2>::fast() );
|
||||
std::cout << 1 << ' '; test( uint_t<1>::fast() );
|
||||
std::cout << 0 << ' '; test( uint_t<0>::fast() );
|
||||
|
||||
return 0;
|
||||
}
|
89
integer_traits.html
Normal file
89
integer_traits.html
Normal file
@ -0,0 +1,89 @@
|
||||
<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 <limits> header supplies a class template
|
||||
numeric_limits<> with specializations for each fundamental
|
||||
type.</p>
|
||||
<p>
|
||||
For integer types, the interesting members of std::numeric_limits<> 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<class T>
|
||||
class integer_traits : public std::numeric_limits<T>
|
||||
{
|
||||
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<T>::min()</code></td></tr>
|
||||
<tr><td><code>const_max</code></td><td><code>T</code></td><td>equivalent
|
||||
to <code>std::numeric_limits<T>::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<>::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
|
83
integer_traits_test.cpp
Normal file
83
integer_traits_test.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
/* boost integer_traits.hpp tests
|
||||
*
|
||||
* Copyright Jens Maurer 2000
|
||||
* Permission to use, copy, modify, sell, and distribute this software
|
||||
* is hereby granted without free provided that the above copyright notice
|
||||
* appears in all copies and that both that copyright notice and this
|
||||
* permission notice appear in supporting documentation,
|
||||
*
|
||||
* Jens Maurer makes no representations about the suitability of this
|
||||
* software for any purpose. It is provided "as is" without express or
|
||||
* implied warranty.
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
* Revision history
|
||||
* 2000-02-22 Small improvements by Beman Dawes
|
||||
* 2000-06-27 Rework for better MSVC and BCC co-operation
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <boost/integer_traits.hpp>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#error This test relies on assert() and thus makes no sense with NDEBUG defined
|
||||
#endif
|
||||
|
||||
// enable if you have "long long" and the proper macros in <limits.h>
|
||||
#undef HAVE_LONG_LONG
|
||||
|
||||
|
||||
/*
|
||||
* General portability note:
|
||||
* MSVC mis-compiles explicit function template instantiations.
|
||||
* For example, f<A>() and f<B>() are both compiled to call f<A>().
|
||||
* BCC is unable to implicitly convert a "const char *" to a std::string
|
||||
* when using explicit function template instantiations.
|
||||
*
|
||||
* Therefore, avoid explicit function template instantiations.
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
void runtest(const char * type, T)
|
||||
{
|
||||
typedef boost::integer_traits<T> traits;
|
||||
std::cout << "Checking " << type
|
||||
<< "; min is " << traits::min()
|
||||
<< ", max is " << traits::max()
|
||||
<< std::endl;
|
||||
assert(traits::is_specialized);
|
||||
assert(traits::is_integer);
|
||||
assert(traits::is_integral);
|
||||
assert(traits::const_min == traits::min());
|
||||
assert(traits::const_max == traits::max());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
runtest("bool", bool());
|
||||
runtest("char", char());
|
||||
typedef signed char signed_char;
|
||||
runtest("signed char", signed_char());
|
||||
typedef unsigned char unsigned_char;
|
||||
runtest("unsigned char", unsigned_char());
|
||||
runtest("short", short());
|
||||
typedef unsigned short unsigned_short;
|
||||
runtest("unsigned short", unsigned_short());
|
||||
runtest("int", int());
|
||||
typedef unsigned int unsigned_int;
|
||||
runtest("unsigned int", unsigned_int());
|
||||
runtest("long", long());
|
||||
typedef unsigned long unsigned_long;
|
||||
runtest("unsigned long", unsigned_long());
|
||||
#ifdef HAVE_LONG_LONG
|
||||
typedef long long long_long;
|
||||
runtest("long long", long_long());
|
||||
typedef unsigned long long unsigned_long_long;
|
||||
runtest("unsigned long long", unsigned_long_long());
|
||||
#endif
|
||||
// Some compilers don't pay attention to std:3.6.1/5 and issue a
|
||||
// warning here if "return 0;" is omitted.
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user