C++ Boost.Range 的元组 - 获取元素类型的元组?
我正在尝试 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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
boost::mpl::transform
与您编写为函子的 typedef 链一起使用!请参阅http://www.boost.org/ doc/libs/1_47_0/libs/mpl/doc/refmanual/transform.html
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