Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-03-16 09:03:04


The latest version (6) of "crc.zip" contains a prototype of finding a
built-in (unsigned) type that has exactly the desired number of bits. The
class template is in "crc.hpp" and is called "detail::exact_uint_t". It
should be a regular Boost class, maybe in <boost/integer.hpp>.

Here's a copy:

//=================================================================
#include <boost/integer.hpp>
#include <climits>
#include <cstddef>

//...

    // Forward declarations
    template < ::std::size_t Bits >
        struct exact_uint_t;

    template < >
        struct exact_uint_t< CHAR_BIT >;

#if USHRT_MAX > UCHAR_MAX
    template < >
        struct exact_uint_t< CHAR_BIT * sizeof(unsigned short) >;
#endif

#if UINT_MAX > USHRT_MAX
    template < >
        struct exact_uint_t< CHAR_BIT * sizeof(unsigned int) >;
#endif

#if ULONG_MAX > UINT_MAX
    template < >
        struct exact_uint_t< CHAR_BIT * sizeof(unsigned long) >;
#endif

    // Traits class for an unsigned type with exactly the amount
    // of given bits (should go into <boost/integer.hpp> later)
    template < >
    struct exact_uint_t< CHAR_BIT >
    {
        typedef unsigned char exact;
        typedef typename ::boost::int_fast_t<exact>::fast fast;

    }; // boost::detail::exact_uint_t

#if USHRT_MAX > UCHAR_MAX
    template < >
    struct exact_uint_t< CHAR_BIT * sizeof(unsigned short) >
    {
        typedef unsigned short exact;
        typedef typename ::boost::int_fast_t<exact>::fast fast;

    }; // boost::detail::exact_uint_t
#endif

#if UINT_MAX > USHRT_MAX
    template < >
    struct exact_uint_t< CHAR_BIT * sizeof(unsigned int) >
    {
        typedef unsigned int exact;
        typedef typename ::boost::int_fast_t<exact>::fast fast;

    }; // boost::detail::exact_uint_t
#endif

#if ULONG_MAX > UINT_MAX
    template < >
    struct exact_uint_t< CHAR_BIT * sizeof(unsigned long) >
    {
        typedef unsigned long exact;
        typedef typename ::boost::int_fast_t<exact>::fast fast;

    }; // boost::detail::exact_uint_t
#endif
//=================================================================

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk