Accessing inactive union member and undefined behavior?

前端 未结 5 1267
独厮守ぢ
独厮守ぢ 2020-11-21 06:00

I was under the impression that accessing a union member other than the last one set is UB, but I can\'t seem to find a solid reference (other than answers clai

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 06:49

    Something that is not yet mentioned by available answers is the footnote 37 in the paragraph 21 of the section 6.2.5:

    Note that aggregate type does not include union type because an object with union type can only contain one member at a time.

    This requirement seem to clearly imply that you must not write in a member and read in another one. In this case it might be undefined behavior by lack of specification.

提交回复
热议问题