이 튜토리얼에서는 C++에서 템플릿 전문화를 이해하기 위한 프로그램에 대해 논의할 것입니다.
sort()와 같은 표준 함수는 모든 데이터 유형에 사용할 수 있으며 각 데이터 유형에 대해 동일하게 작동합니다. 그러나 특정 데이터 유형(사용자 정의 포함)에 대해 함수의 특별한 동작을 설정하려는 경우 템플릿 특수화를 사용할 수 있습니다.
예
#include <iostream>
using namespace std;
template <class T>
void fun(T a) {
cout << "The main template fun(): " << a << endl;
}
template<>
void fun(int a) {
cout << "Specialized Template for int type: " << a << endl;
}
int main(){
fun<char>('a');
fun<int>(10);
fun<float>(10.14);
return 0;
} 출력
The main template fun(): a Specialized Template for int type: 10 The main template fun(): 10.14