C++ Coding Standards
File: skills/cpp-coding-standards/SKILL.md Reference: C++ Core Guidelines
Essential Rules
Functions
- Cheap types by value, expensive by
const&, sink parameters by value (for move) - Return structs for multiple outputs, not output parameters
- Use
constexprandnoexceptwhere applicable - Prefer pure functions
Classes
- Rule of Zero: Let compiler generate special members when possible
- Rule of Five: If managing a resource, define all five (destructor, copy/move constructor, copy/move assignment)
- Single-argument constructors:
explicit - Base class destructors: public virtual or protected non-virtual
- Use
overridefor virtual function implementations
Resource Management
unique_ptrfor exclusive ownership,shared_ptrfor shared- Raw pointer = non-owning observer
- No naked
new/delete, nomalloc/free
Expressions
- Always initialize objects at declaration
- Prefer
{}initializer syntax - Default to
const/constexpr - Use
nullptr(not0/NULL), no C-style casts, no magic numbers
Error Handling
- Custom exception types (not built-in types)
- Throw by value, catch by reference
- RAII for exception-safe code
Concurrency
- RAII locks (
scoped_lock/lock_guard), always named scoped_lockfor multiple mutexes (deadlock-free)- Always wait with a condition on condition variables
- Don't use
volatilefor synchronization
Templates (C++20)
- Constrain with concepts (
std::integral, custom concepts) - Use
usingovertypedef - Avoid template metaprogramming where
constexprsuffices
Style
enum classover plainenumunderscore_stylenaming, ALL_CAPS for macros only'\n'instead ofstd::endl- Include guards, self-contained headers, no
using namespacein headers
Quick Checklist
No raw new/delete | Objects initialized | const/constexpr by default | enum class | nullptr | No narrowing conversions | explicit constructors | Rule of Zero/Five | Concepts on templates | RAII locks | Custom exceptions | No magic numbers
Remember: Modern C++ emphasizes safety, clarity, and zero-overhead abstractions.