C++ Core Guidelines
Overview
These guidelines help write C++ code that is:
- Type-safe: No implicit violations of the type system
- Resource-safe: No leaks (memory, handles, locks, etc.)
- Performant: Efficient without sacrificing correctness
- Correct: Catches more logic errors at compile time
Load Reference by Scenario
Choose the reference file based on what you're doing:
| Scenario | Reference File | Rules |
|---|---|---|
| Checking memory leaks, RAII, pointers | memory-safety.md | R., ES., Con.* |
| Designing function signatures, APIs | api-design.md | I., F. |
| Designing or reviewing classes | class-design.md | C.* |
| Thread safety, data races, locks | concurrency.md | CP.* |
| Exceptions, error codes, error handling | error-handling.md | E.* |
| Templates, STL, generic code | templates.md | T., SL., Enum.* |
| Coding style, naming, philosophy | modern-style.md | P., NL., SF.* |
| Performance optimization, C compatibility | performance.md | Per., CPL., A.* |
Quick Reference
Memory Safety
// Bad: manual management
X* p = new X;
delete p;
// Good: RAII
auto p = make_unique<X>();
API Design
// Bad: unclear ownership
void process(int* data, int n);
// Good: explicit ownership and size
void process(span<const int> data);
Class Design
// Rule of zero - let compiler generate
class Widget {
unique_ptr<Impl> pImpl; // No explicit dtor/copy/move needed
};
Concurrency
// Bad: manual lock
mutex mtx;
mtx.lock();
// ...
mtx.unlock();
// Good: RAII lock
lock_guard<mutex> lock(mtx);
Code Review Checklist
When reviewing C++ code, load the appropriate reference and check:
Memory Safety → memory-safety.md
- No raw
new/delete - RAII for all resources
- No dangling pointers
- Proper const usage
API Design → api-design.md
- Interfaces are explicit
- Parameters express ownership
- Return values over output params
Class Design → class-design.md
- Class has clear invariant
- Rule of zero/five followed
- Virtual destructor for base classes
Thread Safety → concurrency.md
- No data races
- lock_guard/scoped_lock used
- No deadlocks