다음을 통해 공유


함수 템플릿의 오버로드

업데이트: 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++ 컴파일러의 주요 변경 사항