非整数静态数据的类内初始化

Gun*_*iez 0 c++ static

所以我刚刚通过编译器错误了解到数组的类内初始化是无效的(为什么?).现在我想在模板类中初始化一些数组,不幸的是内容依赖于模板参数.精简的测试用例如下所示:

template<typename T>
struct A {
    T x;
    static const int len = sizeof(T);         // this is of course fine
    static const int table[4] = { 0, len, 2*len, 3*len };    //this not
}
Run Code Online (Sandbox Code Playgroud)

知道如何拉出常数阵列吗?

编辑:添加了'int'.

Geo*_*che 5

正如你没有模板那样做; 把初始化放在类的声明之外:

template<class T>
const int A<T>::table[4] = { 0, len, 2*len, 3*len };
Run Code Online (Sandbox Code Playgroud)