From this question, I learned that anonymous structures and unions were part of the C11 standard (from comments). Then I thought it would be nice to use the bitball union to replace the bitmask, and I found that someone else had already submitted a question and set an example , which is like my idea. And the answers to this question have agreed on the safety of using the method of combining bit fields to replace the bitmask. Nevertheless, the answers to the message there , as I understand it, denied the security of access to an inactive member of the union, saying that access to an inactive member of the union is undefined. I think the answers to these two questions are contradictory:
Just using the example there , after the change, the .uservalue .rawwill become undefined (according to the responses to the message ). Therefore, I think it is unsafe to use the method of combining bit fields to replace the bitmask.
I'm right? or did I misunderstand these answers?
Edit: if the answer to my question is different for C and C ++, I want to know them all.
source
share