c++boost.gif (8819 bytes)Integer Type Selection Templates

The <boost/integer.hpp> 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.

The templates int_t<> and uint_t<> supply typedefs least and fast.  The least type be the smallest type which holds at least the number of bits (including sign) specified. The fast type will be at least as large as the least type, but may possible be larger.  There is no guarantee that the fast type will actually be faster than other possible types.

Alternative

If the number of bits required is known beforehand, it may be more appropriate to use the types supplied in <boost/cstdint.hpp>.

Synopsis

namespace boost
{
  //  fast integers from least integers
  // int_fast_t<> works correctly for unsigned too, in spite of the name.
  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 implementation-supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  //  unsigned
  template< int Bits >   // bits required, 0-32 valid
  struct uint_t 
  {
      typedef implementation-supplied  least;
      typedef int_fast_t<least>::fast  fast;
      // int_fast_t<> works correctly for unsigned too, in spite of the name.
  };
} // namespace boost

[Templates to select type based on maximum value are under development.]

Example

#include <boost/integer.hpp>
using boost::int_t;

...
int_t<24>::least my_var;  

Demonstration Program

The program integer_test.cpp is a not very smart demonstration of the results from instantiating various int_t<> and uint_t<> examples.

Rationale

The rationale for the design of the templates in this header includes:

Credits

The author of the Boost integer type choosing templates is Beman Dawes.  He thanks to Valentin Bonnard and Kevlin Henney for sharing their designs for similar templates.


Revised  August 31, 1999

© 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.