~/talk

Type Punning in C++17: Avoiding Pun-defined Behavior

Talkat CppCon 2017 · 2017-09-24..09-29· [compilers-and-language-design, c-and-cpp]
links:none on file
slides:none on file

C++17 rules which type-punning idioms are undefined and which are tolerable: the talk shows that casting between unrelated pointers or mutating non-mutable consts is UB, while conversions round-tripping via integer types are implementation-defined and access via unsigned char or std::byte is permitted. It examines unions (common initial sequence allowed; non-active member access is UB), recommends memcpy as the practical workaround (standard wording is fuzzy), discusses std::launder() for placement-new cases, and measures clang -O3 output (memcpy/REINTERPRET and union variants produce similar tight assembly; the char-based form costs one extra instruction).

AI-written summary · about

  1. 0:00Introduction to type punning
  2. 1:26What is type punning
  3. 3:11Why type pun and risks involved
  4. 6:14Trivially copyable types
  5. 12:30Behavior types in C++
  6. 23:21Undefined behavior explained
  7. 24:29Casting away const
  8. 31:14Pointer to integer conversions
  9. 40:23Accessing value representations
  10. 45:15Reinterpret cast and strict aliasing
  11. 52:17Type punning with unions
  12. 56:46Punning with unrelated types
  13. 1:03:33Summary and best practices
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.