Boost logo

Boost :

Subject: Re: [boost] gcc-4.4.0 and boost::optional is giving (spurious?) strict-aliasing warnings
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2009-05-15 18:42:35


On Fri, May 15, 2009 at 9:11 PM, Fernando Cacciola
<fernando.cacciola_at_[hidden]> wrote:

[ the following text was by me]
z>>
>> IIRC aliasing rules talk about dynamic types of objects: roughly, you
>> can access a memory location only with a type which is the same as the
>> *dynamic type* of that location or as a char array.
>>
>> Boost optional does an explicit placement new of the stored type on
>> the buffer
>
> Via a char*, so the placement new is merely taking the address of a suitably
> aligned memory b'lock.
>
>> which changes the dynamic type of the buffer
>
> Just for the record, no it doesn't.
>

Wait, how it doesn't?

It uses placement new. The lifetime of an object, and thus its type
begins at the new expression and ends when deleted. Initially the
buffer of an optional<T> certainly hasn't type T (in fact, being an
union, the type would be undetermined until the first store to a
member of the union), but when placement new T is used and the object
is constructed, the buffer certainly has type T. As the type is a
property of the object, when the object is deleted, the buffer
certainly no longer has that type. So I think you can say that, in
optional at least, placement new changes the type of the buffer, from
undetermined to T.

Am I missing something?

-- 
gpd

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