迭代元组 [英] iterate over tuple
本文介绍了迭代元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何迭代一个元组(使用C ++ 11)?我尝试以下,但是不起作用:
How can I iterate over a tuple (using C++11)? I tried the following, but that doesn't work:
for(int i=0; i<std::tuple_size<T...>::value; ++i)
std::get<i>(my_tuple).do_sth();
错误1:sorry,未实现:无法展开Listener ... '转换为固定长度的参数列表。
错误2:我不能出现在常量表达式中。
Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list.
Error 2: i cannot appear in a constant expression.
那么,如何正确迭代元组的元素?
So, how do I correctly iterate over the elements of a tuple?
推荐答案
Boost.Fusion 是一种可能性:
未经测试的示例:
struct DoSomething
{
template<typename T>
void operator()(T& t) const
{
t.do_sth();
}
};
tuple<....> t = ...;
boost::fusion::for_each(t, DoSomething());
这篇关于迭代元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文