如何迭代 std::tuple 的元素?
如何迭代元组(使用 C++11)?我尝试了以下方法:
How can I iterate over a tuple (using C++11)? I tried the following:
for(int i=0; i<std::tuple_size<T...>::value; ++i)
std::get<i>(my_tuple).do_sth();
但这不起作用:
错误 1:抱歉,未实现:无法将Listener ..."扩展为固定长度的参数列表.
错误 2:i 不能出现在常量表达式中.
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());
相关文章