1. 程式人生 > >c++ 之 std::move 原理實現與用法總結

c++ 之 std::move 原理實現與用法總結

在C++11中,標準庫在<utility>中提供了一個有用的函式std::move,std::move並不能移動任何東西,它唯一的功能是將一個左值強制轉化為右值引用,繼而可以通過右值引用使用該值,以用於移動語義。從實現上講,std::move基本等同於一個型別轉換:static_cast<T&&>(lvalue);

std::move函式可以以非常簡單的方式將左值引用轉換為右值引用。(左值 右值 引用 左值引用)概念 https://blog.csdn.net/p942005405/article/details/84644101

  1. C++ 標準庫使用比如vector::push_back 等這類函式時,會對引數的物件進行復制,連資料也會複製.這就會造成物件記憶體的額外建立, 本來原意是想把引數push_back進去就行了,通過std::move,可以避免不必要的拷貝操作。
  2. std::move是將物件的狀態或者所有權從一個物件轉移到另一個物件,只是轉移,沒有記憶體的搬遷或者記憶體拷貝所以可以提高利用效率,改善效能.。
  3. 對指標型別的標準庫物件並不需要這麼做.

用法:

原lvalue值被moved from之後值被轉移,所以為空字串. 

//摘自https://zh.cppreference.com/w/cpp/utility/move
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
    std::string str = "Hello";
    std::vector<std::string> v;
    //呼叫常規的拷貝建構函式,新建字元陣列,拷貝資料
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";
    //呼叫移動建構函式,掏空str,掏空後,最好不要使用str
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";
    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";
}

輸出:

After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"

std::move 的函式原型定義

template <typename T>
typename remove_reference<T>::type&& move(T&& t)
{
	return static_case<typename remove_reference<T>::type&&>(t);

結構體 remove_reference 的原型,就是過載了多個結構體模板來獲取原型別 type.

/// remove_reference
template<typename _Tp>
  struct remove_reference
  { typedef _Tp   type; };
 
template<typename _Tp>
  struct remove_reference<_Tp&>
  { typedef _Tp   type; };
 
template<typename _Tp>
  struct remove_reference<_Tp&&>
  { typedef _Tp   type; }

原理實現:

 首先,函式引數T&&是一個指向模板型別引數的右值引用,通過引用摺疊,此引數可以與任何型別的實參匹配(可以傳遞左值或右值,這是std::move主要使用的兩種場景)。關於引用摺疊如下:

      公式一)X& &、X&& &、X& &&都摺疊成X&,用於處理左值

string s("hello");
std::move(s) => std::move(string& &&) => 摺疊後 std::move(string& )
此時:T的型別為string&
typename remove_reference<T>::type為string 
整個std::move被例項化如下
string&& move(string& t) //t為左值,移動後不能在使用t
{
    //通過static_cast將string&強制轉換為string&&
    return static_case<string&&>(t); 
}


      公式二)X&& &&摺疊成X&&,用於處理右值

std::move(string("hello")) => std::move(string&&)
//此時:T的型別為string 
//     remove_reference<T>::type為string 
//整個std::move被例項如下
string&& move(string&& t) //t為右值
{
    return static_case<string&&>(t);  //返回一個右值引用
}


簡單來說,右值經過T&&傳遞型別保持不變還是右值,而左值經過T&&變為普通的左值引用.

②對於static_cast<>的使用注意:任何具有明確定義的型別轉換,只要不包含底層const,都可以使用static_cast。

double d = 1;
void* p = &d;
double *dp = static_cast<double*> p; //正確
 
const char *cp = "hello";
char *q = static_cast<char*>(cp); //錯誤:static不能去掉const性質
static_cast<string>(cp); //正確 


③對於remove_reference是通過類模板的部分特例化進行實現的,其實現程式碼如下
//原始的,最通用的版本

template <typename T> struct remove_reference{
    typedef T type;  //定義T的類型別名為type
};
 
//部分版本特例化,將用於左值引用和右值引用
template <class T> struct remove_reference<T&> //左值引用
{ typedef T type; }
 
template <class T> struct remove_reference<T&&> //右值引用
{ typedef T type; }   
  
//舉例如下,下列定義的a、b、c三個變數都是int型別
int i;
remove_refrence<decltype(42)>::type a;             //使用原版本,
remove_refrence<decltype(i)>::type  b;             //左值引用特例版本
remove_refrence<decltype(std::move(i))>::type  b;  //右值引用特例版本 


總結:

std::move實現,首先,通過右值引用傳遞模板實現,利用引用摺疊原理將右值經過T&&傳遞型別保持不變還是右值,而左值經過T&&變為普通的左值引用,以保證模板可以傳遞任意實參,且保持型別不變。然後我們通過static_cast<>進行強制型別轉換返回T&&右值引用,而static_cast<T>之所以能使用型別轉換,是通過remove_refrence<T>::type模板移除T&&,T&的引用,獲取具體型別T。

 

 


參考連結:

https://blog.csdn.net/fengbingchun/article/details/52558914 

https://blog.csdn.net/cpriluke/article/details/79462388 

https://blog.csdn.net/swartz_lubel/article/details/59620868