Boost logo

Boost :

From: Marco (mrcekets_at_[hidden])
Date: 2007-10-02 13:46:17


On Tue, 02 Oct 2007 12:24:47 +0200, Marco Costalba <mcostalba_at_[hidden]>
wrote:

skip
>
> I could write directly
>
>
           
skip

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

Maybe you should replace tuple<> with tuples::null_type ...

skip

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

... but I don't know if this is the only problem.

However your last changes makes the code too complicated, IMO.
I made up a metafunction called wrapper that should do the work.

examples:

(1)

typedef tuple<int, double, char> fund_types;

typedef wrapper<fund_types, vector>::type vectors;

BOOST_STATIC_ASSERT((
        is_same< vectors,
                 cons<vector<int>,
                       cons<vector<double>,
                            cons<vector<char>,
                                 null_type
>
>
>
> ));

(2)

typedef tuple<
                 int(char)
                     , double(int, char)
               , char(std::string)
>
         signatures;

typedef wrapper<signatures, boost::function>::type functions;

BOOST_STATIC_ASSERT((
        is_same< functions,
                  cons<function<int(char)>,
                       cons<function<double(int, char)>,
                            cons<function<char(std::string)>,
                                 null_type
>
>
>
> ));

Here follow the code for the metafunction:

namespace boost { namespace detail {

template<
     typename Seq,
     template<typename > class TT,
     unsigned int N = tuples::length<Seq>::value
>
struct wrapper
{
     typedef typename Seq::head_type head;
     typedef typename Seq::tail_type tail;

     typedef TT<head> new_head;
     typedef typename wrapper<tail, TT>::type new_tail;

     typedef
         tuples::cons< new_head, new_tail >
         type;
};

template< typename Seq, template<typename > class TT>
struct wrapper<Seq, TT, 0>
{
     typedef tuples::null_type type;
};

} } // end namespaces

I used it inside the overload struct replacing the code:

typedef typename tuples::element<0, Seq>::type Sig0;
typedef typename tuples::element<1, Seq>::type Sig1;
typedef typename tuples::element<2, Seq>::type Sig2;
typedef typename tuples::element<3, Seq>::type Sig3;

typedef
     tuple<
          boost::function<Sig0>
        , boost::function<Sig1>
        , boost::function<Sig2>
        , boost::function<Sig3>
>
     functions_type;

with a single line :

typedef
     typename detail::wrapper<Seq, boost::function>::type
     functions_type;

... and all seems to work correctly. I tested it with gcc 4.1.2 and boost
1.33.1 under linux.
Moreover the wrapper metafunction can be used in other context too.

The question is :
it's ok to use cons< cons < ... >, ..., null_type > instead of tuple type ?

Regards,
Marco Cecchetti

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

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