Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically fill a std::array base on sizeof a variadic template?

In this simplified code :

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...>
{
};

I want to automatically fill indexes base on the vars... size .

Example :

Compile_Time_Array <1,3,5,2> arr1;//indexes --> [0,1,2,3]
Compile_Time_Array <8,5> arr2;   // indexes --> [0,1]

Any idea ?

like image 845
uchar Avatar asked Jun 10 '14 17:06

uchar


1 Answers

The following definition apparently works with GCC-4.9 and 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)>{});
like image 116
nosid Avatar answered Nov 10 '22 00:11

nosid