A deep dive into a common C++ misconception: that reinterpret_cast converts an object from one type to another. In reality, reinterpret_cast only converts the pointer type — it does not create a new object or start its lifetime. Accessing the resulting pointer as the new type is undefined behavior. The post contrasts this with std::start_lifetime_as (C++23), which properly starts the lifetime of a new object in a given memory region without invoking constructors or destructors, making it the correct tool for type punning. A summary table compares reinterpret_cast, std::bit_cast, and std::start_lifetime_as and their appropriate use cases.
Table of contents
My motivationThe most common (misusage) I seeYou often want object lifetimeKey takewaysSort: