Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2008-07-20 09:49:52

David Abrahams wrote:

> This instantiates the template on the cdr with N-1 iterations
> i.e., it's like advance(++x, n-1)
> You need ++advance(x, n-1) to avoid O(N^2)
>> >
>> element;

Nifty! I implemented this in fusion. Pretty straightforward (yeah,
I'm using partial specialization):

     template <typename Cons>
     struct cons_deref
         typedef typename Cons::car_type type;

     template <typename Cons, int I>
     struct cons_advance
         typedef typename
             cons_advance<Cons, I-1>::type::cdr_type

     template <typename Cons>
     struct cons_advance<Cons, 0>
         typedef Cons type;


     template <>
     struct at_impl<cons_tag>
         template <typename Sequence, typename N>
         struct apply
             typedef detail::cons_deref<
                 typename cons_advance<Sequence, N::value>::type>


As an added bonus, I unrolled advance for up to N==5.

Thanks for the tips, Dave, Steven!


Joel de Guzman

Boost list run by bdawes at, gregod at, cpdaniel at, john at