在这个简化的代码中:
template <int... vars>
struct Compile_Time_Array_Indexes
{
static std::array < int, sizeof...(vars)> indexes;//automatically fill it base on sizeof...(vars)
};
template <int ... vars>
struct Compile_Time_Array :public Compile_Time_Array_Indexes<vars...>
{
};
我想根据 vars...
大小自动填充 indexes
。
示例:
Compile_Time_Array <1,3,5,2> arr1;//indexes --> [0,1,2,3]
Compile_Time_Array <8,5> arr2; // indexes --> [0,1]
有什么想法吗?
最佳答案
以下定义显然适用于 GCC-4.9 和 Clang-3.5:
template <typename Type, Type ...Indices>
auto make_index_array(std::integer_sequence<Type, Indices...>)
-> std::array<Type, sizeof...(Indices)>
{
return std::array<Type, sizeof...(Indices)>{Indices...};
}
template <int... vars>
std::array<int, sizeof...(vars)>
Compile_Time_Array_Indexes<vars...>::indexes
= make_index_array<int>(std::make_integer_sequence<int, sizeof...(vars)>{});
关于c++ - 如何根据可变参数模板的大小自动填充 std::array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147819/