Iterating over boost::hana::tuple
Asked Answered
I

1

3

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.

Indusium answered 4/10, 2016 at 14:8 Comment(2)
I doubt this decltype(boost::hana::tuple_t<A, B>). What does this mean? Isn't tuple_t<A,B> a type itself?Charily
@Nawaz Documentation says its usage is like this auto types = hana::tuple_t<int*, char&, void>; So I guess it is a C++14 variable template. boost.org/doc/libs/1_61_0/libs/hana/doc/html/index.htmlIndusium
A
10

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;
});
Aport answered 4/10, 2016 at 14:25 Comment(5)
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.Indusium
@Indusium tuple_t<Ts...> is approximately a wrapper for tuple<type_c<Ts>...>. You use it when you want type_cs.Aport
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.Indusium
@Indusium Yes, this is it.Blackfish
@LouisDionne Oh hey Louis :)Aport

© 2022 - 2024 — McMap. All rights reserved.