Boost logo

Boost :

Subject: Re: [boost] Boost.Integer makeover
From: Ravi (lists_ravi_at_[hidden])
Date: 2009-11-29 10:25:38


On Sunday 29 November 2009 12:38:40 John Bytheway wrote:
> I've used the compile-time log2 to determine the smallest integer type
> in which I can store any value of an enumeration. It's not easy to use,
> though; I'd prefer something that returned ceil(log2(n)) rather than
> floor(log2(n)). I can't make any sense of the existing example.

Here's what I use (if it can go into boost, that'd be one less thing I'd have
to maintain):

template <boost::static_log2_argument_type num> struct static_log2_ceil
{
  BOOST_STATIC_CONSTANT( boost::static_log2_result_type,
                         value = static_log2<num-1>::value+1 );
};

// Handle the case of static_log2 not allowing 0
template <> struct static_log2_ceil<1>
{
  BOOST_STATIC_CONSTANT( boost::static_log2_result_type, value = 0 );
};

// Asking for log2(0) is an error
template <> struct static_log2_ceil<0> {};

If this is useful to you, could you please create a trac ticket and attach the
implementation as a patch?

Regards,
Ravi


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