From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2006-03-11 05:56:47
On Friday 10 March 2006 17:14, Vaclav Vesely wrote:
> When two optionals (one initialized and other not) are swaped, then the one
> value in uninitialized optional is constructed and the other one in
> initialized optional is destructed.
> IMHO it's not necessary. It should be more effective to swap optional's
> m_storages without any values constructiona and destruction.
> Is there any reason for the current implementation?
You can't simply do so. The reason is that an object might contain (directly
or indirectly) a pointer to itself. Now, if you'd just swap memory bytewise,
it would change its address but those pointers would not be updated.
It could only be done if the object was moveable, i.e. depending on a
type_trait. Looking at the Boost type_traits library, I wonder why this trait
doesn't exist - many things can be sped up using memcpy when is_pod is true,
but even things like vector/string/deque (list/map/set depending on whether
their root node is stored by pointer) can be moved in memory without negative
effects (yes, talking about typical implementations here, not guarantees).
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk