Etherealone Etherealone - 2 months ago 17
C++ Question

Iterating over boost::hana::tuple

I could not find a way to access real object with

hana::for_each
iterating over tuples.

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;
});


Type of
a
appears to be
hana::tuple_impl<...>
and seems to be not-castable to its underlying type
decltype(std::decay_t<a>)::type
.

I basically want to iterate over a list of templated objects (containers) that have the same interface but contain different values. Better ways to achieve this is welcome.

Answer

tuple_t is for a tuple of hana::types. You want a tuple of normal objects, which is just tuple:

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