Microsoft KB Archive/179273

From BetaArchive Wiki
Knowledge Base


You receive a C2059 syntax error when you initialize a static data member of a template class

Article ID: 179273

Article Last Modified on 6/2/2005



APPLIES TO

  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q179273

SYMPTOMS

The compiler generates the following errors when compiling the sample code shown below (the code initializes a static data member of a template class):

C2059: syntax error : syntax error : 'string'
C2063: 'identifier' : 'r' : not a function
C2040: 'operator' : 'r' : 'class Register (void)' differs in levels of indirection from 'class Register'

RESOLUTION

To work around this problem, use the assignment operator when initializing the static data member of a template class.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.


MORE INFORMATION

The following sample code demonstrates the problem and the workaround:

   /* Compile options needed: none
   */ 

   #include <stdio.h>

   class Register {
   public:

       Register( const char* class_name )
       { printf("In Register ctor for class %s.\n", class_name); }
       ~Register()
       { printf("In Register dtor.\n"); }

   };

   template<class T>
   class Base {
   public:

       static Register r;

   };


  template<class T>
   Register Base<T>::r( "Base<T>" ); //Cause of C2059, C2063 and C2040

   // To work around the problem, comment the line above and
   // uncomment the following line:
   // Register Base<T>::r = Register("Base<T>");

   int main() {
       return 0;
   }
                


Additional query words: initializer

Keywords: kbtshoot kbbug kbfix kbnoupdate KB179273