连接两个类型可变的模板包

Concatenate two typed variadic template packs

考虑这个结构

template<std::size_t... a>
struct A{};

如何连接两个类型可变的模板参数?

concat<A<1,2,3>, A<4,5,6,7>> // should be of type A<1,2,3,4,5,6,7>

您可以使用处理 A.

实例化的专门化来实现它
template<typename...>
struct concat_impl;   // just a declaration, to allow for specializations

template<std::size_t... s1, std::size_t... s2>
struct concat_impl<A<s1...>, A<s2...>> {        // specialize   
    using type = A<s1..., s2...>;               // concatenate
};

和一个方便的别名,以避免在任何地方都说 typename

template<typename A1, typename A2>
using concat = typename concat_impl<A1,A2>::type;

这是一个demo