Article ID: 240871 - Last Review: July 5, 2005 - Revision: 1.1

BUG: Explicitly Specified Template Functions Not Overloaded Correctly

This article was previously published under Q240871

On This Page

Expand all | Collapse all

SYMPTOMS

If all the template parameters are not used in function arguments or return type of a template function, the template functions are not overloaded correctly.

Please refer to the sample in the "More Information" Section for details.

CAUSE

The bug is the result of the way the compiler decorates template function names. Name Decoration uses the arguments and return type and doesn't use the explicitly specified template argument type. Therefore, all three template function instantiations receive the same decorated name, as seen in the sample code the "More Information" section.

RESOLUTION

Use dummy arguments to the function.

STATUS

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

MORE INFORMATION

Please refer to the C++ Standard, section 14.8.1, for information on explicit template argument specification.

Steps to Reproduce Behavior

The following sample code demonstrates the problem:
#include<iostream>
//#define WORKAROUND
#ifdef WORKAROUND
template <class T>
void f(T dummy = 0)
     {
     std::cout <<typeid(T).name() <<std::endl;
     }
#else
template <class T>
void f(void)
     {
     std::cout <<typeid(T).name() <<std::endl;
     }
#endif

int main(void)
     {
     f<bool>();
     f<char>();
     f<int>();
     return 0;
     }

Output :
int
int
int
				

APPLIES TO
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbbug kblangcpp kbtemplate kbstl kbcpponly kbpending kbcompiler KB240871
 

Article Translations