1. 17
  1. 5

    I thought that writing to one union member and reading from another is UB in C? Or does that apply only to members of different type?

    1. 2

      Only in c++, not c. Unions (along with memcpy) are the ‘correct’ way to do type punning.

      1. 4
      2. 1

        It is (probably due to possible “trap values” [1]) but there’s a lot of code that assumes one can do that.

        [1] In over 30 years of coding, I’ve yet to program on an architecture that has a “trap value”. I’ve yet to program on something other than a byte-oriented, 2’s complement machine.

      3. 4

        Whether using the define or the union approach I’m having a hard time imagining this being worth it. The extra .sub.field just doesn’t seem that painful if you need the bulk sub object handling as well. Maybe in a “it’s been 10 years of accretion this should have been a sub object all along, but I don’t want to touch all the consumers” kind of scenario? Does somebody have a concrete “in the wild” example?