From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-07-28 14:42:45
Mathias Gaunard wrote:
> Your code needs to be able to assign a non-empty object to an empty one.
> Would you also consider it necessary to be able to assign an empty
> object to a non-empty one?
To be more specific my code needs to assign a non-empty object
to one that may or may not be empty.
> Possible alternative here: destruct and construct.
> *write_pos++ = std::move(*begin);
> reassign(*write_pos++, std::move(*begin));
> template<typename T1, typename T2>
> void reassign(T1& old, T2&& new)
> new(&old) T1(std::forward<T2>(new));
> A function called `reassign' (that would destroy the old object then
> construct the new one) could be used instead of assignment in cases
> where the left operand can be in a `moved' state.
This only provides yet another way to assign (And to get undefined
behavior if you forget to use the right function).
a = b; // a must not have been moved from
a = move(b); // ditto
reassign(a, b); // a may have been moved from. doesn't even provide
basic exception safety.
reassign(a, std::move(b)); // a may have been moved from. Exception
safe as long as the move constructor can't throw.
I thought that you were advocating safety?
> Anyway, I'm pretty sure that function could be purely implemented in
> terms of swap, which seems like a safer construct.
Sure it could be implemented in terms of swap, but I disagree that
swap is safer than proper move assignment. swap is also less efficient