Static member definition of template class fails 
Author Message
 Static member definition of template class fails


Quote:
> Hi,
> I wanted to define the member variable with a constant initialiser like
> this...

> template <class T> int C<T>::val(1);

This looks like a compiler bug... try this:

template <class T> int C<T>::val = 1;

marco



Thu, 04 Apr 2002 03:00:00 GMT  
 Static member definition of template class fails

Quote:

>Hi,

>I have a template class which has been cut down to the following minimal
>definition...
>template <class T> class C
>{
>public:
> static int val;
>};

>I wanted to define the member variable with a constant initialiser like
>this...

>template <class T> int C<T>::val(1);

>But this throws a compiler error stating that val is not a function:
>error C2059: syntax error : 'constant'
>error C2063: 'val' : not a function
>error C2040: 'val' : 'int (void)' differs in levels of indirection from
>'int'

>However, if I specialise the template, like this for example...

>template<> int C<int>::val(1);

>the compiler is quite happy.

>Note that the both definitions work fine under the HP aCC compiler.
>I have tried this on MSVC 5, 6 and 6+service pack 3, with the same problem.

>Is this a compiler bug and is there a fix/workaround?

It's a bug. Here's a workaround:

 template <class T> int C<T>::val = 1;

--
Doug Harrison

Visual C++ MVP



Thu, 04 Apr 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Static member definition of template class fails

2. template functions in template classes declared outside the class definition

3. template template parameter and static var definition

4. template classes with static members

5. static data member of template class

6. Initialization of static data member of template class

7. template classes with static members

8. Static member variables of template class

9. static template class member variable in DLL

10. static members in template class across DLL's

11. template classes with static members

12. Using a class as a template argument inside the definition of the class

 

 
Powered by phpBB® Forum Software