Article ID: 138567 - Last Review: July 5, 2005 - Revision: 2.1

BUG: Incorrect Function Name Binding in Function Templates

This article was previously published under Q138567

On This Page

Expand all | Collapse all

SYMPTOMS

Given a function template G whose definition includes a function call to F that does not depend on a template parameter, the compiler does not bind function names at the point of the template definition. Instead the compiler binds them when the function template is instantiated. This could cause unexpected runtime results.

CAUSE

This is a fairly recent change to the ANSI C++ working paper. The current version of the Microsoft compilers do not support this feature.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

MORE INFORMATION

Based on the ANSI C++ working paper draft (14.2.7), if a name can be bound at the point of the template definition and it is not a function called in a way that depends on a template parameter, it will be bound at the template definition point and the binding is not affected by later declarations.

Sample Code to Demonstrate Problem

   /* Compile options needed: Default
   */ 

   #include <iostream.h>

   void F(char)  { cout << "In F(char)" << endl; }

   template <class T>
   void G( T t )
   {
       F(1);   // F() is not dependent on T or t
   }

   void F(int) { cout <<"In F(int)" << endl; }

   void main()
   {
       G(2);
       G('a');
   }

   /* The output from the program */ 

   In F(int)
   In F(int)

   /* The output should be */ 

   In F(char)
   In F(char)
 
				

APPLIES TO
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • 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++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
Keywords: 
kbbug kbcpponly kbcompiler KB138567
 

Article Translations

 

Related Support Centers