#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
请按任意键继续. . .