模板类的静态模板字段?

Ale*_*lex 6 c++ templates

我有这个代码从windows移植到linux.

template<class T, int Size> 
class CVector {
 /* ... */
};

template<int n, int m>
class CTestClass {
public:
 enum { Size = 1 << n };
private:
 static CVector<int, Size> a; // main.cpp:19
};

template<int n, int m>
CVector<int, CTestClass<n, m>::Size> CTestClass<n, m>::a; // main.cpp:24
Run Code Online (Sandbox Code Playgroud)

它使用VS2008进行编译,但不适用于g ++ 4.3.2.我收到的错误是:

main.cpp:24:错误:冲突声明'CVector CTestClass :: alpha_to'

main.cpp:19:错误:'CTestClass <n,m> :: alpha_to'的前一个声明为'CVector <int,CTestClass <n,m> :: Size> CTestClass <n,m> :: alpha_to'

main.cpp:24:错误:声明'CVector <int,CTestClass <n,m> :: Size> CTestClass <n,m> :: alpha_to'在类之外是不定义的

有人知道如何通过g ++使其可编辑吗?

谢谢!

Geo*_*che 8

这适用于gcc 3.4和4.3以及VC8:

template<class T, int Size> 
class CVector {
 /* ... */
};

template<int n, int m>
class CTestClass {
public:
    enum { Size = 1 << n };
    typedef CVector<int, Size> Vector;
private:
    static Vector a; 
};

template<int n, int m>
typename CTestClass<n,m>::Vector CTestClass<n,m>::a;
Run Code Online (Sandbox Code Playgroud)