languages/cpp.md
language: cpp extensions: [".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx"]
C++ — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only C++-specific rules and idioms.
PR Analyzer — C++ Risk Signals
- Raw
new/deleteoutside of smart pointer wrappers reinterpret_cast— almost always a red flag; require justification- Disabled compiler warnings (
#pragma warning(disable:...),-w) // TODO/// FIXMEnear ownership or lifetime code- Hardcoded credentials or keys in source
- Use of deprecated C-style functions:
strcpy,sprintf,gets
Code Quality — C++ Checks
- Raw owning pointers (
T*) used whereunique_ptr/shared_ptrwould express ownership shared_ptroverused whereunique_ptrsuffices — implies shared ownership unnecessarilystd::endlused in hot paths — flushes the buffer every call; prefer'\n'- Implicit conversions between signed and unsigned integers
- Virtual destructor missing on base classes with virtual methods
catch (...)swallowing all exceptions without logging or re-throwing
Security
- Flag
reinterpret_caston user-controlled data — potential type confusion - Flag raw array indexing without bounds check — use
.at()or assert bounds - Flag
std::stringdata passed to C APIs without null-termination guarantee — use.c_str() - Flag hardcoded buffer sizes — derive from
sizeofor usestd::array<T, N> - Flag
sscanf/sprintf— usestd::istringstreamorstd::format(C++20) - Flag user-controlled data used as a format string
Async / Concurrency
- Flag
std::shared_ptraccessed from multiple threads — the pointer itself is not thread-safe for write; usestd::atomic<std::shared_ptr<T>>(C++20) or external locking - Flag
std::vector/std::mapmutated from multiple threads without a mutex - Flag
std::mutexlocked twice in the same thread withoutstd::recursive_mutex— deadlock - Flag detached threads (
std::thread::detach) with no lifetime coordination - Flag
volatileused instead ofstd::atomicfor inter-thread communication
Resource Management
- Flag raw
newreturning an owning pointer — wrap immediately instd::make_uniqueorstd::make_shared - Flag
deletecalled manually outside of a destructor or smart pointer — ownership confusion - Flag RAII violations — resources acquired in constructor but not released via destructor
- Flag
std::ifstream/std::ofstreamnot checked for open failure before use - Flag exceptions thrown from destructors — causes
std::terminateif thrown during stack unwinding
Exception Handling
- Flag
catch (...)that swallows exceptions without logging or re-throwing - Flag exceptions thrown from destructors — wrap in
try/catchinside the destructor - Flag
noexcepton functions that can actually throw — causesstd::terminate - Flag exception specifications (
throw(...)) — deprecated since C++11, removed in C++17 - Flag using exceptions for control flow in performance-critical paths
Performance
- Flag pass-by-value for non-trivial types where pass-by-const-reference suffices
- Flag
std::vector::push_backin a loop withoutreservewhen size is known — repeated reallocations - Flag
std::mapused wherestd::unordered_mapwould give O(1) lookup - Flag
std::endlin loops — prefer'\n'to avoid repeated buffer flushes - Flag unnecessary copies from missing
std::moveon local temporaries being returned or passed
Idioms and Best Practices
Ownership and Lifetime
- Prefer
std::unique_ptrfor sole ownership,std::shared_ptronly for shared ownership - Prefer
std::make_unique/std::make_sharedovernew— exception-safe - Use
std::weak_ptrto breakshared_ptrcycles - Never use raw owning pointers in new code — they are for non-owning observation only
Modern C++ (17/20)
- Prefer
std::optional<T>over sentinel values or nullable pointers for optional returns - Prefer
std::variantover tagged unions - Prefer
std::string_viewoverconst std::string&for read-only string parameters - Prefer range-based
forloops over index loops where the index isn't needed - Prefer
if constexprover#ifdeffor compile-time branching
Type Safety
- Prefer
static_castover C-style casts — explicit and auditable - Avoid
reinterpret_castexcept in low-level I/O or FFI code with a comment - Use
enum classover plainenumto avoid implicit integer conversions