C++ 的 copy and swap 惯用法
文章目录
- 1. 先说结论
- 2. copy and move 代码示例
- 3. 用 move_resource 代替 swap
- 4. 对异常安全性的说明
- 5. 关于 the rule of five 的说明
- C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all
1. 先说结论
对于管理资源的 class ,使用 copy and swap 惯用法主要有 3 个好处:
- 避免在拷贝赋值运算符中,因内存不足抛出异常,破坏当前的对象。即具有异常安全性。
- 精简代码,将 2 个赋值运算符函数合二为一。
- 在赋值运算符函数中,省去了检查自赋值的操作。
我在 copy and swap 惯用法基础上做了一点改进,得到 copy and move ,既保留了上面的 3 个好处,还能够提升一点代码的运行效率。
2. copy and move 代码示例
示例代码如下,主要包括 5 个步骤,使用时直接套用这 5 个步骤即可:
- 创建默认构造函数。
- 在拷贝构造函数中实现 value semantics 值语义,即对 class 所有的资源数据,都创建一份独立的拷贝。
- 创建 move_resource 用于赋值运算符函数。 当成员变量的移动较复杂时,也可以用于移动构造函数。
- 在移动构造函数中转移资源。
- 创建统一的赋值运算符函数,将拷贝赋值运算符和移动复制运算符合二为一。
#include <memory>class CopyAndMove {public:CopyAndMove() = default; // 1. 必须有默认构造函数。// 2. 在拷贝构造函数中实现值语义 value semantics,即对 class 所有的资源数据,都创建一份独立的拷贝。CopyAndMove(const CopyAndMove& other) {if (other.data_ptr_) { // 检查 nullptr*data_ptr_ = *other.data_ptr_; // 使用解引用,拷贝数据。} };// 3. 创建 move_resource 用于赋值运算符函数。 当成员变量的移动较复杂时,也可以用于移动构造函数。void move_resource(CopyAndMove&& other) {// 直接把 other 中的数据,转移到当前对象 this 。this->data_ptr_ = std::move(other.data_ptr_);}// 4. 在移动构造函数中转移资源。CopyAndMove(CopyAndMove&& other) noexcept : // 应明确标记 noexcept 。 data_ptr_{std::move(other.data_ptr_)} {// 如果涉及到较多较复杂的移动操作,也可能要调用 move_resource。// move_resource(std::move(other)); };// 5. 创建统一的赋值运算符函数,将拷贝赋值运算符和移动复制运算符合二为一。// 因为是以值传递的方式创建了 other ,所以在调用赋值运算符时,如果输入为左值,则会用拷贝构造函数// 创建 other。如果输入为右值,则会使用移动构造函数创建 other。CopyAndMove& operator=(CopyAndMove other) {// 创建 other 之后,可以直接调用 move_resource 转移其资源,无须进行自赋值验证。move_resource(std::move(other));return *this;}~CopyAndMove() = default; // 应用 the rule of five ,明确定义 5 个函数。private:// data_ptr_ 创建时即初始化,实施 RAII 原则。 std::unique_ptr<int> data_ptr_{std::make_unique<int>(888)};
};
3. 用 move_resource 代替 swap
如果使用 copy and swap 惯用法,它的赋值运算符函数可能为如下形式。
CopyAndSwap& operator=(CopyAndSwap other) {// 其实并不需要把 *this 的数据交换给 other,因为 other 没有其它作用。swap(*this, other); return *this; }
在上面的函数中,将当前对象 *this 和 other 的数据进行交换。但实际上把 *this 的数据给 other 是一步多余的操作,因为 other 没有其它作用。如果把这一步骤省去,能够提升一点效率,因此我把 swap 替换成了 move_resource 函数,直接转移 other 的资源即可。
4. 对异常安全性的说明
上面提到 copy and swap 的第一个好处是异常安全性。这是相对于传统的拷贝赋值运算符来说。一个示例代码如下。
class MyArray {public:// 传统的拷贝赋值运算符函数(不使用 copy-and-swap)MyArray& operator=(const MyArray& other) {if (this != &other) { // 自赋值检查。delete[] data_; // 先释放当前资源,即当前对象 *this 的数据已丢弃,无法恢复。size_ = other.size_;// 下面开辟新的内存空间,可能因为内存不足抛出 bad_alloc,无法复制 other 。data_ = new int[size_]; std::copy(other.data_, other.data_ + size_, data_);}return *this;}int* data_;int size_;// 下面省略了其它代码。
};
在传统的拷贝赋值运算符中,因为会先释放当前资源,如果后续发生内存不足抛出异常,就等于破坏了当前对象,并且无法恢复。
而 copy and move 因为操作顺序相反,是先用拷贝构造函数生成 other ,再转移 other 的资源。所以即使生成 other 时因为内存不足抛出异常,当前对象也不会被破坏。
5. 关于 the rule of five 的说明
上面的析构函数写成 ~CopyAndMove() = default; 是应用了 C++ Core Guidelines 中的 the rule of five 原则。
The rule of five 的意思是:把 class 中的 5 个函数捆绑使用。即如果明确定义了其中的一个,则应该把另外四个也进行明确地定义,包括使用 = default 或 = delete 的方式。
这 5 个函数是:拷贝构造函数,移动构造函数,拷贝构造运算符,移动构造运算符,析构函数。
原文如下 : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-five
C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all
Reason The semantics of copy, move, and destruction are closely related, so if one needs to be declared, the odds are that others need consideration too.
—————————— 本文结束 ——————————