从Typelist创建向量元组

Aco*_*orn 5 c++ templates tuples vector typelist

我有一个简单的类型列表实现;

template<typename... Ts> 
struct Typelist
{
  static constexpr size_t count{sizeof...(Ts)};
};
Run Code Online (Sandbox Code Playgroud)

我想用它做的,是生成std::tuplestd::vector>在每一个类型串式; 例如:

struct A {};
struct B {};
struct C {};

using myStructs = typelist<A,B,C>;
using myList = tupleOfVectorTypes<myStructs>; tuple<vector<A>, vector<B>, vector<C>>
Run Code Online (Sandbox Code Playgroud)

这就是我一直在玩的:

template<template<typename... Ts> class T>
struct List
{
  using type = std::tuple<std::vector<Ts>...>;
};
Run Code Online (Sandbox Code Playgroud)

然而,它继续吐出它期望的类型.我试过包裹Ts decltype,就像这样:

using type = std::tuple<std::vector<decltype(Ts)>...>;

但那也是错的,我猜我也decltype错了.那么,我如何基于我抛出的类型列表创建类型向量的元组?

Sam*_*hik 6

诀窍是使用专门化深入到模板参数.

-std=c++1z模式下使用gcc 5.3.1进行测试:

#include <vector>
#include <tuple>

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

// Declare List
template<class> class List;

// Specialize it, in order to drill down into the template parameters.
template<template<typename...Args> class t, typename ...Ts>
struct List<t<Ts...>> {
    using type = std::tuple<std::vector<Ts>...>;
};

// Sample Typelist

struct A{};
struct B{};
struct C{};

using myStructs = Typelist<A,B,C>;

// And, the tuple of vectors:

List<myStructs>::type my_tuple;

// Proof

int main()
{
    std::vector<A> &a_ref=std::get<0>(my_tuple);
    std::vector<B> &b_ref=std::get<1>(my_tuple);
    std::vector<C> &c_ref=std::get<2>(my_tuple);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)