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.
Related C++ Standard Sections
This episode covers topics found in these sections of the C++ standard:
-
[atomics]40% match -
[bit.cast]40% match