简体   繁体   English

可变参数模板:创建编译时间数组

[英]Variadic templates : Creating a compile time array

I have a function that I want to use to create an array at compile time. 我有一个要在编译时用来创建数组的函数。

template<typename... uint32_t> 
static constexpr auto AddressArray(uint32_t... ns) { 
    return std::array<uint32_t, sizeof ...(uint32_t)>{ ns... }; 
}

when I use this code, I get a compiler error 当我使用此代码时,出现编译器错误

`compiler is out of heap space.` 

What am I doing wrong? 我究竟做错了什么?

Instead of giving a pack of types, it would be better to give a type for array and pack for values: 与其给出类型的包,不如给出数组的类型和值的包:

template<typename T, T ...vals> 
static constexpr auto AddressArray() { 
    return std::array<T, sizeof...(vals)>{ vals... }; 
}

Example usage: 用法示例:

auto array = AddressArray<int, 1, 2, 4, 5>();

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

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