Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 creating static array from a parameter pack

Is it possible to create a static const array with values from template parameter pack? I tried the following code, but gcc 4.8.1 gives "error: parameter packs not expanded"

template<int... N>
struct ARRAY_OF_DIMS
{
    static constexpr size_t NDIM = sizeof...(N);
    static const int DIMS[NDIM];
};

template<int... N>
const int ARRAY_OF_DIMS<N>::DIMS[ARRAY_OF_DIMS<N>::NDIM] = { N... };
like image 616
user2052436 Avatar asked Jun 11 '13 15:06

user2052436


1 Answers

Try with:

template<int... N>
const int ARRAY_OF_DIMS<N...>::DIMS[ARRAY_OF_DIMS<N...>::NDIM] = { N... };

The parameter pack in ARRAY_OF_DIMS<N> is the one that is not being expanded. Every parameter pack that is not an argument to sizeof... has to be expanded.

like image 81
K-ballo Avatar answered Oct 24 '22 17:10

K-ballo