Files
Obsidian-Main/02. PARA/03. Resources(資源)/C++17/rvalue.md
Awin Huang 5f5e32ea88 vault backup: 2022-06-09 13:50:26
Affected files:
02. PARA/03. Resources(資源)/C++17/rvalue.md
2022-06-09 13:50:26 +08:00

45 lines
922 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
rvalue 是指:
- 等號右邊的值
- 臨時的值,例如運算的結果
- 無法被取址address-of的物件
## rvalue reference
一般的參考只能參考[[lvalue]]如下的程式是ok的
```cpp
int a = 10;
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
## 參考
- [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category)
- [rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)