It's an invariant of the 'DirectorySearchResult' class that query is non-null, as the destructor deletes it without checking it for nullity. The most straight-forward and efficient way to achieve both the stealing and the valid state is to swap the pointers.
Deleting a null pointer is a fully specified no-op in C++.
To make the question clearer they should have added a function on the original class that checks (or doesn't check) the pointer before dereferencing it, thereby clarifying whether query being null is valid or not.
Swap is additionally risky because someone can ADL specialize it for their own purposes. (which is the point) And suddenly your move assignment is broken.
So people work around it by adding using std::swap clause there...
And then you're calling all those destructors.
e.g. (pseudo-ish code)
operator=(vector&& foo) {
delete [] elements;
elements = foo.elements;
foo.elements = nullptr;
}
what the example code they have as an answer does is something morally equivalent to this:
operator=(vector&& foo) {
swap(elements, foo.elements);'
}
relying on the knowledge that because it's being moved the ~vector is going to be called immediately afterwards.
This strikes me as being "clever" in the "potentially confusing code, for no real reason" sense
The idiomatic copy and swap is like this:
T& operator =(T x) // note pass by value { std::swap(this, x); return this; }
This acts both as a normal and move assignment operator (if T is not copy constructible);
edit: how do you get HN to show asterisks? There should be one before each 'this'