简体   繁体   English

如何制作递归编译时可变参数结构模板

[英]How to make a recursive compile time variadic struct template

Im trying to make a variadic template in c++ to create a static(non-dynamic allocation) struct defined by arguments of a template. 我试图在c ++中制作可变参数模板,以创建由模板参数定义的静态(非动态分配)结构。

I tried to google that during 3 days but I think the documentation and my ability to formulate my queries ends here. 我尝试在3天内用谷歌搜索,但是我认为文档和提出查询的能力到此为止。

This what im trying to go for: 这就是我想要去的:

myTemplate<1,5,2,8> myinstance;

myInstance would give me this: myInstance会给我这个:

myinstance { int[1], int[5], int[2], int[8] };

So how can you achive this, using variadic template in c++ ? 那么,如何使用c ++中的可变参数模板来实现这一目标呢? I know already about partial specialisation and that kind of stuff but the documentation is lacking in term of recursion. 我已经知道部分专业化和这类专业知识,但是在递归方面缺少文档。

Thanks. 谢谢。

You can use std::tuple : 您可以使用std::tuple

#include <tuple>
#include <array>

template<std::size_t... ns>
struct foo {
    std::tuple<std::array<int, ns>...> contents;
};

foo<1, 5, 2, 8> my_foo;

int main() {
    std::get<1>(my_foo.contents)[3] = 4;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM