c++ - 如何遍历 std::tuple 的元素?

标签 c++ c++11 iteration template-meta-programming stdtuple

如何迭代元组(使用 C++11)?我尝试了以下方法:

for(int i=0; i<std::tuple_size<T...>::value; ++i) 
  std::get<i>(my_tuple).do_sth();

但这行不通:

Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list.
Error 2: i cannot appear in a constant expression.

那么,我该如何正确地迭代元组的元素呢?

最佳答案

我有一个基于 Iterating over a Tuple 的答案:

#include <tuple>
#include <utility> 
#include <iostream>

template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
  print(std::tuple<Tp...>& t)
  { }

template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
  print(std::tuple<Tp...>& t)
  {
    std::cout << std::get<I>(t) << std::endl;
    print<I + 1, Tp...>(t);
  }

int
main()
{
  typedef std::tuple<int, float, double> T;
  T t = std::make_tuple(2, 3.14159F, 2345.678);

  print(t);
}

通常的想法是使用编译时递归。事实上,这个想法被用来制作一个类型安全的 printf,如原始元组论文中所述。

这可以很容易地概括为元组的 for_each:

#include <tuple>
#include <utility> 

template<std::size_t I = 0, typename FuncT, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
  for_each(std::tuple<Tp...> &, FuncT) // Unused arguments are given no names.
  { }

template<std::size_t I = 0, typename FuncT, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
  for_each(std::tuple<Tp...>& t, FuncT f)
  {
    f(std::get<I>(t));
    for_each<I + 1, FuncT, Tp...>(t, f);
  }

虽然这需要一些努力让 FuncT 代表元组可能包含的每种类型具有适当重载的东西。如果您知道所有元组元素将共享一个公共(public)基类或类似的东西,那么这种方法效果最好。

关于c++ - 如何遍历 std::tuple 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24296511/

相关文章:

python - 将数组列表发送到 For 循环

c++ - 在迭代时修改数据结构

c++ - 模板问题

c++ - 处理未知大小的输入

javascript - 如果我不知道名称,如何访问 javascript 对象的属性?

c++ - 对不带参数的可变参数模板函数的调用不明确?

c++ - 尝试编译队列时出现 static_initialization_and_destruction 错误

c++ - 如何在 Switch 语句中使用 Cmd 行参数中提供的 TCHAR*?

c++ - 在编译时检查静态函数是否在类中可用

C++11 "In class initialization"功能不适用于 union