我正在试验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;
但我认为应该可以直接从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;
最佳答案 使用boost :: mpl :: transform和你编写为functor的typedef链!
见http://www.boost.org/doc/libs/1_47_0/libs/mpl/doc/refmanual/transform.html