当前位置: 首页 > news >正文

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 个好处:

  1. 避免在拷贝赋值运算符中,因内存不足抛出异常,破坏当前的对象。即具有异常安全性。
  2. 精简代码,将 2 个赋值运算符函数合二为一。
  3. 在赋值运算符函数中,省去了检查自赋值的操作。

我在 copy and swap 惯用法基础上做了一点改进,得到 copy and move ,既保留了上面的 3 个好处,还能够提升一点代码的运行效率。


2. copy and move 代码示例

示例代码如下,主要包括 5 个步骤,使用时直接套用这 5 个步骤即可:

  1. 创建默认构造函数。
  2. 在拷贝构造函数中实现 value semantics 值语义,即对 class 所有的资源数据,都创建一份独立的拷贝。
  3. 创建 move_resource 用于赋值运算符函数。 当成员变量的移动较复杂时,也可以用于移动构造函数。
  4. 在移动构造函数中转移资源。
  5. 创建统一的赋值运算符函数,将拷贝赋值运算符和移动复制运算符合二为一。
#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.


—————————— 本文结束 ——————————

http://www.lryc.cn/news/582804.html

相关文章:

  • CompareFace人脸识别算法环境部署
  • Foundry 依赖库管理实战
  • 代码详细注释:ARM-Linux字符设备驱动开发案例:LCD汉字输出改进建议开发板断电重启还能显示汉字,显示汉字位置自定义
  • 常见前端开发问题的解决办法
  • 什么是2.5G交换机?
  • 德隆专家:投资“三知道”原则
  • React Native 一些API详解
  • docker proxy
  • 容器技术入门之Docker环境部署
  • Docker企业级应用:从入门到生产环境最佳实践
  • Docker部署前后端项目完整教程(基于Spring Boot项目)
  • 【计算机组成原理】-CPU章节学习篇—笔记随笔
  • 开疆智能Profinet转DeviceNet网关连接掘场空气流量计配置案例
  • 用 Spring Boot + Redis 实现哔哩哔哩弹幕系统(上篇博客改进版)
  • RHA《Unity兼容AndroidStudio打Apk包》
  • 分享|大数据采集工程师职业技术报考指南
  • C# IIncrementalGenerator干点啥
  • N8N与Dify:自动化与AI的完美搭配
  • 基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(1)搭建框架基本雏形
  • UE5内置插件 AnimToTexture 简单入门
  • Spring Boot 项目中的多数据源配置
  • ElasticSearch集群状态查询及_cat 命令详解
  • GitHub Copilot 三种模式详解:Ask、Agent、Edit
  • 【web安全】SQLMap 参数深度解析:--risk 与 --level 详解
  • leetcode-二叉树的层序遍历-113
  • 基于Java+Maven+Testng+RestAssured+Allure+Jenkins搭建一个接口自动化框架
  • 谁主沉浮:人工智能对未来信息技术发展路径的影响研究
  • 基于 Rust 的Actix Web 框架的应用与优化实例
  • 从零构建MCP服务器:FastMCP实战指南
  • 基于物联网架构的温室环境温湿度传感器节点设计