From this question I learned that anonymous structs and unions had been part of the C11 standard (from the comments). Then I thought it might be a good idea to use bitfield union to replace bitmask, and I found someone else had already posted a question and shown an example, which is exactly the same as my idea. And the answers to that question agreed on the safety of using bitfield union method to replace bitmask. However, the answers to the post there, as I understood, denied the safety of accessing inactive union member, saying that accessing inactive union member is undefined behavior. I think the answers to these two questions are contradictory:
Just using the example there, after modifying .user
, the value of .raw
will become undefined (by answers to the post). So I think it is not safe to use bitfield union method to replace bitmask.
Am I right? or I misunderstood these answers?
Edit: If the answer to my question is different for C and C++, I wish to know both of them.