C++之std::move(移动语义)

相关系列文章

C++之std::is_object

C++之std::decay

C++模板函数重载规则细说

C++之std::declval

C++之std::move(移动语义)

C++之std::forward(完美转发)

C++之std::enable_if

C++之std::is_pod(平凡的数据)

目录

1.介绍

2.源码分析

3.优点

4.示例


1.介绍

在C++11中,标准库在中提供了一个有用的函数std::move,std::move并不能移动任何东西,它唯一的功能是将一个左值引用强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义。

2.源码分析

以VS2019为例,std::move原型定义:

template <class _Ty>
_NODISCARD constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) noexcept { // forward _Arg as movable
    return static_cast<remove_reference_t<_Ty>&&>(_Arg);
}

remove_reference_t的定义如下:

template <class _Ty>
using remove_reference_t = typename remove_reference<_Ty>::type;

进一步推导remove_reference的定义如下:

template <class _Ty>
struct remove_reference {     //原始的,最普通的版本
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty;
};

template <class _Ty>
struct remove_reference<_Ty&> {   //左值引用
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty&;
};

template <class _Ty>
struct remove_reference<_Ty&&> {  //右值引用
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty&&;
};

        首先,函数参数T&&是一个指向模板类型参数的右值引用,通过引用折叠,此参数可以与任何类型的实参匹配(可以传递左值或右值,这是std::move主要使用的两种场景)。关于引用折叠如下:

1)所有右值引用折叠到右值引用上仍然是一个右值引用。(A&& && 变成 A&&) 。
2)所有的其他引用类型之间的折叠都将变成左值引用。 (A& & 变成 A&; A& && 变成 A&; A&& & 变成 A&)。

简单来说,右值经过T&&传递类型保持不变还是右值,而左值经过T&&变为普通的左值引用。

3.优点

std::move将左值变为右值,再结合类的移动构造函数,实现所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝,从而极大地提高代码运行效率。

4.示例

    std::string old("12212412512");

	std::string new1 = std::move(old); //old变为""

	std::unique_ptr<int>  pValue1(new int{ 5 });
	std::unique_ptr<int>  pValue2 = std::move(pValue1); //pValue1清空

	std::vector<int> pInts = { 0, 1, 2, 3, 4, 5, 67, 1000 };

	std::vector<int> pInt1 = pInts; //pInt1拷贝pInts,和pInts一样

	std::vector<int> pInt2 = std::move(pInts); //pInts清空
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值