#include <cstdio>
#include <cstring>
template <class T>
T mymax(const T t1, const T t2)
{
return t1 < t2 ? t2 : t1;
}
//模板特化
//特化為絕對類型
//上述定義中template < >告訴編譯器這是一個特化的模板。并且在聲明特化模板之前一定要有非特化的聲明!并且兩個類的名字是一樣的!
//特化的模板必須放在非特化的模板的之后
//否則編譯器死給你看,如下:
//'const char *mymax(const char *,const char *)' is not a specialization of a function template
template<>
const char* mymax(const char* t1,const char* t2)
{
return (strcmp(t1,t2) < 0) ? t2 : t1;
}
/*
非模板函數
非模板函數具有最高的優先權
const char* mymax(const char* t1,const char* t2)
{
return (strcmp(t1,t2) < 0) ? t2 : t1;
}
*/
int main()
{
int highest = mymax(5,10);
char c = mymax('a', 'z');
const char* p1 = "hello";
const char* p2 = "world";
const char* p = mymax(p1,p2);
return 0;
}