8

static const是否可以使用模板参数包中的值创建一个数组?我尝试了以下代码,但 gcc 4.8.1 给出“错误:参数包未扩展”

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

1 回答 1

14

尝试:

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

里面的参数包ARRAY_OF_DIMS<N>没有被扩展的。每个不是参数的参数包sizeof...都必须扩展。

于 2013-06-11T15:52:13.843 回答