boostorg / hana

Your standard library for metaprogramming
http://boostorg.github.io/hana
Boost Software License 1.0
1.66k stars 217 forks source link

Is there a replacement for fusion::nview #505

Closed jarro2783 closed 2 years ago

jarro2783 commented 2 years ago

Is there an equivalent for fusion::nview? I have some code that calls

fold(nview<T, Indices>(t), ...)

I am trying to improve compile time so I think hana is a good replacement, but I'm not quite sure how to do this.

tzlaine commented 2 years ago

This is very easy to do yourself:

auto t = /* some hana tuple */;
hana::fold_left(
  Indices{}, // should be a sequence of hana integral constants for convenience
  result_type{},
  [t](auto result, auto x) {
    // get the xth element out of t, using the current index x from Indices{}
    auto t_element = t[x];
    return /* some function of result and t_element */;
});
jarro2783 commented 2 years ago

Ahah, that's what I was looking for, I couldn't quite get my head around it.

Thanks very much.

jarro2783 commented 2 years ago

Ok next problem is that t is a BOOST_FUSION_DEFINE_STRUCT, and I can't access it with []. I'm not sure if I can just convert this to BOOST_HANA_DEFINE_STRUCT and it works, or if I somehow need to fold over the struct.

tzlaine commented 2 years ago

I believe it will work if you use the hana version of the adapt macro. You can also just use the number in each x (which is a compile-time constant) above to get the xth value out of t, even if t is a fusion tuple.

jarro2783 commented 2 years ago

I tried using BOOST_HANA_DEFINE_STRUCT, and that doesn't provide a [] operator either. Is there a function that will get the xth element out of one of those structs?

jarro2783 commented 2 years ago

Actually I just managed to get this to work. I used hana::accessors to get element x. Thanks for your help.