mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-03-30 21:53:28 +08:00
- agents: cpp-build-resolver, cpp-reviewer - commands: cpp-build, cpp-review, cpp-test - rules: cpp/ (coding-style, hooks, patterns, security, testing) - tests: 9 new hook test files with comprehensive coverage Cherry-picked from PR #436.
52 lines
1.3 KiB
Markdown
52 lines
1.3 KiB
Markdown
---
|
|
paths:
|
|
- "**/*.cpp"
|
|
- "**/*.hpp"
|
|
- "**/*.cc"
|
|
- "**/*.hh"
|
|
- "**/*.cxx"
|
|
- "**/*.h"
|
|
- "**/CMakeLists.txt"
|
|
---
|
|
# C++ Patterns
|
|
|
|
> This file extends [common/patterns.md](../common/patterns.md) with C++ specific content.
|
|
|
|
## RAII (Resource Acquisition Is Initialization)
|
|
|
|
Tie resource lifetime to object lifetime:
|
|
|
|
```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_;
|
|
};
|
|
```
|
|
|
|
## Rule of Five/Zero
|
|
|
|
- **Rule of Zero**: Prefer classes that need no custom destructor, copy/move constructors, or assignments
|
|
- **Rule of Five**: If you define any of destructor/copy-ctor/copy-assign/move-ctor/move-assign, define all five
|
|
|
|
## Value Semantics
|
|
|
|
- Pass small/trivial types by value
|
|
- Pass large types by `const&`
|
|
- Return by value (rely on RVO/NRVO)
|
|
- Use move semantics for sink parameters
|
|
|
|
## Error Handling
|
|
|
|
- Use exceptions for exceptional conditions
|
|
- Use `std::optional` for values that may not exist
|
|
- Use `std::expected` (C++23) or result types for expected failures
|
|
|
|
## Reference
|
|
|
|
See skill: `cpp-coding-standards` for comprehensive C++ patterns and anti-patterns.
|