C++ Hana中满足谓词的元组元素索引序列

C++ Hana中满足谓词的元组元素索引序列,c++,tuples,c++14,boost-hana,C++,Tuples,C++14,Boost Hana,是否有一种简明的方法来获取满足中谓词的元组元素的索引序列 以下是我仅使用标准库编写的代码: 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&g

是否有一种简明的方法来获取满足中谓词的元组元素的索引序列

以下是我仅使用标准库编写的代码:

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;
模板
类获取的索引{
静态constexpr size\u t size=std::tuple\u size::value;
模板结构impl{
使用type=std::conditional\u t<
Pred::value,
typename impl::type,
typename impl::type>;
};
模板结构impl{
使用type=std::index\u序列;
};
公众:
使用type=typename impl::type;
};
模板
使用get\u index\u of_t=typename get\u index\u of::type;
用法示例:

using types = std::tuple<std::string,int,double,char>;
using ints = get_indices_of_t<std::is_integral,types>;
使用types=std::tuple;
使用ints=获取t的索引;

ints
的类型现在是
std::index\u sequence

我猜是这样的:

constexpr auto get_indices_of = [](auto tuple, auto predicate){
    constexpr auto indices = to<tuple_tag>(range_c<std::size_t, 0, size(tuple)>);
    return filter(indices, [=](auto i){ return predicate(tuple[i]); }); 
};

你的代码怎么了?不管怎么说,它都会被藏在图书馆的标题里,所以谁在乎它是否有点复杂。我对如何以不同的方式实现这一点有一些想法,但如果不知道当前方法的问题是什么,很难找出最好的方法。我的代码没有问题。我把它贴出来解释我想做什么。我只是想知道Hana是否有一个算法来解决这个问题。猫和狗互相回答元编程问题-集体歇斯底里!顺便说一句,从技术上讲,对该功能也有一个开放的请求,之所以需要
到(range_c)
,是因为
range_c
不是
MonadPlus
(请参见),而不是因为它不是
序列。这只是吹毛求疵,你的回答在其他方面是正确的。@LouisDionne一定是使用了一个古老的hana,我发誓静态断言说它必须是一个序列-肯定是MonadPlus说的。呜呜声。
constexpr auto types = make_tuple(
    type_c<std::string>, type_c<int>, type_c<double>, type_c<char>);
constexpr auto ints = get_indices_of(types, trait<std::is_integral>);