#include <iostream>
template<typename T>
void exchangeTwoNumber(T &t1, T &t2);
void test();
int main()
{
int a = 10, b = 20;
double q = 10.1, w = 20.2;
char z = 'z', x = 'x';
std::cout << "交换前的 a = " << a << ", b = " << b << std::endl;
std::cout << "交换前的 q = " << q << ", w = " << w << std::endl;
std::cout << "交换前的 z = " << z << ", x = " << x << std::endl;
exchangeTwoNumber(a, b);
exchangeTwoNumber(q, w);
exchangeTwoNumber(z, x);
test();
std::cout << std::endl;
std::cout << std::endl;
std::cout << "交换后的 a = " << a << ", b = " << b << std::endl;
std::cout << "交换后的 q = " << q << ", w = " << w << std::endl;
std::cout << "交换后的 z = " << z << ", z = " << x << std::endl;
}
template<typename T>
void exchangeTwoNumber(T &t1, T &t2) //通用的函数模板
{
t1 = t1 + t2;
t2 = t1 - t2;
t1 = t1 - t2;
}
void test() //异或的方式,变形成函数模板,编译不通过
{
int a = 1, b = 2;
a ^= b;
b ^= a;
a ^= b;
std::cout << "...a=" << a << ", b=" << b << std::endl;
}