Modern C++ Best Practices
Modern C++ emphasizes safety, clarity, and performance. Use RAII for resource management, prefer smart pointers over raw pointers, and write expressive interfaces.
Prefer RAII and smart pointers
Manage resources using objects with deterministic destructors. Use `std::unique_ptr` for sole ownership and `std::shared_ptr` when shared ownership is required. Avoid raw `new`/`delete` in most code paths.
Value semantics
Design types with clear value semantics. Implement move constructors/assignments where copying is expensive. Pass by value for small types, and by const reference for larger ones.
Use the standard library
The standard library offers robust containers and algorithms that are well-tested and optimized. Use `std::vector`, `std::array`, and `std::algorithm` instead of hand-rolled equivalents.
Prefer algorithms over loops
Use concise, expressive algorithms which make intent clear and enable compiler optimizations.
Follow the principle of least surprise — write code that is easy to read, reason about, and maintain.