共用方式為


函式範本的多載化

更新:2007 年 11 月

在 Visual Studio .NET 中,對於其簽章對應至樣板函式之明確特製化的函式,編譯器都會將其視為特製化來處理,即使該樣板函式之前並非加上 template<> 也一樣。現在,則會將這些函式視為非樣板的多載來處理。

在類似如下的狀況中,執行階段行為可能會變更:

// 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()
{
    f<int>(3);
    // 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)
   
    f(4);     
    // Visual C++ .NET will call template function specialization
    // because no global non-template overload defined.
    // The current compiler will call the non-template overload.
}

請注意在上述範例中,透過讓 f(int) 成為明確特製化 (而非應該是多載的明確特製化),也可以產生相同的行為。在 Visual C++ 的 Visual Studio .NET 2003 和 Visual Studio .NET 版本中,都會呼叫此特製化。

請參閱

參考

Visual C++ 編譯器的重大變更