我试图将不同类型的 N 个参数包折叠为<$的 std :: tuple c $ c> N-1 std :: pairs 与相应的类型。
I am trying to fold a parameter pack of N different types into a std::tuple of N-1 std::pairs with respective types.
因此,例如表达式
ResolveToTupleOfPairs<void, int, long>::Type tuple;应评估为
std::tuple<std::pair<void, int>, std::pair<int, long>> tuple;所以我正在搜索 ResolveToTupleOfPairs 按说明折叠参数包。我当前的实现如下,但是很显然,它使类型成为成对的元组,每个对具有两次相同的类型,而不是< T0,T1> ;,< T1,T2> ;, ... 。
So I am searching for an implementation of the ResolveToTupleOfPairs type to fold the parameter pack as explained. My current implementation follows, but obviously it causes the type to be a tuple of pairs which each hold the same type twice instead of <T0, T1>, <T1, T2>, ....
template<typename... T> struct ResolveToTupleOfPairs { static_assert(sizeof...(Args) > 1, "need at least two arguments"); using Type = std::tuple<std::pair<T, T>...>; };我可以接受 c ++ 17 解决方案。
推荐答案我们利用了以下事实:参数包扩展确实非常聪明
We take advantage of the fact that parameter pack expansion is really really smart
template<typename...> struct fold; template<size_t... Is, typename... Ts> struct fold<std::index_sequence<Is...>, Ts...> { using tuple = std::tuple<Ts...>; using type = std::tuple<std::pair<std::tuple_element_t<Is, tuple>, std::tuple_element_t<Is + 1, tuple>>...>; }; template<typename... Ts> using fold_t = typename fold<std::make_index_sequence<sizeof...(Ts) - 1>, Ts...>::type;实时
更多推荐
将N种类型的参数包折叠成N
发布评论