C++ Tuple of Boost.Range - get Tuple of element types?

那年仲夏 提交于 2020-01-03 17:48:10

问题


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;

回答1:


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



来源:https://stackoverflow.com/questions/7010791/c-tuple-of-boost-range-get-tuple-of-element-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!