Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static template field of template class?

Tags:

c++

templates

I've got this code to port from windows to 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

It compiles with VS2008, but doesn't with g++ 4.3.2. The error I receive is:

main.cpp:24: error: conflicting declaration ‘CVector CTestClass::alpha_to’

main.cpp:19: error: ‘CTestClass< n, m >::alpha_to’ has a previous declaration as ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’

main.cpp:24: error: declaration of ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’ outside of class is not definition

Does someone know how to make it compilable via g++?

Thanks!

like image 667
Alex Avatar asked Dec 30 '09 15:12

Alex


1 Answers

This works with gcc 3.4 & 4.3 as well as 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;
like image 189
Georg Fritzsche Avatar answered Oct 23 '22 05:10

Georg Fritzsche