<div dir="auto"><div>json::Value in JSON.h is a discriminated union.<div dir="auto">The storage is a char array of appropriate type and alignment. The storage holds one object at a time, it's initialized (and for nontrivial types, destroyed) at the right times to ensure this. The cast is only to the type of object that's already there, there's no magic here.</div><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Aug 10, 2018, 17:52 Andrew Haley <<a href="mailto:aph@redhat.com">aph@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 08/10/2018 05:30 AM, Liu Hao wrote:<br>
> Only an lvalue of a pointer to (possibly CV-qualified) `void` or a <br>
> pointer to a character type (in C) / any of `char`, `unsigned char` or <br>
> `std::byte` (in C++) can alias objects.<br>
<br>
Yes.<br>
<br>
> That is to say, in order to eliminate the aliasing problem an <br>
> intermediate lvalue pointer is required.<br>
<br>
Not exactly.  You can cast a pointer to a pointer to some character<br>
type or the type of the object stored in memory.  It does not matter<br>
whether you use an intermediate type or not.  Having not seen the test<br>
case, I can't tell whether this rule is followed.<br>
<br>
What you can't do is store as a double, cast the double pointer to a<br>
character pointer, cast that pointer to some other pointer type, and<br>
read from memory.  GCC won't give you a warning for that, but it's<br>
still undefined.<br>
<br>
JSON.h seems to hope that if you cast a pointer to T to a pointer to<br>
some union type, magic will happen.  It won't work, unless the object<br>
stored in the memory at that address was stored as the union type.<br>
<br>
Do not lie to the compiler or it will get its revenge.<br>
<br>
-- <br>
Andrew Haley<br>
Java Platform Lead Engineer<br>
Red Hat UK Ltd. <<a href="https://www.redhat.com" rel="noreferrer noreferrer" target="_blank">https://www.redhat.com</a>><br>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671<br>
</blockquote></div></div></div>