DOCUMENT:Q179273 11-FEB-2002 [visualc] TITLE :FIX: C2059 Initializing Static Data Member of a Template Class PRODUCT :Microsoft C Compiler PROD/VER::5.0,5.0sp1,5.0sp2,5.0sp3,6.0 OPER/SYS: KEYWORDS:kbVC500bug kbVC600bug kbNoUpdate ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 5.0sp1, 5.0sp2, 5.0sp3, 6.0 - Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 5.0sp1, 5.0sp2, 5.0sp3, 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 ------------------------------------------------------------------------------- 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 this to be a bug in the Microsoft products 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 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 Base { public: static Register r; }; template Register Base::r( "Base" ); //Cause of C2059, C2063 and C2040 // To work around the problem, comment the line above and // uncomment the following line: // Register Base::r = Register("Base"); int main() { return 0; } Additional query words: initializer ====================================================================== Keywords : kbVC500bug kbVC600bug kbNoUpdate Technology : kbVCsearch kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500SP1 kbVC500SP2 kbVC500SP3 kbVC500Search Version : :5.0,5.0sp1,5.0sp2,5.0sp3,6.0 Issue type : kbbug Solution Type : kbfix ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2002.