Microsoft KB Archive/140188

= FIX: C1001 or C2434: Using Templates in Default Arguments =

Article ID: 140188

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional 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 Q140188



SYMPTOMS
The compiler generates either an internal compiler error C1001 or a C2434 error when using the Minimal Rebuild option with code that contains:
 * Classes that contain member functions that have default arguments of type template class or member of template class.
 * A Template class that contains member functions that have non trivial default arguments (arguments that are not simple constants).

Even though the code is correct, the compiler generates one of the following two errors:

fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 899)

-or-

error C2434: cannot convert default argument from 'type1' to 'type2'



CAUSE
The error occurs when processing the non-trivial default argument of a member function of a class that requires instantiating a template class for the first time.

The error occurs when the compiler is forced to instantiate a template class for the first time in a default-argument expression because it needs to record information about the class for Minimal Rebuild purposes.

One function is destroying data required by a function that is further down the call stack. The compiler generates a syntax error depending on how it interprets the destroyed data. If the compiler cannot make sense of the destroyed data, it will generate an Internal Compiler Error C1001.

Depending on the code, the errors C1001 or C2434 may only occur when using one or more of these compiler options:

  /Gm  Enable Minimal Rebuild /GX Enable Exception Handling /Zi Create CodeView type Debug Information in a PDB file



RESOLUTION

 * Re-order the code so that the first instantiation of the template class does not occur in a default-argument expression. Make sure you have provided definitions for all the member functions of the class template. See the "More Information" section for an example. -or-


 * Turn off the Minimal Rebuild (-Gm) option. To do this:


 * On the Build menu, click Settings.
 * Select the Debug Version of the project.
 * Click the C/C++ tab.
 * Select Customize from the Category list.
 * Clear the Enable Minimal Rebuild option.



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 generates the incorrect error C2434. The workaround in the comment below is to explicitly instantiate the template class (X ) before using it in a default- argument expression.

Sample Code
/* Compile options needed: NONE const int flag = 0;

template struct X { void Xmf(int = flag) {} static int m_data; };

template int X::m_data = 1;

// Workaround: uncomment this explicit instantiation: // template struct X ;

struct Y { void Ymf(int = X ::m_data); // this line generates a C2434 };

void main(void) {}

Additional query words: kbVC400bug 10.00 10.10 10.20

Keywords: kbbug kbfix kbnoupdate kbcpponly kbcompiler KB140188

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.