#include #include class Test { public: static unsigned int _id; Test(): _a(1000) { _effective_id = _own_id = _id++; log("Default constructor"); }; Test(int a): _a(a) { _effective_id = _own_id = _id++; log("Int constructor with value " + std::to_string(a)); }; Test(const Test& other) { _a = other._a; _effective_id = _own_id = _id++; log("Copy constructor with id " + std::to_string(other._own_id)); } Test(Test&& other) { _a = other._a; _effective_id = _id++; _own_id = other._own_id; log("Move constructor with id " + std::to_string(other._own_id)); other._move_made = true; } ~Test() { log("Destructor"); if(_move_made) { log("After move"); } } Test& operator=(const Test& other) { _a = other._a; log("Copy operator with id " + std::to_string(other._own_id)); return *this; } Test& operator=(Test&& other) { _a = other._a; other._move_made = true; _effective_id = _own_id; _own_id = other._own_id; log("Move copy operator with id " + std::to_string(other._own_id)); return *this; } private: int _a; unsigned int _own_id, _effective_id; bool _move_made = false; void log(std::string msg) { std::cout << _own_id << "(effective: " << _effective_id << ")" << ": " + msg + "n"; } }; Test get_test() { std::cout << "Inside get_test()n"; return Test(5); } Test&& get_move() { std:: cout << "Inside get_move()n"; return std::move(Test(7)); } void copy_pass(Test copy) { std::cout << "Took by copyn"; } void pass(const Test& ref) { std::cout << "Took by refn"; } void pass(Test&& subject) { std::cout << "Took by rvaln"; } unsigned int Test::_id = 0; int main() { std::cout << "Initialize an"; Test a = get_test(); std::cout << "Pass by copyn"; copy_pass(a); std::cout << "Pass by refn"; pass(a); std::cout << "Pass by moven"; pass(std::move(a)); std::cout << "Initializing bn"; Test b(7); std::cout << "Move to cn"; auto c = std::move(b); std::cout << "Ref to dn"; auto& d = c; std::cout << "Copy by ref to en"; Test e(d); std::cout << "Copy by rval to fn"; Test f(std::move(e)); std::cout << "Getting by moven"; Test g(get_move()); }