Boost logo

Boost :

Subject: Re: [boost] [move][container] Review Request (new versions ofBoost.Move and Boost.Container in sandbox and vault)
From: David Abrahams (dave_at_[hidden])
Date: 2009-09-07 18:50:23


on Mon Sep 07 2009, Howard Hinnant <howard.hinnant-AT-gmail.com> wrote:

>> Do we think swap(x,y) is also an error when x == y?
>
> This is a decent argument to not assert in self-move-assignment.

And not to call it undefined behavior. self-std:swapping was allowed in
C++03 and I don't think we can break that.

> But this is also almost certainly harmless:
>
> #include <algorithm>
> #include <iostream>
>
> class A
> {
> int data_;
> public:
> explicit A(int data = 0) : data_(data) {}
> A(A&& a) : data_(a.data_) {a.data_ = 0;}
> A& operator=(A&& a) {data_ = a.data_; a.data_ = 0; return *this;}
> friend std::ostream& operator<<(std::ostream& os, const A& a)
> {
> return os << a.data_;
> }
> };
>
> int main()
> {
> A a(5);
> std::cout << "Before swap a = " << a << '\n';
> std::swap(a, a);
> std::cout << "After swap a = " << a << '\n';
> }
>
> Before swap a = 5
> After swap a = 5

Yes, it's harmless, but I don't see how it's a "but." I must be missing
your point, because that example seems to support my argument.

> But if I caught my own code doing a self-swap, yeah, I would treat it
> as a bug and correct it. For example, every time I've written reverse
> (which does nothing but swap x and y while x and y move closer to each
> other in the sequence), I'm careful to break out of the loop before &x
> == &y.

Yes, but if it's reverse's job to avoid that, then it's also swap's
job (both are algorithms, neh?) So should we put a self-swap test there?

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk