vault backup: 2022-06-09 13:50:26

Affected files:
02. PARA/03. Resources(資源)/C++17/rvalue.md
This commit is contained in:
2022-06-09 13:50:26 +08:00
parent 48bb4e08d6
commit 5f5e32ea88

View File

@@ -3,12 +3,41 @@ rvalue 是指:
- 臨時的值,例如運算的結果 - 臨時的值,例如運算的結果
- 無法被取址address-of的物件 - 無法被取址address-of的物件
可以用`&&`來參考rvalue。例如 ## rvalue reference
一般的參考只能參考[[lvalue]]如下的程式是ok的
```cpp ```cpp
int c{5}; int a = 10;
int&& rtepm {c + 3}; // rtepm等於c + 3的結果 int& b = a;
``` ```
但是像這樣就不行了:
```cpp
int a = 10;
int b = 5;
int& c = a + b;
```
因為`a+b`是一個rvalue臨時的值沒辦法取址所以無法參考。
但是可以用`&&`來參考rvalue。例如
```cpp
int a = 10;
int b = 5;
int&& c = a + b; // c = 15
```
而不用這樣:
```cpp
int a = 10;
int b = 5;
int r = a + b;
int& c = r;
```
了解rvalue reference之後就可以實作類別的 move constructor 跟move assignment operator。
## Move constructor
Mov
## 參考 ## 參考