stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了.
- 左值和右值的區別
int a = 0; // a是左值,0是右值 int b = rand(); // b是左值,rand()是右值
直觀理解:左值在等號左邊,右值在等號右邊
深入理解:左值有名稱,可根據左值獲取其內存地址,而右值沒有名稱,不能根據右值獲取地址。
2. 引用疊加規則
左值引用A&和右值引用A&& 可相互疊加
A& + A& = A& A& + A&& = A& A&& + A& = A& A&& + A&& = A&&
舉例示例,void foo(T&& x)中,如果T是int&, x為左值語義,如果T是int&&, x為右值語義
3. 為什么要使用std::move
如果類X包含一個指向某資源的指針,在左值語義下,類X的賦值構造函數如下:
X::X(const X& other) { // .... // 銷毀資源 // 復制other的資源,並使指針指向它 // ... }
應用代碼如下,其中,tmp被賦給a之后,便不再使用。
X tmp; // ...經過一系列初始化... X a = tmp;
如上,執行過程按照時間順序如下: 首先執行一次默認構造函數(tmp申請資源),再執行一次復制構造函數(a復制資源), 最后退出作用域時再執行一次析構函數(tmp釋放資源)。既然tmp遲早要被析構掉,在執行復制構造函數的時候,a能不能將tmp的資源“偷“”過來,直接為我所用?
X::X(const X& other) { // 交換this和other的資源 }
這樣可以減少一次資源的創建和釋放。這就是std::move所要實現的。
4. std::move的實現
std::move用於強制將左值轉化為右值。其實現方式如下:
template<class T> typename remove_reference<T>::type&& std::move(T&& a) noexcept { typedef typename remove_reference<T>::type&& RvalRef; return static_cast<RvalRef>(a); }
當a為int左值(右值)時,根據引用疊加原理,T為int&, remove_reference<T> = int, std::move返回類型為int&&,即右值引用
5. std::move的使用
#include <utility> #include <iostream> #include <string> #include <vector> void foo(const std::string& n) { std::cout << "lvalue" << std::endl; } void foo(std::string&& n) { std::cout << "rvalue" << std::endl; } void bar() { foo("hello"); // rvalue std::string a = "world"; foo(a); // lvalue foo(std::move(a)); // rvalue } int main() { std::vector<std::string> a = {"hello", "world"}; std::vector<std::string> b; b.push_back("hello"); b.push_back(std::move(a[1])); std::cout << "bsize: " << b.size() << std::endl; for (std::string& x: b) std::cout << x << std::endl; bar(); return 0; }
6. 為什么要使用std::forward
首先看下面這段代碼:
#include <utility> #include <iostream> void bar(const int& x) { std::cout << "lvalue" << std::endl; } void bar(int&& x) { std::cout << "rvalue" << std::endl; } template <typename T> void foo(T&& x) { bar(x); bar(std::forward<T>(x)); } int main() { int x = 10; foo(x); foo(10); return 0; }
執行foo(10):首先進入函數foo, 執行bar(x), 輸出"lvalue"。這里有點不合常理,10明明是一個右值,為什么這里輸出"lvalue"呢?這是因為10只是作為一個foo的右值參數,但是在foo函數內部,x卻是一個有名字的變量,因此10是bar(x)的左值參數。但是我們想延續10的左值語義,怎么辦呢?std::forward就派上了用場。
總而言之,std::forward的目的就是保持std::move的語意。
7. std::forwar的實現
template<typename T, typename Arg> shared_ptr<T> factory(Arg&& arg) { return shared_ptr<T>(new T(std::forward<Arg>(arg))); }
template<class S> S&& forward(typename remove_reference<S>::type& a) noexcept { return static_cast<S&&>(a); }
X x;
factory<A>(x);
如果factory的輸入參數是一個左值 => Arg = X& => std::forward<Arg> = X&, 這種情況下,std::forward<Arg>(arg)仍然是左值。
相反,如果factory輸入參數是一個右值 => Arg = X => std::forward<Arg> = X, 這種情況下,std::forward<Arg>(arg)是一個右值。
8. std::forward的使用
直接上碼,如果前面都懂了,相信這段代碼的輸出結果也能猜個八九不離十了。
#include <utility> #include <iostream> void overloaded(const int& x) { std::cout << "[lvalue]" << std::endl; } void overloaded(int&& x) { std::cout << "[rvalue]" << std::endl; } template <class T> void fn(T&& x) { overloaded(x); overloaded(std::forward<T>(x)); } int main() { int i = 10; overloaded(std::forward<int>(i)); overloaded(std::forward<int&>(i)); overloaded(std::forward<int&&>(i)); fn(i); fn(std::move(i)); return 0; }