很早就接触过最简单的swap函数:
#include<iostream>
using namespace std;
void swap(int&a,int&b) {
int temp(a);
a = b;
b = temp;
}
int main() {
int a = 1, b = 2;
swap(a, b);
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}
在缺省情况下(即未自定义),swap动作由标准库提供的swap算法(函数模板)完成,即:
(为了区别于swap函数模板,我自定义了一个myswap函数模板,和swap函数模板的实现完全一样)
template<typename T>
void myswap(T&a, T&b) {
T temp(a);
a = b;
b = temp;
}
看这样一个例子:
class WidgetData {
public:
//...
private:
int a, b, c;//可能有很多数据
//...
};
class Widget {
public:
Widget(const Widget&rhs);
Widget&operator=(const Widget&rhs) {
//...
*(this->p) = *rhs.p;//p所指对象的赋值
//...
}
private:
WidgetData*p;//指向一个堆对象
};
一旦要置换两个Widget对象值,我们唯一需要做的其实就是置换其p指针,但缺省的swap算法不知道这一点,它不止复制构造了一个临时的Widget对象,还进行了多次赋值操作,效率太低!
解决的一个思路是:将std::swap针对Widget全特化。
class Widget {
public:
//...
void swap(Widget&other) {
using std::swap;//这个声明很有必要:表示执行下面这句时如果没有特化的swap版本存在,就使用std内非特化的的swap
swap(this->p, other.p);//这里执行的是std内非特化的的swap
//上面两句可以合为一句std::swap(this->p, other.p);
}
//...
private:
WidgetData*p;
};
namespace std {//std::swap的特化版本
template<>//表明是对std::swap的一个全特化版本
void swap<Widget>(Widget&a, Widget&b) {
a.swap(b);//调用Widget成员函数swap(Widget&other)
}
}
完整的代码如下(自己编的例子):
版本1(全特化的版本):
#include<iostream>
using namespace std;
class WidgetData {
public:
WidgetData(int aa = 0) :a(aa) {}
void display() { cout << a << endl; }
private:
int a;
};
class Widget {
public:
Widget(int aa) :p(new WidgetData(aa)) {}
void swap(Widget&other) {
using std::swap;//这个声明很有必要:表示执行下面这句时如果没有特化的swap版本存在,就使用std内非特化的的swap
swap(this->p, other.p);//这里执行的是std内非特化的的swap
//上面两句可以合为一句std::swap(this->p, other.p);
}
void display() { p->display(); }
private:
WidgetData*p;//指向堆对象
};
namespace std {//std::swap的特化版本
template<>//表明是对std::swap的一个全特化版本
void swap<Widget>(Widget&a, Widget&b) {
a.swap(b);//调用Widget成员函数swap(Widget&other)
}
}
int main() {
Widget w1(1), w2(2);
swap(w1, w2);//调用std::swap的特化版本
w1.display();
w2.display();
system("pause");
return 0;
}
版本2(效率低的版本):
#include<iostream>
using namespace std;
class WidgetData {
public:
WidgetData(int aa=0):a(aa){}
void display() { cout << a << endl; }
private:
int a;
};
class Widget {
public:
Widget(int aa):p(new WidgetData(aa)){}
Widget(const Widget&rhs){//深拷贝
p = new WidgetData();
*p = *rhs.p;
}
Widget&operator=(const Widget&rhs) {
if (this == &rhs)return *this;
delete this->p;
this->p=new WidgetData();
*(this->p) = *rhs.p;//p所指对象的赋值
return *this;
}
void display() { p->display(); }
~Widget() { delete p; }
private:
WidgetData*p;//指向一个堆对象
};
int main() {
Widget w1(1), w2(2);
swap(w1, w2);//调用std::swap的非特化版本
w1.display();
w2.display();
system("pause");
return 0;
}
题外话
当时我突然有个很奇怪的想法,代码如下:
#include<iostream>
using namespace std;
class WidgetData {
public:
WidgetData(int aa = 0) :a(aa) {}
void display() { cout << a << endl; }
private:
int a;
};
class Widget {
public:
Widget(int aa) :p(new WidgetData(aa)) {}
void display() { p->display(); }
private:
WidgetData*p;//指向一个堆对象
};
int main() {
Widget w1(1), w2(2);
swap(w1, w2);//调用std::swap的非特化版本
w1.display();
w2.display();
system("pause");
return 0;
}
这样不就可以直接完成交换吗,为什么要搞得小题大做?后来想了想,发现不是这样的,因为实际中不可能会有调用默认复制构造函数的机会,在涉及到动态分配的情况下复制构造函数肯定是要自定义给出的,所以上述代码在实际中是不可能的。