相关疑难解决方法(0)

如何使用Variadic模板展平类型树?

我有这样的结构:

template<typename... Ts>
struct List {}

typedef List<char,List<int,float,List<int,unsigned char>>,List<unsigned,short>> MyList;
Run Code Online (Sandbox Code Playgroud)

我想基本上把它压扁到一个列表.什么是最好的方法?如果我把它弄得足够长,我想我可以通过递归制作一些东西,但有些东西告诉我应该有更好的方法.

我想要的上述树的结果应该类似于:

typedef List<char,int,float,int,unsigned char,unsigned,short> FlattenedList;
Run Code Online (Sandbox Code Playgroud)

这是我的第一次尝试:

template<typename... Ts>
struct List{};

template<typename... Ts>
struct FlattenTree{
    typedef List<Ts...> Type;
};
template<typename... Ts, typename... Us, typename... Vs>
struct FlattenTree<Ts...,List<Us...>,Vs...>{
    typedef typename FlattenTree<Ts..., Us..., Vs...>::Type Type;
};
Run Code Online (Sandbox Code Playgroud)

但它会导致此错误: error C3515: if an argument for a class template partial specialization is a pack expansion it shall be the last argument

rici在这里指出MSVC2013在抱怨什么,所以这里没有编译错误:

§14.8.2.5(从类型中推导模板参数)第5段列出了无法推导出模板参数的上下文.相关的是列表中的最后一个:

— A function parameter pack that does not occur at …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming variadic-templates c++11

9
推荐指数
1
解决办法
1232
查看次数