Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • What is a "type-pun"? Commented Jul 16, 2018 at 15:07
  • @curiousguy That’s when you reinterpret the bits of some object’s binary representation as if it were a different type. For example, treating the bits of a 64-bit double as a uint64_t so you can twiddle them. Especially when the method is declaring a union, writing to a member of one type, and reading out a member of a different type (which is legal in C, but, as you know, undefined behavior in C++). Commented Jul 16, 2018 at 16:23
  • @curiousguy Certain integer casts do. A cast between signed and unsigned integral types does, and so does a cast from void* to uintptr_t. On the other hand, a reinterpret_cast from double to uint64_t is a type-pun, while a C-style cast has the semantics of static_cast, which represents the value as closely as possible. Commented Jul 16, 2018 at 17:32
  • @curiousguy [expr.reinterpret.cast.11], although that says it needs to be cast to a reference. Commented Jul 16, 2018 at 18:40
  • Yes indeed a reinterpret_cast to a reference type does a reinterpretation. That a reference type was used wasn't clear from your previous comment. Commented Jul 17, 2018 at 13:10

lang-cpp