Boost logo

Boost :

From: Rainer Deyke (root_at_[hidden])
Date: 2002-02-05 14:31:28


----- Original Message -----
From: "Howard Hinnant" <hinnant_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, February 05, 2002 11:11 AM
Subject: Re: [boost] auto_vector - vector with auto_ptr semantics

> Yup, makes sense. I can appreciate the symmetry. However I see a
> problem with vector::insert using only 2 and 3:
>
> Consider vector::insert(iterator position, size_type n, const
> value_type& x);
>
> There is enough capacity to handle the insert. To accomplish this
the
> vector must move some elements from the end of the vector to
> uninitialized memory, say by using 3 (move construct, destruct
source).
> At this point there is an area in the middle of the vector of
> uninitialized memory. If an exception is thrown while copy
constructing
> x into this unitialized memory, the vector escapes in a corrupted
state.
>
> However if the vector uses 1 (move construct) to move the elements
out
> of the way, and an exception is thrown while copy assigning x into
> place, then the vector is still in a valid state. There are no
holes of
> uninitialized memory. Basic exception safety has been maintained.

There are three ways of dealing with this specific problem:

1. If any exceptions are thrown by the copy operation, catch it, move
all elements back into their original position, and rethrow.

2. Copy 'x' first, then move the elements, then move the copy of 'x'
into place.

3. push_back 'x' and use 'std::swap' to fix the order of the vector
elements. This requires no move operation.

--
Rainer Deyke | root_at_[hidden] | http://rainerdeyke.com

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