
--- David Abrahams <dave@boost-consulting.com> wrote:
Alberto Barbati <abarbati@iaanus.com> writes:
Suppose that T *is* assignable, is destroy+copy construction better than assignment? Probably no, for several reasons, including:
1) assigment is probably more efficient. In particular it might avoid destruction/reconstruction of sub-objects non related with the class invariant (for example: buffers, mutexes, etc.). I agree the assignment is more efficient. If you dig in the code little bit you will find that there is assignment operation provided for the primitive types.
2) with the dtor/ctor idiom, if the ctor throws an
element is lost and you can't do anything about it, so the container cannot provide more than the basic guarantee for any method that might overwrite elements. However,
implement assigment with the strong guarantee,
exception, the old the user might the container might
leverage on that and provide the strong guarantee too at least for methods that might overwrite a single element such as push_back/push_front. (Of course, the user might implement assignment so badly that it doesn't provide even the basic guarantee and the container could not recover from that, but a library such not try to outsmart the user... too much ;).
Just my opinion,
The idea behind the dtor/ctor idiom is like this: suppose you have full circular_buffer and you want to push_back a new element (instance of some class). That means the fron-most element is about to be overwritten. Now if there will be the assignment idiom applied the front-most will be assigned to the new one - it will be not destroyed (no destructor will be called). IMHO this is not correct. The old element will not disappear - just its value/state will be different. I think that the overwrite operation means destruction of the old object, NOT assignment. What do you think? Jan
Well said.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
__________________________________ Do you Yahoo!? Yahoo! Search - Find what you�re looking for faster http://search.yahoo.com