imp::unsigned_int< MinBits >

Contents

The Type

This library declares a class-template unsigned_int< N > in namespace imp:

namespace imp
{
  template < unsigned MinBits >
  struct unsigned_int
  {
    // *implementation details snipped* ...
  };
}

Goal

To emulate, as far as possible, an inbuilt unsigned integral type with a user specified fixed minumum number of bits.

Provides

  • A templated converting constructor from any inbuilt numeric type (int, double etc ).

  • Conversion to any numeric type (via a templated operator T () ) including bool.

  • All of inbuilt unsigned's operator's:

    unary +, -, !

    binary +, -, * , /, %, ++, --, <<, >>, ^, &, |

    and +=, -=, *=, /=, %=, <<=, >>=, ^=, &=, |=.

  • An explicit Constructor from char const (&)[] ( unsigned_int<> has no literal's ).

  • Overloads of the stream operators << and >>.

  • A specialization of std::numeric_limits< unsigned_int< ... > >.

Limitations

  • unsigned_int<>::operator<> T () will match any argument (that isn't a non-const reference) of any member of an overload set. so explicit cast's are required (See also Overloading).

    Alternatively for non-member function's an overload that takes an unsigned_int<> can be added.

  • By design the actual number of bits provided is a multiple of std::numeric_limits< unsigned >::digits.

  • Narrowing conversion's don't produce a loss of precision warning, see Widening Conversion.

See Also

A Basic Declaration

imp::unsigned_int< 128 > variable;

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

Ok

A Basic Example

#include <iostream>
#include <ostream>

#include "imp/unsigned_int.hpp"

unsigned const Bits = 256; // for example.

imp::unsigned_int< Bits >
    default_constructed, // = 0
    int_constructed( 2 ),
    float_constructed( 2e7 ),
    cstr_constructed( "0x123456789ABCDEF" ), //  explicit
    copy_constructed( int_constructed );

int main()
{
  default_constructed = int_constructed * float_constructed;
  default_constructed /= copy_constructed;
  default_constructed += cstr_constructed;
  std::cout << default_constructed << std::endl;
}

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

81985529236486895

Table of Contents