Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-01-17 08:24:13


"Fredrik Blomqvist" <fredrik_blomqvist_at_[hidden]> writes:

> Hi there,
>
> I thought I'd forward this little link:
> http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-CPPCompileTimeBinCons
> t&forum=totd&id=-1
> (several improved versions of the initial code appear in the thread)
>
> This has most likely been done before but IMO a boostified version using MPL
> and PP could
> actually be a useful convenience utility.

Aleksey and I were just discussing this one. As soon as he's done
implementing the "for_" algorithm it could look like this:

template <unsigned long N> // N must be an *octal* constant
struct binary
    : for_<N, _1, shift_right<_1, int_<3> >, // "forward" state, condition, step
            plus<bitand_<_1, int_<1> >, _2> // "backward" step
>
    {};

Now binary<01101>::type::value = 13

> (since I also remember the discussions of a roman-number utility for example
> :)

Left as an exercise for the reader ;-)

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

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