Folding a parameter pack of N types into N-1 pairs

好久不见. 提交于 2019-12-06 02:16:24

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;

Live

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!