更新时间:2023-11-30 18:08:04
您可以做更多不仅仅是扩展一个可变参数包作为一个简单的列表:一个表达式。因此,您可以将 m_sequences
作为向量的元组,而不是元素的元组:
You can do more than just expand a variadic parameter pack as a plain list: you can expand an expression too. You can therefore have m_sequences
be a tuple of vectors rather than a tuple of the elements:
template <typename... T>
struct sequences
{
std::tuple<std::vector<T>...> m_sequences;
};
您还可以使用参数包来拾取适当的元素:
You can also do nifty tricks with parameter packs to pick the appropriate element from the vector:
template<size_t ... Indices> struct indices_holder
{};
template<size_t index_to_add,typename Indices=indices_holder<> >
struct make_indices_impl;
template<size_t index_to_add,size_t...existing_indices>
struct make_indices_impl<index_to_add,indices_holder<existing_indices...> >
{
typedef typename make_indices_impl<
index_to_add-1,
indices_holder<index_to_add-1,existing_indices...> >::type type;
};
template<size_t... existing_indices>
struct make_indices_impl<0,indices_holder<existing_indices...> >
{
typedef indices_holder<existing_indices...> type;
};
template<size_t max_index>
typename make_indices_impl<max_index>::type make_indices()
{
return typename make_indices_impl<max_index>::type();
}
template <typename... T>
struct sequences
{
std::tuple<std::vector<T>...> m_sequences;
template<size_t... Indices>
std::tuple<T...> get_impl(size_t pos,indices_holder<Indices...>)
{
return std::make_tuple(std::get<Indices>(m_sequences)[pos]...);
}
std::tuple<T...> get(size_t pos)
{
return get_impl(pos,make_indices<sizeof...(T)>());
}
};