C ++ Tuple of Boost.Range - 获取元素类型的元组?

时间:2011-08-10 12:30:42

标签: c++ metaprogramming boost-tuples

我正在试验Boost.Range和Boost Tuple。如果我有一个范围元组,我如何键入一个元组或相应的元素值?换句话说,我在这里代替/*?*/

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

我可以手工完成,当然我会写:

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

甚至:

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;

但我认为应该可以直接从TupleOfElements派生TupleOfRanges,无论元组大小如何。欢迎任何想法!

编辑:这似乎有效,谢谢@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 个答案:

答案 0 :(得分:2)

boost::mpl::transform与您编写为functor的typedef链一起使用!

请参阅http://www.boost.org/doc/libs/1_47_0/libs/mpl/doc/refmanual/transform.html

相关问题