Boost logo

Boost Users :

Subject: Re: [Boost-users] [Spirit] dereferencing type-punned pointer will break strict-aliasing rules
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-08-11 19:07:31


On Wed, Aug 11, 2010 at 3:14 PM, David Abrahams <dave_at_[hidden]> wrote:
> At Wed, 11 Aug 2010 11:59:21 -0700,
> Emil Dotchevski wrote:
>>
>> On Wed, Aug 11, 2010 at 1:37 AM, Roland Bock <rbock_at_[hidden]> wrote:
>> > Hi,
>> >
>> > compiling the attached example with gcc-4.4.3 (Ubuntu 10.4, 64bit) with
>> >
>> > -Wall -O3
>> >
>> > leads to a long warning message (second attachment), culminating in
>> >
>> > boost/1.44/include/boost/function/function_base.hpp:321: warning:
>> > dereferencing type-punned pointer will break strict-aliasing rules
>> > boost/1.44/include/boost/function/function_base.hpp:325: warning:
>> > dereferencing type-punned pointer will break strict-aliasing rules
>> >
>> >
>> > I wonder how I could avoid this without -fno-strict-aliasing or
>> > -Wno-strict-aliasing?
>>
>> The code in question is a reinterpret_cast which casts a char pointer
>> to a user-specified functor type pointer. The interpret cast seems
>> correct, and the warning seems correct too if not very useful in this
>> case.
>
> Really, the reinterpret_cast is portably correct?  Pretty much the
> only thing you can do portably with reinterpret_cast is round-trip
> cast a pointer type to another pointer type and then cast it back
> before you use it again.

That's what this code does.

Honestly, I'm probably giving the compiler too much credit, since I
assumed that it sees that the object it's taking the address of is a
single char, not an array of chars. In reality it's probably not that
clever and the warning is plain wrong because in general strict
aliasing rules don't apply to char pointers, do they?

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net