Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-28 00:50:21


"E. Gladyshev" <egladysh_at_[hidden]> writes:

> --- David Abrahams <dave_at_[hidden]> wrote:
> [..]
>>
>> Wha?? Copying the _LHS_ to heap backup?? That can't possibly result
>> in the basic guarantee unless I've completely missed something.
>
> Yeah, we all missed something. :)
> The current variant does create a heap copy of the *lhs* and
> switches to it if there is an exception.

Umm. OK, I guess that one works for the basic guarantee, though it's
weird because the heterogeneous assignment only gives the basic
guarantee when it could give the strong guarantee at similar cost.

>> I believe my suggestion has been misunderstood. The only way to
>> get this right is to use heap storage for the new rhs. In other
>> words:
>>
>> variant<A,B> internally is conceptually a union {A,B,A*,B*};
>>
>> direct construction of a variant with A or B constructs an A or B
>> in-place. Assignment from the same type assigns in-place.
>> Assignment of the other type allocates the new RHS on the heap,
>> destroys the LHS, and copies the pointer to the heap-allocated RHS
>> copy into the union.
>> This was exactly my understanding
>
> There should be a special case if the variant has a non-throw
> copy-constructable type.

Yes, yes of course.

> Other than that this was my understanding too, till I looked at the
> variant's codes.
>
> If the variant doesn't have any guarantees about
> the storage location of the controlled object,

...like the stability of that location...

> it seems that it is possible to create a heap
> copy of the *lhs* not *rhs* and try to allocate
> *rhs* in-place. If it is true, it seems that
> the current implementation is more efficient
> in terms of memory fragmentation (create a temp. *lhs*
> and if everything is fine, delete it).

Oooh, now I understand. Actually that's clever. OK, sorry Eric; nice
job!

> For example provided that there were no exceptions, an array of
> variants will occupy a continuous chunk of storage.
>
> Also, I mentioned it to Eric that the current implementation doesn't
> actually provide basic guarantees. He was going to look into that.

It sounds like it should/could be modified to do so.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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