【问题标题】:C++ static const array initialization in template class模板类中的 C++ 静态常量数组初始化
【发布时间】:2013-07-11 18:15:04
【问题描述】:

我有以下模板类:

template <unsigned N>
class XArray {
  static const int Xdata[N];
};

我想为我使用的每个XArray&lt;N&gt; 初始化静态常量数组,例如让XArray&lt;N&gt;::Xdata = {1, 2, 3, ..., N}。如何制作?

【问题讨论】:

  • 这个link 应该有帮助
  • 看来array 是一个不错的解决方案,谢谢

标签: c++ arrays templates static initialization


【解决方案1】:

你在你的类中声明了一个静态 const int 数组,所以你必须在类声明之外定义静态成员,就像这样:

template<unsigned N>
class XArray
{
public:
static const int array[N];
};
template<unsigned N>
const int XArray<N>::array[N] = {1,2,3,4,5};

但是你必须注意的是:当你使用这个模板时,你必须确保“N”大于你初始化数组的个数;

【讨论】:

  • 但是我们可以使用 constexpr 函数来初始化具有从 1 到 N.., 的完整数字序列的静态数组吗?
  • 我不这么认为。因为当你声明一个 const 成员时你必须给它一个初始化值,如果你想初始化一个 const 静态数组的每个元素,你必须使用 const_cast 来改变const 元素正常。
【解决方案2】:

您可以如下所示进行初始化。请参阅 inline cmets 了解我的解释。

template <unsigned N>
class XArray {
    private:
        static const int Xdata[N];
    public:
        //I added this for illustration purpose
        void print()
        {
            for (int i = 0; i < N; ++i)
            {
                std::cout << Xdata[i] << std::endl;
            }
        }
};

//you can initialize like this
//automatic size counting works with static arrays
//here I initialize with 3 elements
//make sure you don't use N < 3 anywhere
template <unsigned N>
const int XArray<N>::Xdata[] = {1,2,3};

int main(void)
{
    XArray<3> obj1; //N = 3: This is okay.
    XArray<8> obj2; //N > 3: This is okay. Remaining elements will be 0.
    XArray<2> obj3; //N < 3: This is also okay.
    obj1.print();
    obj2.print();
    obj3.print(); //but this will give compilation error

    return 0;
}

【讨论】:

    【解决方案3】:

    编辑:

    看来other question已经有人为你的问题提供了解决方案,答案和我的完全一样。

    另外,对于更通用的答案,您可以查看this question 的答案。


    代码

    如果您不介意使用 C++11 功能,那么variadic templates 可能会派上用场:

    template <unsigned ...Args>
    struct XArrayData
    {
        static const int Values[sizeof...(Args)];
    };
    
    template<unsigned N, unsigned ...Args>
    struct _XArrayGenerator
    {
        typedef typename _XArrayGenerator<N - 1, N, Args...>::Xdata Xdata;
    };
    
    template<unsigned ...Args>
    struct _XArrayGenerator<1, Args...>
    {
        typedef typename XArrayData<1, Args...> Xdata;
    };
    
    template<unsigned N>
    struct XArray
    {
        typedef typename _XArrayGenerator<N>::Xdata Xdata;
    };
    
    template <unsigned ...Args>
    const int XArrayData<Args...>::Values[sizeof...(Args)] = { Args... };
    

    说明

    XArray模板结构将数组元素的数量作为模板参数(N)。编译时使用_XArrayGenerator生成N个连续数字的模板参数列表。它以数字N开头,然后递归使用自己,直到达到1。此时,模板参数列表如下所示:

    1, 2, ..., N

    最后要做的就是将这些参数传递给XArrayData。代码的最后一行(实际数组的定义)使用参数来初始化数组。

    用法

    for (int i = 0; i < 3; ++i)
        cout << XArray<3>::Xdata::Values[i] << endl;
    

    输出:

    1
    2
    3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2017-03-24
      • 2016-10-28
      • 2017-05-26
      • 1970-01-01
      • 2019-03-25
      相关资源
      最近更新 更多