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

Modern C++ std::move的实现原理

前言

有一节我们分析了std::bind的实现原理,本节稍作休息分析一个比较简单的std::move

原理

std::move的原理太简单了,一句话:就是把传进来的参数强转为右值引用类型。作用为调用移动构造函数,或移动赋值函数。下面通过例子和代码说明。

例子

#include <iostream>
#include <cstring>using namespace std;struct Thing {Thing(){cout << "default construct\n";}// Copy operatorThing(const Thing& other){cout << "copy constructor\n";}// Move constructorThing(Thing&& other) noexcept{cout << "move constructor\n";}// assignment operatorThing& operator=(const Thing& rhs) {cout << "copy operator\n";return *this;}// move assignment operatorThing& operator=(Thing&& rhs) noexcept {cout << "move operator\n";return *this;}// destructor necessary since we are working in dangerous new/delete territory~Thing() noexcept {cout << "destructor " << "\n";}
};
int main()
{cout << "main::constructing a\n";Thing a;cout << "main::moving a to newly constructed c\n";Thing c(std::move(a));Thing x = std::move(c);cout << ">main::thing y\n";Thing y;y = std::move(x);return 0;
}

[mzhai@c++11]$ ./a.out
main::constructing a
default construct
main::moving a to newly constructed c
move constructor //Thing c(std::move(a));
move constructor //Thing x = std::move©;
main::thing y
default construct
move operator //y = std::move(x);
destructor
destructor
destructor
destructor

a, c, x虽然都是左值,但std::move却把它们强转成了右值引用,从而调用了move语义的函数而不是copy语义的。

std::move源码

 92   /**93    *  @brief  Convert a value to an rvalue.94    *  @param  __t  A thing of arbitrary type.95    *  @return The parameter cast to an rvalue-reference to allow moving it.96   */97   template<typename _Tp>98     constexpr typename std::remove_reference<_Tp>::type&&99     move(_Tp&& __t) noexcept
100     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

让我们看看remove_reference 的定义,看看它是怎么脱去类型的外衣的:

1402   /// remove_reference
1403   template<typename _Tp>
1404     struct remove_reference
1405     { typedef _Tp   type; };
1406
1407   template<typename _Tp>
1408     struct remove_reference<_Tp&>
1409     { typedef _Tp   type; };
1410
1411   template<typename _Tp>
1412     struct remove_reference<_Tp&&>
1413     { typedef _Tp   type; };

原来不管你原来的类型是左值引用还是右值引用,统统都定义type为脱去外衣的类型。

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

相关文章:

  • 爬虫工作量由小到大的思维转变---<第四十章 Scrapy Redis 实现IP代理池管理的最佳实践>
  • C# 实现 XOR 密码
  • 【Web前端开发基础】CSS3之空间转换和动画
  • Go实现一个简单的烟花秀效果(附带源码)
  • 【数学建模】插值与拟合
  • 全卷积网络:革新图像分析
  • ubuntu20.04 格式化 硬盘 扩展硬盘GParted
  • docker的资源限制(cgroup)
  • ChatGPT与文心一言:应用示例与体验比较
  • 紫光展锐T760_芯片性能介绍_展锐T760安卓核心板定制
  • 从动力系统研究看当今数学界
  • 【征服redis15】分布式锁的功能与整体设计方案
  • MATLAB中实现机械臂逆运动学求解的方法之一是使用阻尼最小二乘法
  • 2024.1.24 GNSS 学习笔记
  • 2024-01-22(MongoDB)
  • 无人机航迹规划(六):七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划(提供MATLAB代码)
  • 《WebKit 技术内幕》学习之十二(2):安全机制
  • 算法优化:LeetCode第122场双周赛解题策略与技巧
  • IDEA导出jar
  • Win10/11中VMware Workstation设置网络桥接模式
  • html Canvas粒子文字特效
  • @JsonFormat失效,被jackson自定义配置覆盖
  • SaaS系统如何助力企业数字化转型
  • nginx配置内网代理,前端+后端分开配置
  • i18n多国语言Internationalization的动态实现
  • C++笔记(二)
  • 【技能---构建github中SSH密钥的流程】
  • linux-centos服务器离线安装yapi(包含nodejs、mongodb、yapi、pm2离线安装)
  • 手撕重采样,考虑C的实现方式
  • 网络安全产品之认识入侵防御系统