Boost logo

Boost :

Subject: Re: [boost] [optional] Strict Aliasing Warnings on Trunk
From: Bo Persson (bop_at_[hidden])
Date: 2009-12-17 16:06:34


Emil Dotchevski wrote:
> On Thu, Dec 17, 2009 at 9:51 AM, Bo Persson <bop_at_[hidden]> wrote:
>> Mathias Gaunard wrote:
>>> Dean Michael Berris wrote:
>>>> I've recently been seeing this error more and more especially on
>>>> GCC with -Wall: /home/dean/boost/boost/optional/optional.hpp:407:
>>>> warning:
>>>> dereferencing pointer ‘<anonymous>’ does break strict-aliasing
>>>> rules /home/dean/boost/boost/optional/optional.hpp:427: note:
>>>> initialized from here and:
>>>>
>>>> /home/dean/boost/boost/function/function_base.hpp:321: warning:
>>>> dereferencing type-punned pointer will break strict-aliasing
>>>> rules /home/dean/boost/boost/function/function_base.hpp:325:
>>>> warning: dereferencing type-punned pointer will break
>>>> strict-aliasing rules
>>>>
>>>> This is on Linux and GCC 4.4.1.
>>>>
>>>> I'm not an expert in this regard but is there a way to either
>>>> silence or avoid these warnings? Anything you want me to try from
>>>> my end?
>>>
>>> This was discussed in the past, and some people claimed the
>>> warning was spurious.
>>>
>>> I am personally not convinced whether it is spurious or not,
>>> however.
>>
>> Using a reinterpret_cast to convert from one pointer type to an
>> unrelated type seems to conflict with the rules at the end of
>> chapter 3 of the standard.
>>
>> "If a program attempts to access the stored value of an object
>> through an lvalue of other than one of the following
>> types the behavior is undefined: ..."
>>
>>
>> The gcc 4.4 documentation recommends using a union, instead of a
>> type punned pointer or reference.
>
> Union is also undefined. You can't portably write one type of data
> somewhere and then read something else.
>

Right, but gcc 4.4 documents this as an implementation defined
extension, so it is supported for that compiler.

It also warns that it doesn't support the reinterpret_cast. That is at
least potentially serious.

Bo Persson


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk