關鍵字:C++11,右值引用,rvalue,std::move,VS 2015ios
OS:Windows 10函數
右值引用(及其支持的Move語意和完美轉發)是C++0x加入的最重大語言特性之一。從實踐角度講,它可以完美解決C++中長久以來爲人所詬病的臨時對象效率問題。從語言自己講,它健全了C++中的引用類型在左值右值方面的缺陷。從庫設計者的角度講,它給庫設計者又帶來了一把利器。從庫使用者的角度講,不動一兵一卒即可以得到「免費的」效率提高… this
下面用實例來深刻探討右值引用。spa
#include "stdafx.h" #include <iostream> class A { public: A() { std::cout << "Constructor" << std::endl; } A(const A&) { std::cout << "Copy Constructor" << std::endl; } ~A() {} }; static A getA() { A a; return a; } int main() { A a = getA(); return 0; }
運行以上代碼,輸出結果以下:設計
Constructor
Copy Constructor
能夠看到A的構造函數調用一次,拷貝構造函數調用了一次,構造函數和拷貝構造函數是消耗比較大的,這裏是否能夠避免拷貝構造?C++11作到了這一點。code
#include "stdafx.h" #include <iostream> class A { public: A() { std::cout << "Constructor" << std::endl; } A(const A&) { std::cout << "Copy Constructor" << std::endl; } A(const A&&) { std::cout << "Move Constructor" << std::endl; } ~A() {} }; static A getA() { A a; return a; } int main() { A a = getA(); return 0; }
運行以上代碼,輸出結果:對象
Constructor
Move Constructor
這樣就沒有調用拷貝構造函數,而是調用移動構造。這裏並無看到移動構造的優勢。blog
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA(); std::cout << "================================================" << std::endl; A a1(a); return 0; }
運行以上代碼,輸出結果:get
A Constructor ================================================ A Move Constructor ================================================ B Constructor A Copy Constructor
「 A a = getA();」調用的是A的移動構造,「 A a1(a); 」調用的是A的拷貝構造。A的拷貝構造須要對成員變量B進行深拷貝,而A的移動構造不須要,很明顯,A的移動構造效率高。io
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) noexcept : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA(); std::cout << "================================================" << std::endl; A a1(a); std::cout << "================================================" << std::endl; A a2(std::move(a1)); return 0; }
運行以上代碼,輸出結果:
A Constructor ================================================ A Move Constructor ================================================ B Constructor A Copy Constructor ================================================ A Move Constructor
「 A a2(std::move(a1));」將a1轉換爲右值,所以a2調用的移動構造而不是拷貝構造。
#include "stdafx.h" #include <iostream> #include <vector> class B { public: B() {} B(const B&) { std::cout << "B Constructor" << std::endl; } }; class A { public: A(): m_b(new B()) { std::cout << "A Constructor" << std::endl; } A(const A& src) : m_b(new B(*(src.m_b))) { std::cout << "A Copy Constructor" << std::endl; } A(A&& src) : m_b(src.m_b) { src.m_b = nullptr; std::cout << "A Move Constructor" << std::endl; } A& operator=(const A& src) noexcept { if (this == &src) return *this; delete m_b; m_b = new B(*(src.m_b)); std::cout << "operator=(const A& src)" << std::endl; return *this; } A& operator=(A&& src) noexcept { if (this == &src) return *this; delete m_b; m_b = src.m_b; src.m_b = nullptr; std::cout << "operator=(const A&& src)" << std::endl; return *this; } ~A() { delete m_b; } private: B* m_b; }; static A getA() { A a; std::cout << "================================================" << std::endl; return a; } int main() { A a = getA();//移動構造 std::cout << "================================================" << std::endl; A a1(a);//拷貝構造 std::cout << "================================================" << std::endl; A a2(std::move(a1));//移動構造 std::cout << "================================================" << std::endl; a2 = getA();//移動賦值 std::cout << "================================================" << std::endl; a2 = a1;//拷貝賦值 return 0; }
運行以上代碼,輸出結果:
A Constructor ================================================ A Move Constructor ================================================ B Constructor A Copy Constructor ================================================ A Move Constructor ================================================ A Constructor ================================================ A Move Constructor operator=(const A&& src) ================================================ B Constructor operator=(const A& src)
總之儘可能給類添加移動構造和移動賦值函數,而減小拷貝構造和拷貝賦值的消耗。 移動構造,移動賦值要加上noexcept,用於通知標準庫不拋出異常。
參考連接: