
On Sat, Mar 14, 2009 at 04:47, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
David Abrahams skrev:
A class that attempts to provide value semantics but doesn't support x = x is putting a big hole in the system of equational reasoning. Â Justifying that (to me) would take some pretty heavy proof.
I have not really seen code that exhibits x = x. I've seen lot's of discussion (e.g. Sutter & Meyers). Does anybody write such code?
On purpose? Doubtful. Does it happen? Probably. Is there really a cost to allowing it, though? I'd assume it would have a vector-like operator= that'd be something like this: copy(other.begin(), other.begin() + size(), begin()); if (size() > other.size()) erase(begin()+other.size(), end()); else insert(end(), other.begin()+size(), other.end()); That works fine when this == &other, and I can't see a reason it would be slower than some version that doesn't allow self-assignment. Sure, it means self-assignment is O(n), but that's acceptable and consistent. It's sufficiently exception-safe, too.