https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90285

--- Comment #4 from Niall Douglas <s_gccbugzilla at nedprod dot com> ---
> "non-aliasing reinterpret cast"?  Whatever that means.
> 
>     // Reinterpret bytes by copying (not UB for TC types)
>     memmove(temp, &v, sizeof(T));
> 
>     // Put reinterpreted bytes back. This avoids the UB
>     // of reinterpret casting without creating new objects.
>     memmove(ret, temp, sizeof(T));
> 
> not sure I understand this.  Note that mem{cpy,move} transfer the
> effective type, this is a no-op.
> 
> But well, C++ folks go wild these days...

WG21 hasn't formally seen this proposal yet. I report only WG14 feedback, which
is that a unidirectional reinterpreting cast where input object lifetime ends
within the abstract machine would be valuable. Then the compiler can know for a
fact that pointers to the input object are invalidated, and thus cannot alias
pointers derived from the returned value.

Under the C2x object model, the effective type of non-allocated storage
propagates to the destination. As we memmove() from non-allocated storage (the
stack allocated type), the effective type written to `ret` is that of `temp`.
The object lifetime for input object v ends, if input object v is not stored in
allocated storage.

So basically these operations are greenlit for the C2x object model, which is
why I presented them here, and the C committee says they are fine under the
existing C abstract machine. (Yes, as reported here they are written in C++,
but just as easily could be implemented as a C macro)

We will find out what WG21 SG12 thinks of this proposal come July.

Niall

Reply via email to