Files
Obsidian-Main/20.02. CPP/C++17.md

39 lines
1.2 KiB
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.
- 變數宣告的方式變了
- Old: `int a = 3;`
- New: `int a {3};`
- `if`裡面可以宣告變數
```cpp
if (auto a {3}; a > b) {
// Do something
}
```
- `unique_ptr`: 無法複製的指標
- 傳統方法:
```cpp
unique_ptr<uint8_t[]> buffer = new uint8_t[256];
```
- 新方法:
```cpp
auto buffer = std::make_unique<uint8_t[]>(256);
```
- `share_ptr`: 可以複製,但要避免循環參考問題
- 透過refernce傳遞array參數
- 考慮一個帶有長度的陣列要傳到function裡面但是又希望在function面可以指定陣列長度
```cpp
double value[] { 1.0, 2.0, 3.0 }; // Error!
double value[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; // Pass!
double average(const double (&array)[5]) {
...
}
```
- 用 `std::string_view` 代替 `const std::string&`。
## Multi-Thread
### 使用`std::async`
- [C++ 使用 Async 非同步函數開發平行化計算程式教學](https://blog.gtwang.org/programming/cpp-11-async-function-parallel-computing-tutorial/)
- [std::atomic](https://en.cppreference.com/w/cpp/atomic/atomic)