gpt4 book ai didi

c++ - 如何从模板类的可变包中的每个类型中恢复非类型名模板参数?

转载 作者:行者123 更新时间:2023-11-30 03:17:17 25 4
gpt4 key购买 nike

我正在尝试从一组可变模板类(其中每个类都有一个非类型参数)中恢复非类型名模板参数,以便我可以将它们用作另一种类型中的整数序列。

下面的代码显示了我所拥有的。整数序列/成员序列是从元组中抄袭的。

template<std::size_t... Integers>
struct size_t_sequence {
using type = size_t_sequence<Integers...>;
};

template <std::size_t, typename>
struct push_size_t_sequence;

template <std::size_t I, std::size_t... Integers>
struct push_size_t_sequence<I, size_t_sequence<Integers...>>
: size_t_sequence<I, Integers...> {};

template <std::size_t N, std::size_t Head, std::size_t... Tail>
struct make_size_t_sequence
: push_size_t_sequence<Head, typename make_size_t_sequence<N - 1, Tail...>::type>::type {};

template<std::size_t I, std::size_t... OneLeft>
struct make_size_t_sequence <2, I, OneLeft...> :
push_size_t_sequence<I, size_t_sequence<OneLeft...>>::type {};



template<typename... Members>
struct member_sequence {
using type = member_sequence<Members...>;
};
template <typename, typename>
struct push_member_sequence;

template <typename M, typename... Members>
struct push_member_sequence<M, member_sequence<Members...>>
: member_sequence<M, Members...> {};

template <std::size_t N, typename Head, typename... Tail>
struct make_member_sequence
: push_member_sequence<Head, typename make_member_sequence<N - 1, Tail...>::type>::type {};

template<typename M, typename... OneLeft>
struct make_member_sequence <2, M, OneLeft...> :
push_member_sequence<M, member_sequence<OneLeft...>>::type {};


template<typename>
struct unpack_sequence_impl;

template<template<std::size_t> class... T, std::size_t... DimensionSizes>
struct unpack_sequence_impl<member_sequence<T<DimensionSizes>...>> {
using member_types = member_sequence<T<DimensionSizes>...>;
using size_types = size_t_sequence<DimensionSizes...>;
};

template<typename... Members>
struct unpack_sequence :
unpack_sequence_impl<make_member_sequence<sizeof...(Members), Members...>> {
using base_t = unpack_sequence_impl<make_member_sequence<sizeof...(Members), Members...>>;
using member_types = typename base_t::member_types;
using size_types = typename base_t::size_types;
};


template<std::size_t N>
class example {
int s = N;
};

int main()
{
auto mem_sequence = make_member_sequence<3, example<3>, example<2>, example<1>>::type();
auto integer_sequence = make_size_t_sequence<3, 3,2,1>::type();
auto un_mem_sequence = unpack_sequence<example<3>, example<2>, example<1>>::member_types();
auto un_size_sequence = unpack_sequence<example<3>, example<2>, example<1>>::size_types();
}

mem_sequence 的类型和 integer_sequencemember_sequence<example<3>,example<2>,example<1>>size_t_sequence<3,2,1> . un_mem_sequence的类型和 un_size_sequence应该是一样的。

我怎样才能做到这一点?

感谢您的帮助!

蒂姆

编辑:

为了澄清,我试图完成的是从一个模板类中恢复模板参数以在另一个模板类中使用它们。下面是三个模板类:MyObject , MyTuple , MyPack . MyTuple需要 MyObject对象作为其模板参数。我想恢复 MyObject用作 MyPack 的模板参数的模板参数对象。

template<int N>
MyObject;

template<int... Ns>
MyPack;

template<typename... MyObjects>
MyTuple {};



int main() {
MyTuple<MyObject<1>,MyObject<2>,MyObject<3>> a;
MyPack<1,2,3> b;
}

所以我想从 MyObject 的 MyTuple 中提取参数以用于创建 MyPack。

编辑 2:

第二次澄清:MyTuple不只是需要 MyObject类型但具有一个 int 模板参数的任何类型。

template<int N>
MyObject;

template<int N>
MyObject2;

template<int... Ns>
MyPack;

template<typename... MyObjects>
MyTuple {};



int main() {
MyTuple<MyObject<1>,MyObject<2>,MyObject2<1>,MyObject2<2>> a;
MyPack<1,2,1,2> b;
}

最佳答案

template <typename>
struct MakeMyPack;

template <int... Ns>
struct MakeMyPack<MyTuple<MyObject<Ns>...>>
{
using type = MyPack<Ns...>;
};

DEMO

关于c++ - 如何从模板类的可变包中的每个类型中恢复非类型名模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55618409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com