Possible bug in gcc 4.4.7

Andy Falanga (afalanga) afalanga@micron.com
Wed Sep 17 17:15:00 GMT 2014


> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
> Behalf Of Jonathan Wakely
> Sent: Wednesday, September 17, 2014 11:01 AM
> To: Andy Falanga (afalanga)
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: Possible bug in gcc 4.4.7
> 
> On 17 September 2014 17:16, Andy Falanga (afalanga) wrote:
> > Flag operator &=(Flag& f1, Flag f2) {
> >         return static_cast<Flag>(reinterpret_cast<unsigned int&>(f1)
> > &= static_cast<unsigned int>(f2)); }
> 
> That reinterpret_cast looks dodgy to me, accessing the object through a
> different type is undefined behaviour. What's wrong with doing it
> safely?
> 
> Flag operator &=(Flag& f1, Flag f2) {
>   unsigned int i = f1;
>   i &= static_cast<unsigned int>(f2);
>   return f1 = static_cast<Flag>(i);
> }

Ha, nothing except my thinking that, because I was casting a reference, I had to use reinterpret_cast<>.  Of course, that (your suggestion) worked for me.  Please point me to the correct section of the language spec so that I might better understand why this is undefined behavior.

Thanks Jonathan.

Andy


More information about the Gcc-help mailing list