遍历 boost::hana::tuple

Iterating over boost::hana::tuple

我找不到通过 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<...> 并且似乎不可转换为其基础类型 decltype(std::decay_t<a>)::type

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

tuple_t 用于 hana::type 的元组。你想要一个 tuple 的普通对象,它只是 tuple:

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