The following code example is taken from the book
C++ Move Semantics - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2020
The code is licensed under a
Creative Commons Attribution 4.0 International License.
#include <string>
#include <vector>
#include <iostream>
#include <cassert>
long nextID() {
static long id{0};
return ++id;
}
class Customer {
private:
long id{nextID()}; // negative ID signals that the value was moved away
std::string name; // name of the customer
std::vector<int> values; // some values of the customer
public:
Customer(const std::string& n)
: name{n} {
assert(!name.empty());
}
std::string getName() const {
assert(id > 0); // ensure value was not already moved
return name;
}
void addValue(int val) {
assert(id > 0); // ensure value was not already moved
values.push_back(val);
}
friend std::ostream& operator<< (std::ostream& strm, const Customer& cust) {
//assert(cust.id > 0); // ensure value was not already moved
strm << '[' << cust.id << ": " << cust.name << ": ";
for (double val : cust.values) {
strm << val << ' ';
}
strm << ']';
return strm;
}
// copy constructor (copy all members except the ID):
Customer(const Customer& cust)
: name{cust.name}, values{cust.values} {
assert(cust.id > 0); // ensure source value was not already moved
}
// move constructor (steal all members except the ID):
Customer(Customer&& cust)
: name{std::move(cust.name)}, values{std::move(cust.values)} {
assert(cust.id > 0); // ensure source value was not already moved
cust.id *= -1; // negate source ID to signal value was moved
}
// copy assignment (assign all members except the ID):
Customer& operator= (const Customer& cust) {
assert(cust.id > 0); // ensure source value was not already moved
if (this != &cust) {
name = cust.name;
values = cust.values;
if (id < 0) id *= -1; // we have a valid value now
}
return *this;
}
// move assignment (steal all members except the ID):
Customer& operator= (Customer&& cust) {
assert(cust.id > 0); // ensure source value was not already moved
if (this != &cust) {
name = std::move(cust.name);
values = std::move(cust.values);
if (id < 0) id *= -1; // we have a valid value now
cust.id *= -1; // negate source ID to signal value was moved
}
return *this;
}
};