Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2006-03-10 19:42:46


"Vaclav Vesely" <vaclav.vesely_at_[hidden]> wrote in message
news:002101c6445d$ad1723e0$a6cddcd5_at_nbvasek...
> Hi,
>
> 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?

  It would be easy to swap two optionals like this if optional<T> were
implemented using pointers to T that were constructed using new and
destructed using delete, but optional is not implemented this way. It is
implemented using char buffers, with in-place constructors and destructors
used to construct and destruct. There are plusses and minuses to both
approaches. One advantage of the current approach is that construction and
destruction are much faster because they don't have to call new and delete,
but a disadvantage is that there is no easy way to swap two optionals when
one is initialized and the other isn't.

Joe Gottman


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