boost.png (6897 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.

Contents

Synopsis

#include <boost/integer_fwd.hpp>  // forwarding header
#include <boost/cstdint.hpp>      // for boost::uintmax_t, intmax_t

namespace boost
{
  //  fast integers from least integers
  template< typename LeastInt >
  struct int_fast_t
  {
      typedef implementation_supplied  fast;
  };

  //  signed
  template< int Bits >
  struct int_t 
  {
      typedef implementation_supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  template< int Bits >
  struct int_exact_t
  {
      typedef implementation_supplied  exact;
  };

  //  unsigned
  template< int Bits >
  struct uint_t 
  {
      typedef implementation_supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  template< int Bits >
  struct uint_exact_t
  {
      typedef implementation_supplied  exact;
  };

  //  signed
  template< intmax_t MaxValue >
  struct int_max_value_t 
  {
      typedef implementation_supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  template< intmax_t MinValue >
  struct int_min_value_t 
  {
      typedef implementation_supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  //  unsigned
  template< uintmax_t Value >
  struct uint_value_t 
  {
      typedef implementation_supplied  least;
      typedef int_fast_t<least>::fast  fast;
  };

  //  MPL-compatible
  template< int Bits, typename Signedness >
  struct exact_integral
  {
      static  bool const  is_specialized = implementation_supplied;
      static  bool const  is_signed = implementation_supplied;
      static  int const   bit_count = Bits;

      typedef implementation_supplied  type;
  };
} // namespace boost

Processor-Optimized Types

The int_fast_t class template maps its input type to the next-largest type that the processor can manipulate the easiest, or to itself if the input type is already an easy-to-manipulate type. For instance, processing a bunch of char objects may go faster if they were converted to int objects before processing. The input type, passed as the only template parameter, must be a built-in integral type, except bool. Unsigned integral types can be used, as well as signed integral types, despite the name. The output type is given as the class member fast.

Implementation Notes
By default, the output type is identical to the input type. Eventually, this code's implementation should be conditionalized for each platform to give accurate mappings between the built-in types and the easiest-to-manipulate built-in types. Also, there is no guarantee that the output type actually is easier to manipulate than the input type.

Sized Types

The int_t, int_exact_t, uint_t, uint_exact_t, int_max_value_t, int_min_value_t, and uint_value_t class templates find the most appropriate built-in integral type for the given template parameter. This type is given by the class member least or exact. For the non-exact class templates, the easiest-to-manipulate version of that type is given by the class member fast. The following table describes each template's criteria.

Criteria for the Sized Type Class Templates
Class Template (all in name-space boost) Template Parameter Mapping
int_t The smallest built-in signed integral type with at least the given number of bits, including the sign bit. The parameter must be a positive number. A compile-time error results if the parameter is larger than the number of bits in a boost::intmax_t.
int_exact_t The smallest built-in signed integral type with exactly the given number of bits, including the sign bit. A compile-time error results if no qualifying type exists.
uint_t The smallest built-in unsigned integral type with at least the given number of bits. The parameter must be a non-negative number. A compile-time error results if the parameter is larger than the number of bits in a boost::uintmax_t.
uint_exact_t The smallest built-in unsigned integral type with exactly the given number of bits. A compile-time error results if no qualifying type exists.
int_max_value_t The smallest built-in signed integral type that supports the given value as a maximum. The parameter must be a positive number.
int_min_value_t The smallest built-in signed integral type that supports the given value as a minimum. The parameter must be a negative number.
uint_value_t The smallest built-in unsigned integral type that supports the given value as a maximum. The parameter should be a positive number.

MPL-Compatible Variants

The bit-length sized-type class templates have several drawbacks:

The exact_integral class template provides an MPL-compatible alternative. This alternative has the form:

template< SwitchType SwitchValue, typename Signedness >
struct name
{
    static  bool const         is_specialized = implementation_supplied;
    static  bool const         is_signed = implementation_supplied;
    static  SwitchType const   switch_id = SwitchValue;

    typedef implementation_supplied  type;
};

Each member, if present, is defined by:

Members in MPL-Compatible Class Templates
Class Template Member When Defined Meaning
is_specialized Always Flag indicating when a particular template class instantiation is a valid meta-function (true) or not (false).
is_signed is_specialized == true Flag indicating whether the signed-variant (true) or the unsigned-variant (false) of the meta-function is used. This is controlled by the Signedness template parameter:
Effect of Signedness Setting
Signedness Type is_signed
signed true
unsigned false
anything else not defined
The type used is a programmer mnemonic; the compiler cannot prevent someone from using int or signed int instead of signed, or unsigned int instead of unsigned.
switch_id (Actual name is template-specific.) Always The value of the main control parameter, accessible even if the template class instantiation is aliased.
type is_specialized == true The meta-function's result. It appears only if the input parameters satisfy the template's requirements. It's presence, or lack thereof, enables "Substitution Failure Is Not An Error" (SFINAE) techniques, instead of a hard compiler diagnostic.

The following table describes each template's criteria. The classic signed and unsigned equivalents are the sized-type class templates that each MPL-compatible class template emulates. (The setting of Signedness controls the appropriate emulation.)

Criteria for the MPL-Compatible Class Templates
Class Template (all in name-space boost) Parameter Type Parameter Member ID Classic Equivalent Template Parameter Mapping (when type is defined)
Signed Unsigned
exact_integral int bit_count int_exact_t uint_exact_t The smallest built-in integral type with exactly bit_count bits (including the sign bit when Signedness is signed). Not present if no type qualifies.

Example

#include <boost/integer.hpp>
#include <boost/mpl/int.hpp>
#include <iostream>
#include <ostream>

//...

template < int Bits >
bool
fit_exactly( boost::mpl::int_<Bits> const &x,
 typename boost::exact_integral<Bits, signed>::type *unused = 0 )
{
    return true;
}

template < typename T >
bool
fit_exactly( T const &x )
{
    return false;
}

//...

int main()
{
    typedef boost::mpl::int_<24>  twenty_four;

    boost::int_t<twenty_four::value>::least my_var;

    //...

    std::cout << "my_var " << ( fit_exactly(twenty_four()) ? "does" :
     "does not" ) << " fit its type exactly." << std::endl;

    //...
}

Demonstration Program

The program integer_test.cpp is a simplistic demonstration of the results from instantiating various examples of the sized type class templates.

Rationale

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

Alternative

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

Credits

The author of most of the Boost integer type choosing templates is Beman Dawes. He gives thanks to Valentin Bonnard and Kevlin Henney for sharing their designs for similar templates. Daryle Walker designed the exact and value-based sized templates, and the MPL-compatible templates.


Revised July 15, 2008

© Copyright Beman Dawes 1999. Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)