【问题标题】:Iterating over boost::hana::tuple迭代 boost::hana::tuple
【发布时间】:2016-10-04 14:08:59
【问题描述】:

我找不到通过hana::for_each 遍历元组来访问真实对象的方法。

struct A {
  std::string name;
}

struct B {
  std::string name;
}

using type_t = decltype(boost::hana::tuple_t<A, B>);
type_t names;

boost::hana::for_each(names, [&](const auto& a) {
      std::cout << a.name << std::endl;
    });

a 的类型似乎是 hana::tuple_impl&lt;...&gt;,并且似乎不能转换为其基础类型 decltype(std::decay_t&lt;a&gt;)::type

我基本上想遍历具有相同接口但包含不同值的模板化对象(容器)列表。欢迎使用更好的方法来实现这一目标。

【问题讨论】:

  • 我怀疑这个decltype(boost::hana::tuple_t&lt;A, B&gt;)。这是什么意思? tuple_t&lt;A,B&gt; 本身不是 type 吗?
  • @Nawaz 文档说它的用法是这样的 auto types = hana::tuple_t&lt;int*, char&amp;, void&gt;; 所以我猜它是一个 C++14 变量模板。 boost.org/doc/libs/1_61_0/libs/hana/doc/html/index.html

标签: c++ boost boost-hana


【解决方案1】:

tuple_t 用于 hana::types 的元组。你想要一个普通对象的tuple,也就是tuple

boost::hana::tuple<A, B> names;
boost::hana::for_each(names, [&](const auto& x) {
    std::cout << x.name << std::endl;
});

【讨论】:

  • 它确实有效。您能否详细说明何时使用 hana 类型以及它们的确切用途?文档对此有点误导。它说我必须将对象(不是积分,tuple_c 用于积分)包装到hana_typestupe_t 是最简单的方法。
  • @Etherealone tuple_t&lt;Ts...&gt; 大约是 tuple&lt;type_c&lt;Ts&gt;...&gt; 的包装器。你想用就用type_cs.
  • 谢谢。我不知道我是否明白了,但看起来我会使用 tuple_t 以类似 MPL 的方式对类型进行操作(hana 对 hana_type 类型的对象进行操作,并允许我们通过这些处理实际的 C++ 类型对象(值))。正常的元组用于正常的对象使用。
  • @Etherealone 是的,就是这样。
  • @LouisDionne 哦,嘿,路易斯 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
相关资源
最近更新 更多