C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++ std::move移动语义

C++之std::move移动语义的具体实现

作者:流星雨爱编程

本文围绕C++11标准库中的std::move函数展开,介绍了其功能是将左值引用强制转化为右值引用以用于移动语义,进行了源码分析,阐述了引用折叠规则,还说明了其优点是结合移动构造函数实现所有权转移,提高代码运行效率

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清空

到此这篇关于C++之std::move移动语义的具体实现的文章就介绍到这了,更多相关C++ std::move移动语义内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文