正文

vc6中关于模板显式特化问题的解决2006-08-04 16:07:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/cracker007/17243.html

分享到:

#include <iostream.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;}   这段程序在devcpp下没有问题,调用的是特化之后的函数。但是在vc6下面却变成了通用模板函数。经过我的几次尝试,发现要加一点东西vc6才能认识: #include <iostream.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;}

阅读(2665) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册