8
votes

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... };
1
If the solution is indeed correct you should mark it so (the check mark next to his answer). Also consider marking answers to some of your other answers correct if there are correct answers. - SirGuy

1 Answers

14
votes

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.