次の方法で共有


関数テンプレートのオーバーロード

更新 : 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 Studio .NET 2003 および Visual Studio .NET の両バージョンの Visual C++ で有効になるコードの場合は、削除します。

参照

参照

Visual C++ コンパイラの互換性に影響する変更点