Move semantics — transfer resources instead of copying.
1class String {2 char* data;3 size_t len;4public:5 // Move constructor6 String(String&& other) noexcept7 : data(other.data), len(other.len) {8 other.data = nullptr;9 other.len = 0;10 }1112 // Move assignment13 String& operator=(String&& other) noexcept {14 if (this != &other) {15 delete[] data;16 data = other.data;17 len = other.len;18 other.data = nullptr;19 other.len = 0;20 }21 return *this;22 }23};
std::move — casts to rvalue reference:
1String a("hello");2String b(std::move(a)); // calls move constructor
std::forward — perfect forwarding in templates.