Sub topic 3
Designing Movable Classes
The rule of zero or five
The rule of zero says to let member objects manage their own resources. For example, std::string manages its own text, so a class containing a string often needs no custom copy or move code.
If you define or delete any of these five special member functions, consider all five: the destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator. This is the rule of five.
The example below explicitly defaults all five, asking the compiler to generate them. With only a string member, the rule of zero would be simpler.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
struct Item {
std::string name;
explicit Item(std::string value) : name(std::move(value)) {}
~Item() = default;
Item(const Item&) = default;
Item& operator=(const Item&) = default;
Item(Item&&) noexcept = default;
Item& operator=(Item&&) noexcept = default;
};
Item make_item() {
return Item{"book"};
}
void show(const Item&) { std::cout << "lvalue\n"; }
void show(Item&&) { std::cout << "rvalue\n"; }
template <typename T>
void relay(T&& item) {
show(std::forward<T>(item));
}
int main() {
Item item = make_item();
relay(item);
relay(std::move(item));
std::vector<Item> items;
items.push_back(std::move(item));
std::cout << items[0].name << '\n';
}Compile this program as C++17 or later. It prints lvalue, rvalue, and book.
Defaulting a move is not always safe. For example, moving a raw owning pointer this way only copies its address. Both objects could then try to free the same resource.
Exception safety and `noexcept`
An exception signals an error during a program. noexcept promises that a function will not let an exception escape. If one does escape, the program terminates. Use this promise only when the operation cannot fail that way.
Moves in standard containers
When a std::vector grows, it may move its elements to new storage. If moving might throw and copying is available, it can copy instead to protect its contents. A noexcept move makes moving a safer choice.
Return values and copy elision
Copy elision means constructing an object directly in its destination, without an extra copy or move. In C++17, return Item{"book"}; constructs the result directly. Item{"book"} is a prvalue: an expression producing a new value.
Returning a named local can also allow elision. Writing return std::move(local); can prevent it.
Forwarding references
In relay, T&& is a forwarding reference: it accepts either an lvalue or an rvalue. std::forward preserves which kind the caller supplied. This is called perfect forwarding. Calling relay(std::move(item)) does not itself move the item; show only observes it.
Common pitfalls
std::move does not perform a move by itself. Moving a const object usually copies instead, or fails if copying is unavailable. Also, a named rvalue reference is an lvalue expression: use std::move when you intend to move from it.
Exercise
Why might a vector copy an element instead of moving it?
Show answer
If the move might throw and copying is available, copying can protect the vector’s contents.