c++ 11从参数包创建静态数组

C++11 creating static array from a parameter pack

本文关键字:创建 静态 数组 参数 包创建 c++      更新时间:2023-10-16

是否可以创建具有模板参数包值的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... };

Try with:

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

ARRAY_OF_DIMS<N>中的参数pack为未扩容的参数。每个参数包如果不是sizeof...的参数,则必须展开