‹ BackHN Continuity

Thread

Type Punning in C and C++

36 points · 23 comments · ingve

  1. K0IN · · focus · HN ↗
    Does reinterpret_cast works in c++ for this?
    1. sheafification · · focus · HN ↗
      Yeah, you could use it for that, but it’s got some caveats and is unsafe in some cases.

      <a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;cpp&#x2F;language&#x2F;reinterpret_cast" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;cpp&#x2F;language&#x2F;reinterpret_cast

      EDIT: If you’re able to use C++20 then clearly bit_cast is better.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.