반응형

http://www.synch3d.com/wiki/moin/moin.cgi/template?action=LikePages


함수 템플릿

컴파일러는 각 invocation에서 사용되는 arguments의 타입에 따라서 swap의 구별된 특수화들을 인스턴스화한다: 

//template <class T> void Swap(T &a, T &b) // class와 typename에 의미의 차이는 없다. 
template <typename T> void Swap(T &a, T &b)  
{ 
 T t; t=a; a=b; b=t; 
} 

헤더와 소스로 분리하기
// swap.h 
template <typename T> void Swap( T&a, T&b); 
template <> void Swap<float>(float&a, float&b); // 이건 명시적 특수화, 확장되어 사용되는것을 미리 선언. export 키워드 없이 분리하는 법 
template <> void Swap<int>(int &a,int &b); // 이것도 마찬가지 
... 
 
// swap.cpp 
#include "swap.h" 
 
template <> void Swap<int>(int&a, int&b) // 이놈의 특수화된 템플릿 함수 
{ 
        int t; 
        t=a;a=b;b=t; 
} 
 
 
template <> void Swap<float>(float&a, float&b) // 이놈의 특수화된 템플릿 함수 
{ 
        float t; 
        t=a;a=b;b=t; 
} 

반응형

+ Recent posts