Remove This Idiom
summary
Upgrading to an MSVC STL revision that marks over 3,000 functions with [[nodiscard]] uncovers silent bugs where code calls remove without erasing, or omits the crucial v.end and accidentally deletes one element. The concrete fix is to replace the fragile erase-remove idiom with C++20’s std::erase/std::erase_if for vector and list, which combine move and erase safely. Recommended tooling: run regexes or an clang AST matcher to find erase+remove patterns and automated-replace to std::erase_if.
AI-written summary · about
chapters (8)
- 0:00Removing idioms in C++
- 1:49Compiler upgrade case study
- 4:55Misuse of STL remove
- 8:11How remove and erase work
- 12:47Erase-remove pitfalls
- 15:58C++20 erase_if alternatives
- 17:21Finding erase-remove patterns
- 20:55Q&A
related talks
51mTypical C++, but Why?
01h 17mRefactoring Legacy C++ OOP Application Code with Functional Programming and Type Erasure Concepts
01h 25mMove Slow and Break Things - Upgrading to C++26
57mSome Things C++ Does Right
01h 03mC++ Trivial Relocation Through Time
01h 19mThis is C++: Uncompromised Performance, Undefined Behavior, & Move Semantics
43mC++: All the flavours of operators new and delete
01h 04mRemoving Needless Undefined Behavior for a Safer C++
