您如何迭代std :: tuple的元素? [英] How can you iterate over the elements of an std::tuple?

查看:326
本文介绍了您如何迭代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:抱歉,未实现:无法将侦听器..."扩展为固定长度的参数列表.
错误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());

这篇关于您如何迭代std :: tuple的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆