Sequence of indices of tuple elements satifying predicate in Hana

Viewed 206

Is there a concise way to get a sequence of indices of tuple elements which satisfy a predicate in Hana?

Here's the code I wrote for that using just the standard library:

template <template<typename> typename Pred, typename Tuple>
class get_indices_of {
  static constexpr size_t size = std::tuple_size<Tuple>::value;
  template <size_t I, size_t... II> struct impl {
    using type = std::conditional_t<
      Pred<std::tuple_element_t<I,Tuple>>::value,
      typename impl<I+1, II..., I>::type,
      typename impl<I+1, II...>::type >;
  };
  template <size_t... II> struct impl<size,II...> {
    using type = std::index_sequence<II...>;
  };
public:
  using type = typename impl<0>::type;
};
template <template<typename> typename Pred, typename Tuple>
using get_indices_of_t = typename get_indices_of<Pred,Tuple>::type;

Usage example:

using types = std::tuple<std::string,int,double,char>;
using ints = get_indices_of_t<std::is_integral,types>;

The type of ints is now std::index_sequence<1ul, 3ul>.

1 Answers
Related