d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
53 lines
1.4 KiB
Markdown
53 lines
1.4 KiB
Markdown
---
|
||
paths:
|
||
- "**/*.cpp"
|
||
- "**/*.hpp"
|
||
- "**/*.cc"
|
||
- "**/*.hh"
|
||
- "**/*.cxx"
|
||
- "**/*.h"
|
||
- "**/CMakeLists.txt"
|
||
---
|
||
|
||
# C++ 模式
|
||
|
||
> 本文档基于 [common/patterns.md](../common/patterns.md) 扩展了 C++ 特定内容。
|
||
|
||
## RAII(资源获取即初始化)
|
||
|
||
将资源生命周期与对象生命周期绑定:
|
||
|
||
```cpp
|
||
class FileHandle {
|
||
public:
|
||
explicit FileHandle(const std::string& path) : file_(std::fopen(path.c_str(), "r")) {}
|
||
~FileHandle() { if (file_) std::fclose(file_); }
|
||
FileHandle(const FileHandle&) = delete;
|
||
FileHandle& operator=(const FileHandle&) = delete;
|
||
private:
|
||
std::FILE* file_;
|
||
};
|
||
```
|
||
|
||
## 三五法则/零法则
|
||
|
||
* **零法则**:优先使用不需要自定义析构函数、拷贝/移动构造函数或赋值运算符的类。
|
||
* **五法则**:如果你定义了析构函数、拷贝构造函数、拷贝赋值运算符、移动构造函数或移动赋值运算符中的任何一个,那么就需要定义全部五个。
|
||
|
||
## 值语义
|
||
|
||
* 按值传递小型/平凡类型。
|
||
* 按 `const&` 传递大型类型。
|
||
* 按值返回(依赖 RVO/NRVO)。
|
||
* 对于接收后即被消耗的参数,使用移动语义。
|
||
|
||
## 错误处理
|
||
|
||
* 使用异常处理异常情况。
|
||
* 对于可能不存在的值,使用 `std::optional`。
|
||
* 对于预期的失败,使用 `std::expected`(C++23)或结果类型。
|
||
|
||
## 参考
|
||
|
||
有关全面的 C++ 模式和反模式,请参阅技能:`cpp-coding-standards`。
|