The repercussions of missing an Ampersand in C++ and Rust
Pass-by-value vs const reference in C++
- Some argue
const Tby value is often valid and even faster for small or trivially copyable types; ABIs may pass such values in registers, and copying within a cache line is cheap. - Others see
const Tvsconst T&as a dangerous one-character difference that can silently cause big copies and performance bugs, and that intent is often “reference” in large types. - clang-tidy and similar tools can flag many unnecessary by-value parameters, but only conservatively; correctness and ABI specifics complicate static detection.
Tooling, pipelines, and “skill issue”
- One camp says modern C++ practice assumes heavy tooling: clang-tidy, sanitizers, multiple compilers, CI, etc.; with this setup, such mistakes “aren’t an issue.”
- Another counters that many C++ developers don’t use such tooling consistently and that blaming developers (“skill issue”) ignores systemic language pitfalls.
- Rust is praised for needing much less external tooling to avoid whole classes of bugs.
Move semantics: C++ vs Rust
- Rust’s “destructive” move (no valid moved-from object) is seen as simplifying type design: no need for hidden “empty” or “moved-from” states and fewer invariants to maintain.
- C++’s non-destructive moves require every type to define a valid moved-from state, often implying optional-like internal states and more complex destructors and methods.
- Some think this is a fundamental design flaw; others argue it’s flexible and acceptable if you treat moved-from objects as only destructible/reassignable.
References, copies, and ergonomics
- Several comments liken
TvsT&to nullable vs Option: the fact that both compile with identical calling syntax makes subtle mistakes easy and hard to see in code review. - Rust is praised for explicitness: you must write
&at call sites or.clone(), and misuse leads to compile-time errors, including use-after-move. - Some propose making C++ implicit copies rarer (e.g., explicit copy constructors) or using
ref-style keywords (as in D) to reduce ampersand-related footguns.