- 论坛徽章:
- 0
|
10可用积分
书上和网上常常这样写(可能我理解的不太对)
1. 普通函数模板和类模板可以偏特化,但是"类函数模板"模板只能全部特化。对么?
2. 重载解析的时候,首先匹配普通函数,然后找偏特化的版本,然后找普通模板。但是为什么我的main-->f调用的不是特化的版本,而是输出了
template version呢?
- 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(2.0f,1);
- return 0;
- }
复制代码 |
最佳答案
查看完整内容
你那个模板参数上的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;}
|