【问题标题】:C++ Tuple of Boost.Range - get Tuple of element types?Boost.Range 的 C++ 元组 - 获取元素类型的元组?
【发布时间】:2011-08-10 12:30:42
【问题描述】:

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

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;

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

编辑:这似乎可行,谢谢@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;

【问题讨论】:

    标签: c++ metaprogramming boost-tuples


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2018-02-18
      相关资源
      最近更新 更多