Practical Tips for Safer C++
summary
C++ currently allows undefined behavior (UB) that breaks reasoning and optimizations; signed integer overflow is UB while unsigned wraps, and compilers provide options like -fwrapv or sanitizers such as -fsanitize=signed-integer-overflow and unsigned-integer-overflow to detect or change semantics. Use std::strong_order for total float ordering, prefer container::at over operator[] (span gains at in C++26), and prefer high-level algorithms or checked helpers (flux offers flux::ref, flux::mut_ref, read_at/read_at_unchecked) to avoid iterator invalidation from push_back. Use constexpr + static_assert for compile-time UB checks, run UBSan/ASan/MSan/TSan, and note 29% of C++ devs report no unit tests.
AI-written summary · about
chapters (9)
- 0:00Introduction
- 2:19Defining safety
- 3:11Undefined behavior
- 4:30Disclaimers and resources
- 4:55Numeric safety
- 7:58Bounds safety
- 24:59Memory safety
- 36:15Importance of testing
- 55:11Constexpr and final thoughts
related talks
01h 13mPractical Tips for Safer C++
33mUndefined Behavior in C++: What Every Programmer Should Know and Fear
01h 04mRemoving Needless Undefined Behavior for a Safer C++
51mSecure Programming Practices in C++
01h 30mDangerous Optimizations in C and C++ Programming Languages
01h 00mint != safe && int != ℤ
01h 26mVulnerable C++
01h 04mHarnessing constexpr: a path to safer C++
