#include <iostream.h>
#include <string.h>
#include <string.h>
template<class T> T max(T t1,T t2)
{
return (t1>t2?t1:t2);
}
typedef const char* pcc;
template<> pcc max(pcc s1,pcc s2)
{
return (strcmp(s1,s2)>0?s1:s2);
}
void main()
{
int n=max(4,3);
cout<<n<<endl;
const char *p=max("bbb","aaa");
cout<<p;
}
{
return (t1>t2?t1:t2);
}
typedef const char* pcc;
template<> pcc max(pcc s1,pcc s2)
{
return (strcmp(s1,s2)>0?s1:s2);
}
void main()
{
int n=max(4,3);
cout<<n<<endl;
const char *p=max("bbb","aaa");
cout<<p;
}
这段程序在devcpp下没有问题,调用的是特化之后的函数。但是在vc6下面却变成了通用模板函数。经过我的几次尝试,发现要加一点东西vc6才能认识:
#include <iostream.h>
#include <string.h>
#include <string.h>
template<class T> T max(T t1,T t2)
{
return (t1>t2?t1:t2);
}
typedef const char* pcc;
template<> pcc max(pcc s1,pcc s2)
{
return (strcmp(s1,s2)>0?s1:s2);
}
void main()
{
int n=max(4,3);
cout<<n<<endl;
const char *p=max<pcc>("bbb","aaa"); //这里,要“特别强调”pcc这个类型。
cout<<p;
}
{
return (t1>t2?t1:t2);
}
typedef const char* pcc;
template<> pcc max(pcc s1,pcc s2)
{
return (strcmp(s1,s2)>0?s1:s2);
}
void main()
{
int n=max(4,3);
cout<<n<<endl;
const char *p=max<pcc>("bbb","aaa"); //这里,要“特别强调”pcc这个类型。
cout<<p;
}
评论