将N种类型的参数包折叠成N

编程入门 行业动态 更新时间:2024-10-18 03:37:19
本文介绍了将N种类型的参数包折叠成N-1对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将不同类型的 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

本文发布于:2023-11-29 12:02:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:种类   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!