Domanda

I am experimenting with Boost.Range and the Boost Tuple. If I have a Tuple of ranges, how can I typedef a Tuple or the corresponding element values? To put this another way, what do I put in place of /*?*/ here:

typedef boost::tuples::tuple<std::vector<int>&, char[]> TupleOfRanges;
typedef /*?*/ TupleOfElements;

I can do this by hand, of course and I would write:

typedef boost::tuples::tuple<int, char> TupleOfElements;

Or even:

typedef typename boost::tuples::element<0, TupleOfRanges>::type Range0;
typedef typename boost::tuples::element<1, TupleOfRanges>::type Range1;
typedef typename boost::range_iterator<Range0>::type Iterator0;
typedef typename boost::range_iterator<Range1>::type Iterator1;
typedef typename boost::iterator_value<Iterator0>::type Value0;
typedef typename boost::iterator_value<Iterator1>::type Value1;

typedef boost::tuples::tuple<Value0, Value1> TupleOfElements;

But I think it should be possible to derive TupleOfElements directly from TupleOfRanges, whatever the tuple size. Any ideas welcome!

Edit: this seems to work, thanks @ltjax:

struct GetIteratorType
{
    template <class Range>
    struct apply
    {
        typedef typename boost::range_iterator<Range>::type type;
    };
};
typedef boost::mpl::transform<TupleOfRanges, GetIteratorType> TupleOfIterators;

struct GetElementType
{
    template <class Iterator>
    struct apply
    {
        typedef typename boost::iterator_value<Iterator>::type type;
    };
};
typedef boost::mpl::transform<TupleOfIterators, GetElementType> TupleOfElements;
È stato utile?

Soluzione

Use boost::mpl::transform with that typedef chain you wrote as the functor!

See http://www.boost.org/doc/libs/1_47_0/libs/mpl/doc/refmanual/transform.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top