Boost logo

Boost :

From: Marco Costalba (mcostalba_at_[hidden])
Date: 2007-10-02 06:24:47


On 10/2/07, Joel de Guzman <joel_at_[hidden]> wrote:
>
> Ok, your call, since you're the one taking the challenge anyway :)
>

I have tried to remove the dependancy from mpl library and perhaps I
have found a nice way to do it.

The key idea is to use the deinitions of

tuple<A, B, C, D>

that is (perhaps) a

cons<A, cons<B, cons<C, cons<D, null_type> > > >

So instead of the following previous code:

    namespace detail
    {
        template <typename Seq, typename derived, int n, int size>
        struct do_compose
        {
            typedef typename Seq::head_type head;
            typedef typename Seq::tail_type tail;

            typedef typename do_compose<tail, derived, n - 1, size>::type ov;
            typedef overload_function<ov, derived, head> type;
        };

        template <typename Seq, typename derived, int size>
        struct do_compose<Seq, derived, 1, size>
        {
            typedef typename Seq::head_type head;

            typedef overload_function<final_overload_function<size>, derived,
head> type;
        };

        template <typename Seq>
        struct compose_overload_base
        {
            enum { n = tuples::length<Seq>::value };

            typedef overload<Seq> derived;
            typedef typename do_compose<Seq, derived, n, n>::type type;
        };

        struct get_function
        {
           template <typename T>
           struct apply
           {
               typedef boost::function<T> type;
           };
        };
    }

    template <typename Seq>
    struct overload : detail::compose_overload_base<Seq>::type
    {
            typedef typename boost::mpl::
            transform<Seq, detail::get_function>::type functions_type;

        functions_type functions;
    };

I could write directly

            
 namespace detail
    {
        template <typename Seq, typename derived, int n, int size>
        struct do_compose
        {
            typedef typename Seq::head_type head;
            typedef typename Seq::tail_type tail;

            typedef do_compose<tail, derived, n - 1, size> next;

            typedef typename next::type ov;
            typedef typename next::cons cons_tail;

            typedef overload_function<ov, derived, head> type;
            typedef tuples::cons<boost::function<head>, cons_tail> cons;
        };

        template <typename Seq, typename derived, int size>
        struct do_compose<Seq, derived, 1, size>
        {
            typedef typename Seq::head_type head;

            typedef final_overload_function<size> final;

            typedef overload_function<final, derived, head> type;
            typedef tuples::cons<head, tuple<> > cons;
        };

        template <typename Seq>
        struct compose_overload_base : do_compose<
                                         Seq,
                                         overload<Seq>,
                                         tuples::length<Seq>::value,
                                         tuples::length<Seq>::value
> {};
    }

   template <typename Seq>
    struct overload : detail::compose_overload_base<Seq>::type
    {
            typedef detail::compose_overload_base<Seq> Base;
            typedef typename Base::cons functions_type;

        functions_type functions; // COMPILE ERROR HERE
    };

But I have an error as soon as I try to instantiate 'functions_type'

The compile error seems very internal to tuple library and an help on
how to come up with this would be very apreciated...

Thanks a lot for your patience (I'm a newbie)
Marco


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