免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3009 | 回复: 10
打印 上一主题 下一主题

模板偏特化的两个疑问,运行结果和我期待的不一样 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-05 18:01 |只看该作者 |倒序浏览
10可用积分
书上和网上常常这样写(可能我理解的不太对)
1. 普通函数模板和类模板可以偏特化,但是"类函数模板"模板只能全部特化。对么?
2. 重载解析的时候,首先匹配普通函数,然后找偏特化的版本,然后找普通模板。但是为什么我的main-->f调用的不是特化的版本,而是输出了
template version呢?

  1. template<class T1,class T2>
  2. double f(T1 t1,T2 t2){
  3.     printf("template version\n");
  4.     return t1+t2;
  5. }
  6. template<class T1,int>
  7. double f(T1 t1, int i){
  8.     printf("partial specialization\n");
  9.     return t1+i;
  10. }
  11. int main(int argc, char* const argv[]) {
  12.     f(2.0f,1);
  13.     return 0;
  14. }
复制代码

最佳答案

查看完整内容

你那个模板参数上的int没有用,只是语法上通过模板参数用常量是这样用的:templatedouble f(T1 t1,T2 t2){ printf("template version\n"); return t1+t2;}templatedouble f(T1 t1, int i){ printf("partial specialization NUM+i=%d\n", NUM+i); return t1+i;}int main(int argc, char* const argv[]) { f(2.0f,1); return 0;}

论坛徽章:
0
2 [报告]
发表于 2012-06-05 18:01 |只看该作者
你那个模板参数上的int没有用,只是语法上通过

模板参数用常量是这样用的:
template<class T1,class T2>
double f(T1 t1,T2 t2){
  printf("template version\n");
  return t1+t2;
}
template<class T1, int NUM>
double f(T1 t1, int i){
  printf("partial specialization NUM+i=%d\n", NUM+i);
  return t1+i;
}
int main(int argc, char* const argv[]) {
  f<float, 10>(2.0f,1);
  return 0;
}

论坛徽章:
0
3 [报告]
发表于 2012-06-05 18:20 |只看该作者
template<class T1,class T2>
double f(T1 t1,T2 t2){
  printf("template version\n");
  return t1+t2;
}
template<class T1>
double f(T1 t1, int i){
  printf("partial specialization\n");
  return t1+i;
}
int main(int argc, char* const argv[]) {
  f(2.0f,1);
  return 0;
}

论坛徽章:
0
4 [报告]
发表于 2012-06-05 18:42 |只看该作者
shanehan 发表于 2012-06-05 18:20
template
double f(T1 t1,T2 t2){
  printf("template version\n");


多谢,你的这个版本就是我想要的。

不过我的问题是,我写的那个版本:
template<class T1,int>
算是模板偏特化么?

int是一个已知类型啊。

论坛徽章:
0
5 [报告]
发表于 2012-06-05 18:46 |只看该作者
donet8 发表于 2012-06-05 18:42
多谢,你的这个版本就是我想要的。

不过我的问题是,我写的那个版本:


也算是,不过应该写成这样,才能得到你要的结果:
template<class T1,class T2>
double f(T1 t1,T2 t2){
  printf("template version\n");
  return t1+t2;
}
template<class T1, int>
double f(T1 t1, int i){
  printf("partial specialization\n");
  return t1+i;
}
int main(int argc, char* const argv[]) {
  f<float, 1>(2.0f,1);
  return 0;
}

论坛徽章:
0
6 [报告]
发表于 2012-06-05 18:46 |只看该作者
donet8 发表于 2012-06-05 18:42
多谢,你的这个版本就是我想要的。

不过我的问题是,我写的那个版本:


也算是,不过应该写成这样,才能得到你要的结果:
template<class T1,class T2>
double f(T1 t1,T2 t2){
  printf("template version\n");
  return t1+t2;
}
template<class T1, int>
double f(T1 t1, int i){
  printf("partial specialization\n");
  return t1+i;
}
int main(int argc, char* const argv[]) {
  f<float, 1>(2.0f,1);
  return 0;
}

论坛徽章:
0
7 [报告]
发表于 2012-06-05 18:47 |只看该作者
donet8 发表于 2012-06-05 18:42
多谢,你的这个版本就是我想要的。

不过我的问题是,我写的那个版本:


也算是,不过应该写成这样,才能得到你要的结果:
template<class T1,class T2>
double f(T1 t1,T2 t2){
  printf("template version\n");
  return t1+t2;
}
template<class T1, int>
double f(T1 t1, int i){
  printf("partial specialization\n");
  return t1+i;
}
int main(int argc, char* const argv[]) {
  f<float, 1>(2.0f,1);
  return 0;
}

论坛徽章:
0
8 [报告]
发表于 2012-06-05 18:51 |只看该作者
我怎么会回复了三次,

论坛徽章:
0
9 [报告]
发表于 2012-06-05 18:55 |只看该作者
回复 3# donet8
这不能算是偏特化
该模板的模板参数为无类型模板参数(即编译时为已知的整数值)
具体可参考bitset<int> 类模板

   

论坛徽章:
0
10 [报告]
发表于 2012-06-05 19:21 |只看该作者
template<class T1,int>
double f(T1 t1, int i);
这个不是特化,是另外一个同名的函数模版声明。
template<class T1, int>里的int是模版的非类型参数,是模版的形参,它对应的实参是int类型的常量表达式而不是一个类型。
你可以指定class T1和int对应的实参来得到这个模版的实例,类似 f<float, 10>。
其实偏特化是类模版的,函数模版要么全特化要么实例化要么就重载……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP