
Frank Mori Hess skrev:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Thursday 10 April 2008 16:02 pm, Thorsten Ottosen wrote:
Your approach requires the T to be default constructible which is not always the case. Why is this better than the far more expensive copy-construction?
The documentation indicates optional<T> requires T to be copy constructible. It explicitly does not require T to be default constructible.
Well, not all operations requries T to be copy-constructible, just as not all operations would require it to be default constructible.
Besides, to my mind, the current implementation is more obvious for the user. No user expects swap to throw or invalidate iterators or references like the current implementation does.
Personally, I consider any preservation of iterators/references/pointers to the internals of a swapped object to be purely a side-effect of a swap specialization, whose raise d'etre is as a speed optimization.
If speed is an issue, then the current implementation is also worse than what i suggested: copy construction + destruction is *far more expensive* than default construction + specialized swap + destruction for non-trivial classes with heap-based memory. The latter typically requires zero heap allocations. -Thorsten