2

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.

Etherealone
  • 3,488
  • 2
  • 37
  • 56

1 Answers1

9

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;
});
Barry
  • 286,269
  • 29
  • 621
  • 977
  • 1
    It does work. Can you elaborate when to use hana types a little bit and what are they for exactly? Documentation is a bit misleading about it. It talks like I _have to_ wrap objects (which are not integral, `tuple_c` is for integrals) to `hana_types` and `tupe_t` is the easy way to do it. – Etherealone Oct 04 '16 at 14:28
  • 1
    @Etherealone `tuple_t` is approximately a wrapper for `tuple...>`. You use it when you want `type_c`s. – Barry Oct 04 '16 at 14:33
  • 1
    Thanks. I don't know if I got it but it looks like I would use `tuple_t` to operate on types in an MPL-ish way (hana operates on objects of type `hana_type` and allows us to work on actual C++ types via these objects (values)). And normal tuple is for normal object usage. – Etherealone Oct 04 '16 at 14:46
  • 1
    @Etherealone Yes, this is it. – Louis Dionne Oct 05 '16 at 03:27
  • @LouisDionne Oh hey Louis :) – Barry Oct 05 '16 at 11:31