When to Use
- Writing new C++ code (classes, functions, templates)
- Reviewing or refactoring existing C++ code
- Making architectural decisions in C++ projects
- Enforcing consistent style across a C++ codebase
- Choosing between language features
Cross-Cutting Principles
- RAII everywhere - Bind resource lifetime to object lifetime
- Immutability by default - Start with
const/constexpr; mutability is the exception - Type safety - Use the type system to prevent errors at compile time
- Express intent - Names, types, and concepts should communicate purpose
- Minimize complexity - Simple code is correct code
- Value semantics over pointer semantics - Prefer returning by value and scoped objects
Functions
Parameter Passing
Pure Functions and constexpr
Classes
Rule of Zero
Rule of Five
Resource Management
Smart Pointer Usage
RAII Pattern
Constants & Immutability
Enumerations
Quick Reference Checklist
Before marking C++ work complete:- No raw
new/delete— use smart pointers or RAII - Objects initialized at declaration
- Variables are
const/constexprby default - Member functions are
constwhere possible -
enum classinstead of plainenum -
nullptrinstead of0/NULL - No narrowing conversions
- No C-style casts
- Single-argument constructors are
explicit - Rule of Zero or Rule of Five applied
- Base class destructors are public virtual or protected non-virtual
- Templates are constrained with concepts
- No
using namespacein headers at global scope - Headers have include guards and are self-contained
- Locks use RAII (
scoped_lock/lock_guard) - Exceptions are custom types, thrown by value, caught by reference
-
'\n'instead ofstd::endl - No magic numbers