From Jason Turner
Watch on YouTube

Summary

This episode focuses on why using reinterpret_cast in C++ is problematic and should be avoided. Jason explains that reinterpret_cast typically results in undefined behavior because it attempts to treat a memory region as an object type whose lifetime has never begun. Using a common example of parsing binary data from a network into a struct, he demonstrates why this approach violates object lifetime rules in C++. The episode then provides a safer alternative using std::memcpy, which achieves the same assembly output but without invoking undefined behavior. Jason also mentions the upcoming std::bit_cast in C++20 as a future solution.

Resources

Tags