Boost logo

Boost :

Subject: Re: [boost] [interest] underlying type library
From: Julian Gonggrijp (j.gonggrijp_at_[hidden])
Date: 2011-08-20 14:41:53


Marshall Clow wrote:

> On Aug 20, 2011, at 10:45 AM, Olaf van der Spek wrote:
>
>> On Sat, Aug 20, 2011 at 6:28 PM, Mathias Gaunard
>> <mathias.gaunard_at_[hidden]> wrote:
>>> You cannot use bitwise copy to implement move semantics.
>>> That just doesn't work.
>>
>> Hmm, why not? What breaks if you do a bitwise swap?
>
>
> Consider a type that looks contains a pointer to one of its member vars.
>
>
> struct Foo {
> char *curPos;
> char buffer [ 8 ];
> Foo () : curPos (buffer+4) {}
> };
>
> Foo a, b;
>
> Let's assume that a is at 0x1000, and b is at 0x2000 (and four byte pointers)
>
> 0x1000: 00001008
> 0x1004: aaaaaaaaa
> 0x1008: bbbbbbbb
>
> 0x2000: 00002008
> 0x2004: cccccccccc
> 0x2008: dddddddd
>
> Now we do a (bitwise) swap of a and b:
>
> 0x1000: 00002008
> 0x1004: cccccccccc
> 0x1008: dddddddd
>
> 0x2000: 00001008
> 0x2004: aaaaaaaaa
> 0x2008: bbbbbbbb
>
> a's pointer now points into b's buffer.

I see. I'll have to relax my claim that the swap algorithm will work for *any* type. Authors of such a class would have to be warned that they can't use the swap algorithm which applies move_raw.

I can think of one other case in which it doesn't work: when the object contains a pointer to something that points back to the object. Though I don't know why someone would want to do that.


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