BUG: C1001 or C2434: Using Templates in Default Arguments
  
PSS ID Number: Q142936
Article last modified on 01-23-1996
 
4.00
 
WINDOWS NT
 

-----------------------------------------------------------------------
The information in this article applies to:
 
 - The Microsoft C/C++ Compiler (CL.EXE) included with:
   Microsoft Visual C++, 32-bit Edition, version 4.0
-----------------------------------------------------------------------
 
SYMPTOMS
========
 
The compiler generates either an Internal Compiler Error C1001 or a C2434
when it uses the Minimal Rebuild option with code containing:
 
 - Classes containing member functions that have default arguments that are
   of type template class or that are members of a template class.
 
 - The Template class contains member functions that have non-trivial
   default arguments. (Non-trivial default arguments are arguments that are
   not simple constants.)
 
Even though the code is correct, the compiler generates one of these 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 will generate 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 the compiler options:
 
   /Gm  Enable Minimal Rebuild
   /GX  Enable Exception Handling
   /Zi  Create CodeView type Debug Information in a PDB file
 
RESOLUTION
==========
 
Possible workarounds are:
 
 - 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 of this article for an example.
 
 - Turn off the Minimal Rebuild (-Gm) option. To do this:
 
   1. Choose Settings from the Build Menu.
 
   2. Select the Debug Version of the project,
 
   3. Click the C/C++ Tab.
 
   4. Select Customize from the Category list.
 
   5. Clear the Enable Minimal Rebuild option selection.
 
STATUS
======
 
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article. We are researching this problem and will
post new information here in the Microsoft Knowledge Base as it becomes
available.
 
MORE INFORMATION
================
 
The following sample code demonstrates the problem and generates the
incorrect error C2434. The workaround in the comment explicitly
instantiates the template class (X<int>) before using it in a default-
argument expression.
 
Sample Code to Reproduce Problem
--------------------------------
 
/* Compile options needed: NONE
*/
  const int flag = 0;
 
  template<class T> struct X {
     void Xmf(int = flag) {}
     static int m_data;
  };
 
  template<class T> int X<T>::m_data = 1;
 
// Workaround: uncomment the explicit instantiation below
// template struct X<int> ;
 
  struct Y {
     void Ymf(int = X<int>::m_data); // this line generates a C2434
  };
 
  void main(void) {}
 
Additional reference words: 4.00 10.0
KBCategory: kbprg kbbuglist
KBSubcategory: CPPIss CLIss
 
=============================================================================
Copyright Microsoft Corporation 1996.
