右值引用:移動(dòng)語(yǔ)義和完美轉(zhuǎn)發(fā)
指針成員與拷貝構(gòu)造
#include <iostream>
using namespace std;
class HasPtrMem {
public:
HasPtrMem(): d(new int(0)) {}
~HasPtrMem() { delete d; }
int * d;
};
int main() {
HasPtrMem a;
HasPtrMem b(a);
cout << *a.d << endl; // 0
cout << *b.d << endl; // 0
} // 析構(gòu):運(yùn)行時(shí)錯(cuò)誤,多次在同一位置調(diào)用delete
- 淺拷貝(shollow copy)
在未聲明拷貝構(gòu)造函數(shù)時(shí),編譯器也會(huì)為類生成一個(gè)淺拷貝構(gòu)造函數(shù)。
解決淺拷貝問題的方法是用戶自定義拷貝函數(shù),進(jìn)行“深拷貝”(deep copy)。
#include <iostream>
using namespace std;
class HasPtrMem {
public:
HasPtrMem(): d(new int(0)) {}
HasPtrMem(const HasPtrMem & h):
d(new int(*h.d)) {} // 拷貝構(gòu)造函數(shù),從堆中分配內(nèi)存,并用*h.d初始化
~HasPtrMem() { delete d; }
int * d;
};
int main() {
HasPtrMem a;
HasPtrMem b(a);
cout << *a.d << endl; // 0
cout << *b.d << endl; // 0
} // 正常析構(gòu)析構(gòu)
左值,右值和右值引用
在C++11中所有的值必屬于左值(lvalue)、右值(rvalue)兩者之一,右值又可以細(xì)分為純右值(prvalue, Pure RValue)、將亡值(xvalue, eXpiring Value)。
在C++11中可以取地址的、有名字的就是左值,反之,不能取地址的、沒有名字的就是右值(將亡值或純右值)。
a = b+c;
// a 左值, &a 合法
// b+c 是右值, &(b+c) 非法
純右值
- 非引用返回的函數(shù)返回的臨時(shí)變量值
- 1+3 產(chǎn)生的臨時(shí)變量
- 2, 'c', true
- 類型轉(zhuǎn)換函數(shù)的返回值
- lambda 表達(dá)式
將亡值
將亡值則是 C++11 新增的跟右值引用相關(guān)的表達(dá)式,這樣表達(dá)式通常是將要被移動(dòng)對(duì)象.
- 返回右值引用 T&& 的函數(shù)返回值
- std::move 的返回值
- 轉(zhuǎn)換為 T&& 的類型轉(zhuǎn)換函數(shù)的返回值
右值引用就是對(duì)一個(gè)右值進(jìn)行引用的類型.
通常情況下,右值不具有名字,我們只能通過引用的方式找到它.
T&& a = RetureRvalue();
/*
* a 是右值引用
* RetureRvalue 返回一個(gè)臨時(shí)變量
* a 這個(gè)右值引用 引用 RetureRvalue 返回的臨時(shí)變量
*/
右值引用和左值引用都是屬于引用類型。無(wú)論是聲明一個(gè)左值引用還是右值引用,都必須立即進(jìn)行初始化。而其原因可以理解為是引用類型本身自己并不擁有所綁定對(duì)象的內(nèi)存,只是該對(duì)象的一個(gè)別名。左值引用是具名變量值的別名,而右值引用則是不具名(匿名)變量的別名。
通常情況下,右值引用不能綁定到任何左值上.
int c;
int &&d = c; // error, 左值引用不能綁定到左值上
C++98 左值引用是否可以綁定到右值上?
T& e = RetureRvalue(); // error
const T& f = RetureRvalue(); // ok
在 C++98 中,常量左值引用就是個(gè)"萬(wàn)能"的引用類型.可以接受非常量左值、常量左值、右值對(duì)其進(jìn)行初始化.而且使用右值對(duì)其初始化的時(shí)候,常量左值引用還可以向右值引用一樣將右值的生命周期延長(zhǎng).不過相比于右值引用所引用的右值,常量左值引用所引用的右值在它的"余生"中只能是只讀的.相對(duì)的,非常量左值引用只能接受非常量左值對(duì)其初始化.
在 C++98 中使用 常量左值引用 來(lái)減少臨時(shí)對(duì)象的開銷:
#include <iostream>
using namespace std;
struct Copyable {
Copyable() {}
Copyable(const Copyable &o) {
cout << "Copied" << endl;
}
};
Copyable ReturnRvalue() { return Copyable(); }
void AcceptVal(Copyable) {}
void AcceptRef(const Copyable & cp) {}//Copyable c = std::move(cp);}
void AcceptRRef(int && i) {i+=3; cout << (char)i << endl;}
int main() {
cout << "Pass by value: " << endl;
AcceptVal(ReturnRvalue()); // 臨時(shí)值被拷貝傳入
cout << "Pass by reference: " << endl;
AcceptRef(ReturnRvalue()); // 臨時(shí)值被作為引用傳遞
AcceptRRef('c'); // 臨時(shí)值被作為引用傳遞
}
常量右值引用
const T&& crvalueref = RetureRvalue();
右值引用就是為了移動(dòng)語(yǔ)義,而移動(dòng)語(yǔ)義需要右值是可以被修改的,那么常量右值引用在移動(dòng)語(yǔ)義中就沒有用處;二來(lái)如果要引用右值且讓右值不可以更改,常量左值引用就夠了。
| 引用類型 | 非常量左值 | 常量左值 | 非常量右值 | 常量右值 | 注記 |
|---|---|---|---|---|---|
| 非常量左值引用 | Y | N | N | N | 無(wú) |
| 常量左值引用 | Y | Y | Y | Y | 全部類型,可用于拷貝語(yǔ)義 |
| 非常量右值引用 | N | N | Y | N | 用于移動(dòng)語(yǔ)義、完美轉(zhuǎn)發(fā) |
| 常量右值引用 | N | N | Y | Y | 暫無(wú)用途 |
#include <type_traits>
std::cout << is_lvalue_reference<string &&>::value;
std::cout << is_rvalue_reference<string &&>::value;
移動(dòng)語(yǔ)義
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int x) : x(x)
{
cout << "Constructor" << endl;
}
A(A& a) : x(a.x)
{
cout << "Copy Constructor" << endl;
}
A& operator=(A& a)
{
x = a.x;
cout << "Copy Assignment operator" << endl;
return *this;
}
A(A&& a) : x(a.x)
{
cout << "Move Constructor" << endl;
}
A& operator=(A&& a)
{
x = a.x;
cout << "Move Assignment operator" << endl;
return *this;
}
};
A GetA()
{
return A(1);
}
A&& MoveA()
{
return A(1);
}
int main()
{
cout << "-------------------------1-------------------------" << endl;
A a(1);
cout << "-------------------------2-------------------------" << endl;
A b = a;
cout << "-------------------------3-------------------------" << endl;
A c(a);
cout << "-------------------------4-------------------------" << endl;
b = a;
cout << "-------------------------5-------------------------" << endl;
A d = A(1);
cout << "-------------------------6-------------------------" << endl;
A e = std::move(a);
cout << "-------------------------7-------------------------" << endl;
A f = GetA();
cout << "-------------------------8-------------------------" << endl;
A&& g = MoveA();
cout << "-------------------------9-------------------------" << endl;
d = A(1);
}
/*
-------------------------1-------------------------
Constructor
-------------------------2-------------------------
Copy Constructor
-------------------------3-------------------------
Copy Constructor
-------------------------4-------------------------
Copy Assignment operator
-------------------------5-------------------------
Constructor
-------------------------6-------------------------
Move Constructor
-------------------------7-------------------------
Constructor
-------------------------8-------------------------
Constructor
-------------------------9-------------------------
Constructor
Move Assignment operator
*/
#include <iostream>
using namespace std;
class HasPtrMem {
public:
HasPtrMem(): d(new int(0)) {
cout << "Construct: " << ++n_cstr << endl;
}
HasPtrMem(const HasPtrMem & h): d(new int(*h.d)) {
cout << "Copy construct: " << ++n_cptr << endl;
}
~HasPtrMem() {
delete d;
cout << "Destruct: " << ++n_dstr << endl;
}
int * d;
static int n_cstr;
static int n_dstr;
static int n_cptr;
};
int HasPtrMem::n_cstr = 0;
int HasPtrMem::n_dstr = 0;
int HasPtrMem::n_cptr = 0;
HasPtrMem GetTemp() { return HasPtrMem(); }
int main() {
HasPtrMem a = GetTemp();
}
/*
* 程序輸出:
* Construct: 1
* Copy construct: 1
* Destruct: 1
* Copy construct: 2
* Destruct: 2
* Destruct: 3
*/
/*
* 在新的版本的編譯器程序輸出:
* Construct: 1
* Destruct: 1
*/
本示例想說(shuō)明一個(gè)問題,拷貝構(gòu)造的調(diào)用,尤其是深拷貝構(gòu)造的調(diào)用,會(huì)進(jìn)行內(nèi)存的memcpy,消耗大量資源。
C++11 的移動(dòng)語(yǔ)義(move semantics):
不會(huì)進(jìn)行拷貝構(gòu)造,只是將臨時(shí)變量“偷來(lái)”。
#include <iostream>
using namespace std;
class HasPtrMem {
public:
HasPtrMem(): d(new int(3)) {
cout << "Construct: " << ++n_cstr << endl;
}
HasPtrMem(const HasPtrMem& h) : d(new int(*h.d)) {
cout << "Copy construct: " << ++n_cptr << endl;
}
HasPtrMem(HasPtrMem && h): d(h.d) { // 移動(dòng)構(gòu)造函數(shù)
h.d = nullptr; // 將臨時(shí)值的指針成員置空
cout << "Move construct: " << ++n_mvtr << endl;
}
~HasPtrMem() {
delete d;
cout << "Destruct: " << ++n_dstr << endl;
}
int * d;
static int n_cstr;
static int n_dstr;
static int n_cptr;
static int n_mvtr;
};
int HasPtrMem::n_cstr = 0;
int HasPtrMem::n_dstr = 0;
int HasPtrMem::n_cptr = 0;
int HasPtrMem::n_mvtr = 0;
const HasPtrMem GetTemp() {
HasPtrMem h;
cout << "Resource from " << __func__ << ": " << hex << h.d << endl;
return h;
}
int main() {
const HasPtrMem && a = GetTemp();
cout << "Resource from " << __func__ << ": " << hex << a.d << endl;
// hex << a.d ===> hex(a.d)
}
/*
Construct: 1
Resource from GetTemp: 0x907ab0
Resource from main: 0x907ab0
Destruct: 1
*/
// 移動(dòng)構(gòu)造并沒有被調(diào)用,該示例無(wú)法說(shuō)明任何問題.
std::move 強(qiáng)制轉(zhuǎn)換為右值
std::move 將一個(gè)左值強(qiáng)制轉(zhuǎn)換為右值引用,繼而我們通過右值使用該值,以用于移動(dòng)語(yǔ)義。
// 等價(jià)于:
static_cast<T&&>(lvalue);
被強(qiáng)制轉(zhuǎn)化的左值,其生命周期并沒有隨著左右值的變化而改變。
#include <iostream>
class Moveable {
public:
Moveable(): i (new int(3)) {
std::cout << "Moveable" << std::endl;
}
~Moveable() { delete i; }
Moveable(const Moveable & m) : i(new int(*m.i)) {}
Moveable(Moveable && m) : i (m.i) {
m.i = nullptr;
}
int *i;
};
int main()
{
Moveable a;
Moveable c(std::move(a)); // a 為左值,強(qiáng)制轉(zhuǎn)換為右值
std::cout << *(a.i) << std::endl; // 在 std::move(a) 時(shí), a.i 就被設(shè)置為了 nullptr, 故這里運(yùn)行時(shí)錯(cuò)誤
return 0;
}