Thus, move constructors are designed to make copy constructors cheaper in some cases.
Write down what we expect from these constructors and destructors: (This is a bit of a simplification, but it's good for this example).
observer_ptr() { this->ptr == nullptr; } observer_ptr(T *obj) { this->ptr = obj; } observer_ptr(observer_ptr<T> const & obj) { this->ptr = obj.ptr; } ~observer_ptr() { }
You assume that the class provides a move constructor that looks like this:
observer_ptr(observer_ptr<T> && obj) { this->ptr = obj.ptr; obj.ptr = null; }
When I would assume that the existing copy constructor would work just as it is, and cheaper than the proposed move constructor.
How about std::vector though?
A std :: vector when copying actually copies the array that it supports. Thus, the copy constructor std :: vector looks something like this:
vector(vector<T> const & obj) { for (auto const & elem : obj) this->push_back(elem); }
The move constructor for std :: vector can optimize this. He can do this because this memory can be stolen from obj . In std :: vector, this is really useful to do.
vector(vector<T> && obj) { this->data_ptr = obj.data_ptr; obj.data_ptr = nullptr; obj.size = 0; }
source share