花木企业网站源码,餐厅设计,上海网站 建设,做外贸要建什么网站前言
有一节我们分析了std::bind的实现原理#xff0c;本节稍作休息分析一个比较简单的std::move
原理
std::move的原理太简单了#xff0c;一句话#xff1a;就是把传进来的参数强转为右值引用类型。作用为调用移动构造函数#xff0c;或移动赋值函数。下面通过例子和代…前言
有一节我们分析了std::bind的实现原理本节稍作休息分析一个比较简单的std::move
原理
std::move的原理太简单了一句话就是把传进来的参数强转为右值引用类型。作用为调用移动构造函数或移动赋值函数。下面通过例子和代码说明。
例子
#include iostream
#include cstringusing 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;
}[mzhaic11]$ ./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 templatetypename _Tp98 constexpr typename std::remove_reference_Tp::type99 move(_Tp __t) noexcept
100 { return static_casttypename std::remove_reference_Tp::type(__t); }让我们看看remove_reference 的定义看看它是怎么脱去类型的外衣的
1402 /// remove_reference
1403 templatetypename _Tp
1404 struct remove_reference
1405 { typedef _Tp type; };
1406
1407 templatetypename _Tp
1408 struct remove_reference_Tp
1409 { typedef _Tp type; };
1410
1411 templatetypename _Tp
1412 struct remove_reference_Tp
1413 { typedef _Tp type; };原来不管你原来的类型是左值引用还是右值引用统统都定义type为脱去外衣的类型。