如何根据可变参数模板的大小自动填充std :: array?

LxL*_*LxL 5 c++ templates variadic-templates c++11 c++14

在这个简化的代码中:

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...>
{
};
Run Code Online (Sandbox Code Playgroud)

我想indexes根据vars...尺寸自动填充.

示例:

Compile_Time_Array <1,3,5,2> arr1;//indexes --> [0,1,2,3]
Compile_Time_Array <8,5> arr2;   // indexes --> [0,1]
Run Code Online (Sandbox Code Playgroud)

任何的想法 ?

nos*_*sid 7

以下定义显然适用于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)>{});
Run Code Online (Sandbox Code Playgroud)

  • 但是,这并没有用索引填充数组.相反,它使用与参数包中相同的数字填充数组. (2认同)