What is the difference between std::move and std::forward? When would you use move semantics over traditional copying?
Last updated
std::moveis an unconditional cast toT&&that moves nothing itself. It enables move operations and leaves the source valid but unspecified.std::forwardconditionally casts a forwarding reference in templates, preserving lvalue/rvalue category (perfect forwarding).Move instead of copy when the source is expiring or ownership is handed off. It pays off most for heap-owning types like
std::vectorandstd::string.