Move semantics replace copy semantics in situations where copying is inefficient. Copy semantics deals fully with copyable objects, including const objects.
Already, there exists a myriad of non-copyable objects in c++11, for example std::unique_ptr. These objects rely on move semantics completely because moving from an object allows for invalidating it. This is important (imho) for popular design patterns like RAII.
A problem occurs when a const non-copyable object is assigned to an area of memory. Such an object can't be recovered in any way.
This is obviously important during the lifetime of the object, because of its constness. At the end of it's lifetime, when the destructor is called however, the (non-existent) object is briefly non-const.
I suggest that a moving destructor could be a valuable addition to the move semantics model.
Consider a simple situation where an unique_ptr is used in an unordered_set. You can insert
into this set using a move constructor (or construct "emplace"), however if you wanted to move this pointer to another unordered_set (i.e. keeping it const) it would be impossible.
Essential, there is a iterator insert((possibly const) key&&)
but no const key&& erase(iterator)
. In fact it would be impossible. The container could only be extended to return some pointer to the key, and forget about it.
A moving destructor could solve this ie const MyClass&& ~MyClass()
, since it would only violate const during destruction (when the compiler considers the object is invalid anyway).
EDIT: I should point outconst MyClass&& ~MyClass() const
actually makes more sense. The destructor doesn't have to modify anyhting, only destroy the object as if it were no longer a valid handle to whatever resource it controlled.
const
during construction.