显式重载函数模板

#include<iostream>
using namespace std;

template <class X>void swapped(X &a,X &b)
{
 X temp;
 temp=a;
 a=b;
 b=temp;
 cout<<"This is in template"<<endl;
}

template<> void swapped<int>(int &a,int &b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
 cout<<"This is in func/n";
}

int main()
{
 int i=1,j=2;
 float x=1.1,y=1.2;
 cout<<"Original i and j are "<<i<<' '<<j<<endl;
 cout<<"Original x and y are "<<x<<' '<<y<<endl;
 swapped(i,j);
 swapped(x,y);
 cout<<"swapped i and j are "<<i<<' '<<j<<endl;
 cout<<"swapped x and y are "<<x<<' '<<y<<endl;
 return 0;
}

测试结果:

Original i and j are 1 2
Original x and y are 1.1 1.2
This is in func
This is in template
swapped i and j are 2 1
swapped x and y are 1.2 1.1
请按任意键继续. . .

阅读更多
文章标签: float class 测试
个人分类: C语言学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭