What are move semantics, and how do they differ from copy semantics?
Last updated
Copy semantics deep-copy the resource (heap memory, file handle) so both objects own separate data. Move semantics transfer ownership by stealing the pointer/handle, with no allocation.
Moves use the move constructor and
operator=(T&&), which take an rvalue reference. Temporaries bind automatically, andstd::move(just a cast) forces a move on an lvalue.A moved-from source must be left valid but unspecified, so it can still be destroyed or reassigned safely.