Move Semantics and the Rule of Zero/Five – C++
Moving resources instead of copying, rvalue references and perfect forwarding.
Copying large objects is expensive. With move semantics, the contents of an object that is no longer needed anyway are "stolen": pointers are handed over instead of duplicating data. std::move itself moves nothing – it merely casts to an rvalue reference and thereby permits moving.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
std::vector<int> v(1000000, 1);
auto w = std::move(v); // O(1): only internal pointers change
std::cout << w.size() << '\n'; // 1000000
// do not use v anymore (valid but unspecified; only assign/destroy)
std::string a = "a long text that does not fit into the SSO buffer";
std::string b = std::move(a);
std::cout << b << '\n';
}If a class manages a resource itself (a raw pointer, a handle), you need the Rule of Five: destructor, copy and move constructors, and copy and move assignment. Here with copy-and-swap for the assignment:
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <utility>
class Puffer {
public:
explicit Puffer(std::size_t n) : n_(n), daten_(new int[n]{}) {}
~Puffer() { delete[] daten_; }
Puffer(const Puffer& o) : n_(o.n_), daten_(new int[o.n_]) {
std::copy(o.daten_, o.daten_ + n_, daten_);
std::cout << "copied\n";
}
Puffer(Puffer&& o) noexcept
: n_(std::exchange(o.n_, 0)), daten_(std::exchange(o.daten_, nullptr)) {
std::cout << "moved\n";
}
Puffer& operator=(Puffer o) noexcept { // parameter by value: copy OR move
std::swap(n_, o.n_);
std::swap(daten_, o.daten_);
return *this;
}
std::size_t groesse() const { return n_; }
private:
std::size_t n_;
int* daten_;
};
int main() {
Puffer a(100);
Puffer b = a; // copied
Puffer c = std::move(a); // moved
b = std::move(c); // moved
std::cout << b.groesse() << '\n'; // 100
}Even better: the Rule of Zero. Hold resources in members that manage themselves (std::vector, std::string, std::unique_ptr), and write none of the five special functions – the compiler generates correct copying/moving automatically.
#include <memory>
#include <string>
#include <vector>
class Dokument { // Rule of Zero: write nothing yourself
std::string titel_;
std::vector<int> daten_;
std::unique_ptr<int[]> extra_; // makes the class move-only
};Perfect forwarding: T&& on a deduced template parameter is a forwarding reference; std::forward<T> passes the argument on with its original value category (this is how make_unique and emplace_back work).
#include <memory>
#include <utility>
template <typename T, typename... Args>
std::unique_ptr<T> mache(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}return std::move(lokal); is usually a mistake – it prevents copy elision (RVO) and makes the code slower. Simply return local objects with return lokal;. A const object cannot be moved; std::move then silently copies. Move operations should be noexcept, otherwise std::vector prefers to copy when growing.