Files
Obsidian-Main/02. PARA/03. Resources(資源)/C++17/rvalue.md
Awin Huang 1bbd66d5ce vault backup: 2022-06-08 10:55:41
Affected files:
.obsidian/workspace
02. PARA/03. Resources(資源)/C++17/rvalue.md
2022-06-08 10:55:41 +08:00

18 lines
699 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.
- 等號右邊的值
- 臨時的值,例如運算的結果
- 無法被取址address-of的物件
# rvalue參考
可以用`&&`來參考rvalue。例如
```
int c{5};
int&& rtepm {c + 3};
```
關於lvalue、rvalue的分別可以參考[Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category),或是[rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)等文章。這邊講用法跟好處。
rvalue reference可以減少無間的複製成本。例如
```cpp
std::string longStr = "qwertyuiopasdfghjklzxcbnm";
std::string anotherLong = longStr;
```
這時longStr會被複製一份anotherLong把longStr換成很大的buffer