Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-01-28 02:05:47


Is there or could there be a class template that can return the smallest
unsigned type for a given number of bits? I guess the interface could be
like:

// Need much better names
namespace boost
{
    template < ::std::size_t L > class best_type;
}

// Can't exactly use this since we want 'L' values that are greater
// than the number of bits in "unsigned long" (or "unsigned long long")
// to fail.
template < std::size_t L >
boost::best_type
{
    typedef typename best_type<L + 1>::type type;

    static bool const needs_masking = true;
    //static std::size_t const padding = best_type<L + 1>::padding + 1;
};

//...

// No assumptions of the "standard" computer platforms is used, so
// an "8" is not hard-coded. (The C++ std mandates the number is
// at least eight, though.)
template <>
boost::best_type< std::numeric_limits<unsigned char>::digits >
{
    typedef unsigned char type;

    static bool const needs_masking = false;
    //static std::size_t const padding = 0;
};

// Question: should best_type<1> use "unsigned char" or "bool"?
// Question: what should happen for best_type<0>?

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