模板特化(专门化)

来源:互联网 发布:java中类中的static 编辑:程序博客网 时间:2024/06/04 23:21

#include<iostream>
#include<string>
using namespace std;
template<typename T>
int max(T &a,T &b){
 if(a>b){
  return 1;
 }else if(a<b){
  return -1;
 }else{
  return 0;
 }
}

template<>
int max<double>(double &a,double &b){
 cout<<"使用特化模板"<<endl;
 if(a>b){
  return 10086;
 }else if(a<b){
  return-10086;
 }else{
  return 0;
 }
}


int main(){
 int a=10;
 int b=10;
 cout<<max(a,b)<<endl;

 double aa=10.68;
 double bb=9.36;
 cout<<max(aa,bb)<<endl;
 return 0;
}

原创粉丝点击