一、关于std::move()
![在这里插入图片描述](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
原理:将一个左值强制类型转成一个纯右值
二、仿写std::move
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
template<class _Ty> struct my_remove_reference { using type = _Ty; };
template<class _Ty> struct my_remove_reference<_Ty&> { using type = _Ty; };
template<class _Ty> struct my_remove_reference<_Ty&&> { using type = _Ty; };
template<class _Ty> using my_remove_reference_t = typename my_remove_reference<_Ty>::type;
template<class _Ty> my_remove_reference_t<_Ty&&> my_move(_Ty&& arg) { return static_cast<my_remove_reference_t<_Ty>&&>(arg); }
|
注意:my_remove_reference的作用就是去除_Ty类型中的引用、右值引用特性的。若不考虑此项完全可以直接使用注释所示的代码。