3 ms·
From a cursory search I can't find any languages in the C standard that disallow reading not from the last written member. I'm familiar with such language in t
by gpderetta 2y ago
From a cursory search I can't find any languages in the C standard that disallow reading not from the last written member.
I'm familiar with such language in the C++ standard.
Edit: On the contrary, this note 92:
> 92)If the member used to read the contents of a union object is not the same as the member last used to store a value in the
object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new
type as described in 6.2.6 (a process sometimes called type punning). This can possibly be a non-value representation.
Edit2: and that's specifically the text that was added by dr283. I think you might be confusing with a different DR (don't remember the number) that specifically asked if generalized type punning was possible as long as an union containing the aliased types was visible in the translation unit. I think that's still open although GCC definitely forbids it.
- deleted 2y ago[deleted]
- ryao 2y agoWhere did you find that note? I do not see it in the C standard draft I linked.
- gpderetta 2y agoIt is in n3301, the last 202y draft. In the 201x draft it was note 95.
- ryao 2y agoI see it now. I guess the GCC developers were wrong then.
- gpderetta 2y agoI think it is a bit more complicated. The rule, together with the aliasing rule, if taken a face value, means you could do unrestricted aliasing as long as you cast to an union type on access. I believe that's the interpretation the GCC Devs reject as is makes TBAA ineffective. Instead they interpret it narrowly to only allow punning through objects that are actual unions (as described in the GCC docs). Unsurprisingly the standard is kind of a mess.