选择模板参数包中的每个偶数(或奇数)参数

Bar*_*wek 6 c++ templates metaprogramming c++11

我想允许使用我正在编写的类来指定类型列表以及这些类型的分配器列表作为模板参数,其中类型位于奇数位置,分配器位于偶数位置:

template<typename... T>
class MyClass {
  // Stuff inside
}

int main() {
  MyClass<SomeType1, AllocatorOfSomeType1> c1;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2> c2;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2,
          SomeType3, AllocatorOfSomeType3> c3;
  // And so on....
}
Run Code Online (Sandbox Code Playgroud)

在内部,有一个用于存储类型的向量元组是有意义的:

std::tuple<std::vector<EveryOddTypeInParameterPack>...> m_storage_;
Run Code Online (Sandbox Code Playgroud)

和一个用于使用的分配器元组:

std::tuple<std::vector<EveryEvenTypeInParameterPack>...> m_storage_;
Run Code Online (Sandbox Code Playgroud)

我如何在代码中实际声明这些元组?理论上我需要以某种方式选择参数包中的每个奇数/偶数类型 - 这可能吗?

Ise*_*ria 3

虽然代码有点长,但我认为该机制没有不必要的特性。
如果我正确理解这个问题,下面的代码可能会达到目的:

// push front for tuple
template< class, class > struct PFT;

template< class A, class... T > struct PFT< A, tuple< T... > > {
  typedef tuple< A, T... > type;
};

// for even
template< class... > struct even_tuple;

template< class A, class B > struct even_tuple< A, B > {
  typedef tuple< A > type;
};
template< class A, class B, class... T > struct even_tuple< A, B, T... > {
  typedef typename PFT< A, typename even_tuple< T... >::type >::type type;
};
// As for odd elements, in the same way as even(please see the test on ideone)

// objective type
template< class > struct storage_type;

template< class... T > struct storage_type< tuple< T... > > {
  typedef tuple< vector< T >... > type;
};

template< class... T >
struct MyClass {
  typename storage_type< typename even_tuple< T... >::type >::type
    m_storage_even_;
  typename storage_type< typename  odd_tuple< T... >::type >::type
    m_storage_odd_;
};
Run Code Online (Sandbox Code Playgroud)

这是对ideone的测试。