How to remove metaprogramming recursion with Boost Hana

Viewed 211

I'm trying to create a bitset according to the type send to the function. But let's reduce the test case a little.

Warning : I'm using auto gcc extension for this example, I don't need to use template parameter.

namespace hana = boost::hana;

constexpr decltype(auto) rec(auto i, auto max, auto f, auto returnValue) {

  return returnValue |= f(i);
  if constexpr (i < max) //"infinite" loop if no constexpr
    return rec(i + hana::size_c<1>, max, f, returnValue);
  else
    return returnValue;
}

constexpr decltype(auto) foo(auto ct, auto ... type) {

  constexpr auto tuple = hana::make_tuple(type...);
  constexpr unsigned long returnValue = 0L;

  constexpr auto f = [tuple, ct] (auto i) {
    if (hana::contains(tuple, ct[i]))
      return 0 << decltype(i)::value;
    else
      return 0;
  };

  return rec(hana::size_c<0>, hana::size_c<3>, f, returnValue);
}

struct T1 {};
struct T2 {};
struct T3 {};

int main () {

  constexpr auto t1 = hana::type_c<T1>;
  constexpr auto t2 = hana::type_c<T2>;
  constexpr auto t3 = hana::type_c<T3>;
  constexpr auto ct = hana::make_tuple(t1, t2, t3);
  constexpr auto test = foo(ct, t1, t2);
}

Seems like my tuple is not considered Searchable, but if I try the same hana::contains outside the lambda I got no problem.

The whole error is huge so check it there : live demo

By the way, I tried to do this with a for loop but failed. Do you know a good way of doing this kind of things in C++17/20 ?

1 Answers
Related