Type Punning in C and C++

(blog.pwkf.org)

30 points | by ingve 1 hour ago

6 comments

  • scoopr 3 minutes ago
    I just watched video[0] that argues if you can consteval something, then it must be free of UB, which seems compelling, at least for these lower level helpers.

    Unfortunately, it seems memcpy is not constexpr, so cannot be used like this, but std::bit_cast actually works[1] as constexpr, so I think in C++ that would now be the most preferred use. It won't allow the union either.

    [0] https://www.youtube.com/watch?v=-LAXqqqX274 [1] https://godbolt.org/z/xGzjTMGvv

  • eqvinox 15 minutes ago
    The example included below "Why C and C++ Differ" is UB in both C and C++ (and says nothing about "why" C & C++ differ). The article isn't overall wrong but that bit is just...

    (Feels a bit like LLM junk, but honestly not sure.)

    • LoganDark 8 minutes ago
      > A pointer cast worked fine at -O0 and silently broke at -O2.

      Feels like LLM to me. I looked at some of the rest of the article and... pretty much certain now.

  • pipe01 11 minutes ago
    Is it necessary to use the lowest possible font weight?
  • antiloper 48 minutes ago
    > The C vs C++ distinction here is genuinely treacherous

    What's so genuine about this?

    • tialaramex 31 minutes ago
      Despite the fact they're two different languages, people are often taught as though it's basically just a subset relationship and then they carry this through into the actual software they write.

      Because neither of these are memory safe languages, you are required to ensure you've made no mistakes or else anything might happen.

    • ndr 24 minutes ago
      It continues with

      > and most blog posts on the topic get it wrong.

      This smells like Claudism/LLMish.

  • K0IN 1 hour ago
    Does reinterpret_cast works in c++ for this?
  • jefftk 17 minutes ago
    [flagged]