创建从可变参数模板包派生的类型的元组 [英] Creating tuple of types derived from Variadic Template Pack

查看:62
本文介绍了创建从可变参数模板包派生的类型的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定size_t值列表作为可变参数模板参数包,如何根据参数包使元类型的派生类型(例如Matrix)组成元组,使得可变参数的第n个元素生成矩阵< n,n + 1> .例如:

Given a list of size_t values as a variadic template parameter pack, how does one make a tuple of derived types (e.g. Matrix) depending on the parameter pack in such a way, that n-th element of variadic generates Matrix<n, n+1>. For example:

make_matrix_tuple<2,3,4,5>() == make_tuple( Matrix<2,3>, Matrix<3,4>, Matrix<4,5> );

如何编写带有size_t参数包的make_matrix_tuple函数?

通过派生的类型,我的意思不是继承而是依赖(?).我不确定正确的用词是什么.
解压参数包非常简单

How to write make_matrix_tuple function taking in a parameter pack of size_t?

By a type derived I do not mean inheritance but dependence (?). I'm not sure what the proper term is.
Unpacking parameter pack is simple enough

template <typename ElementType, size_t... Sizes>
void make_tuple_of_vectors() { std::tuple < std::array<ElementType, Sizes> ... > tuple; }

但是,我认为我对下一部分感到有些困惑.我正在尝试递归地从参数包中解压缩一对参数,如下所示:

However I believe I am a bit over my head where it comes to the next part. I was trying recursively unpack a pair of arguments from parameter pack like so:

template <typename Type, size_t size1, size_t size2>
struct dummy_matrix
{
    size_t SIZE1 = size1;
    size_t SIZE2 = size2;
    using type = Type;
};

template <size_t Iterator, typename ElementType, size_t T, size_t... Sizes>
struct unpack_two
{
    using type = typename unpack_two<Iterator - 1, ElementType, Sizes...>::type;
};

template<typename ElementType, size_t T, size_t T2, size_t... Sizes>
struct unpack_two<0, ElementType, T, T2, Sizes...>
{ 
    using type = dummy_matrix<ElementType, T, T2>;
};

因此, unpack_two< N,类型,大小...> :: type 给出第N个和第(N + 1)-n个矩阵类型.
因此,我坚持认为对我来说很明智的东西,但是编译器不同意.

So that unpack_two<N, Type, Sizes...>::type gives N-th and (N+1)-nth Matrix type.
With that, I'm stuck with something that appears sensible to me, yet the compiler disagrees harshly.

template <size_t... Sizes, size_t... Is>
auto
foo_impl(std::index_sequence<Is...>) {
    std::tuple < unpack_two<Is, float, Sizes ... >::type ... > tuple; 
    return tuple; 
}
template <size_t... Args>
void foo()
{
    auto vs = foo_impl<Args...>(std::make_index_sequence<sizeof...(Args)-1>{});
}
int main() { foo<6,9,12>(); }

我正在尝试解压缩unpack_two模板的 std :: size_t 大小列表,然后解压缩 std :: std :: index_sequence 的列表.make_tuple().
我会解释为什么我的尝试失败的原因,或者什至是这里的 std :: index_sequence 正确工具的解释.但是我对所提出的问题的任何解决方案都非常感兴趣.

I'm trying to unpack list of std::size_t Sizes for the unpack_two template, and then unpack std::index_sequence for the std::make_tuple().
I would appreciate explanations as to why my tries are failing, or even is the std::index_sequence right tool here. But I'm mostly interested in any solution to the posed problem.

推荐答案

如何根据参数包以这样的方式生成元组派生类型(例如Matrix),即可变参数的第n个元素生成 Matrix< n,n + 1> [?]

也许在辅助函数中使用了 constexpr std :: array ?

Maybe using a constexpr std::array in an helper function?

一个例子

#include <array>
#include <tuple>
#include <utility>

template <std::size_t, std::size_t>
struct Matrix
 { };

template <std::size_t ... Ds, std::size_t ... Is>
auto mmt_helper (std::index_sequence<Is...>)
 {
   constexpr std::array ca { Ds... };

   return std::make_tuple(Matrix<ca[Is], ca[Is+1u]>{}...);
 }

template <std::size_t ... Ds>
auto make_matrix_tuple ()
 { return mmt_helper<Ds...>(std::make_index_sequence<sizeof...(Ds)-1>{}); }

int main ()
 {
   auto mt = make_matrix_tuple<2,3,4,5>();

   using T1 = decltype(mt);
   using T2 = std::tuple<Matrix<2u, 3u>, Matrix<3u, 4u>, Matrix<4u, 5u>>;

   static_assert( std::is_same_v<T1, T2> );
 }

这篇关于创建从可变参数模板包派生的类型的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆