Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2007-04-17 19:51:25


Sohail Somani wrote:
> Still, for generic code, I don't think you should expect to be able to
> reuse a moved object except for destructing it. Specific classes, maybe,
> but not as a general rule.
>
> Even still, I can't think of a case off-hand where assignment requires
> some previous state so maybe this is a moot point. Maybe for some really
> crazy overloading of operator=.

    At a bare minimum you need to be able to destruct or assign to a
moved object. For example, consider the code for inserting a value into
the middle of an vector. If the vector doesn't need to be reallocated
you would use the following algorithm:

1) Use the move constructor to move the last element of the vector to
where end() currently points to. Note that this leaves a "hole" in the
vector in what is now the second-to-last position.
2) Iterating backward, use move assignment to move the element
immediately in front of the hole to the position of the hole, thus
moving the hole back one. Continue doing this until the hole is
in the position where you want to do your insertion.
3) Assign your new value to the position of the hole, thus filling the
hole. Assuming no exceptions were thrown every element of the vector
now has a determined value.

Note that steps 2 and 3 above required assigning to an element that had
previously been moved from.

Joe Gottman


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