Sunday, May 19, 2019

Digest for comp.lang.c++@googlegroups.com - 2 updates in 1 topic

"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 19 11:21PM +0200

On 19.05.2019 21:10, David Brown wrote:
 
> (And that's not just theory - faffing around with casting pointer types
> as you did does not work on anything but the most limited of compilers,
> and usually only with optimisations disabled.)
 
C supports type punning via unions.
 
C++ does not.
 
C++ does not, in the strictest interpretation of the formal, support
general type punning except by way of `memcpy`.
 
I disagree with that interpretation: it's totally impractical, so IMO it
can't be the /intent/.
 
However, the GCC folks have generally adopted the most silly,
impractically rigid literal interpretations of the formal rules, so I
would absolutely not be surprised if they assume the aforementioned one
too. I don't know of any way to tell the C++ compiler that look, these
two pointers are of different types but access the same bytes in memory.
And the thing about UB, even purely formal UB, is that the compiler can
/assume/ that it will not happen, e.g. it can assume that the code
dereferencing that 2nd pointer and accessing the pointee, will never be
executed. Then the compiler can optimize it away. That's what g++ does
in a number of cases, so perhaps also in this one, if one's unlucky.
 
 
Cheers!,
 
- Alf
Sal LO <gegefffffff@gmail.com>: May 19 02:57PM -0700

https://youtu.be/O909xMoMfKk
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: