Overloading of Function Templates
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Overloading of Function Templates

In Visual Studio .NET, the compiler treated functions whose signature mapped to an explicit specialization of a template function (even if the function was not preceded by template<>) as a specialization. Now, such functions are treated as non-template overloads.

Run-time behavior may change in cases like:

// bc_overloading_of_function_templates.cpp
#include <stdio.h>
template<class T>
void f(T)   // called in Visual Studio .NET 2003
    printf_s("in void f(T)\n");

void f(int)   // called in Visual Studio .NET
// for identical behavior for both compiler versions, use
// template<> void 
// f<int>(int)
    printf_s("in void f(int)\n");

int main()
    // Visual C++ .NET calls template function specialization
    // because explicit template arguments were provided. 
    // The current compiler will also call specialization because 
    // explicit template arguments were provided.
    // But these will call different functions because the previous 
    // compiler explicitly specializes on int, and the current
    // compiler does not (creates non-template overload)
    // Visual C++ .NET will call template function specialization
    // because no global non-template overload defined.
    // The current compiler will call the non-template overload.

For the previous example, note that identical behavior can be achieved by making f(int) an explicit specialization instead of what should be an overload. The specialization will be called in both the Visual Studio .NET 2003 and Visual Studio .NET versions of Visual C++.

© 2015 Microsoft