From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2008-04-11 02:22:58
Andrey Semashev skrev:
> Thorsten Ottosen wrote:
>> Andrey Semashev skrev:
>>> 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?
> 1. It adds a new requirement to T.
But only for swap().
> I'd rather not have it since, as
> other noted, optional is frequently used with non-default-constructible
> types. You'll simply restrict swap to be available only for
> default-constructible types, which is not the case ATM.
> 2. I'd rather not assume what is more or less expensive. The opposite
> case is quite common too (shared pimpl object is an example).
In that case they are about the same complexity. But for any container
or aggregation of containers, there is a huge difference. Just consider
std::vector<std::map<int,int>>, for example.
>>> 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.
> Default constructor may throw as well, you won't gain anything here.
Default constructors are much less likely to throw.
> I'm not sure I understand what you meant with iterators or references
If I hold a reference or an iterator into, say a vector<T> object,
that reference or iterator is not invalidated by swapping that object
with another. For optional<vector<T>> this is no longer true.
Anyway, I'll like to hear Fernando's take on it.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk