#include #include #include class MyType { public: MyType() { pointer_ = new int; *pointer_ = 1; memset(array_, 0, sizeof(array_)); vector_.push_back(3.14); } MyType(MyType&& other) : pointer_(nullptr) { // Note that although the type of |other| is an rvalue reference, |other| // itself is an lvalue, since it is a named object. In order to ensure that // the move assignment is used, we have to explicitly specify // std::move(other). *this = std::move(other); } ~MyType() { delete pointer_; } MyType& operator=(MyType&& other) { // Steal the memory, null out |other|. delete pointer_; pointer_ = other.pointer_; other.pointer_ = nullptr; // Copy the contents of the array. memcpy(array_, other.array_, sizeof(array_)); // Swap with our vector. Leaves the old contents in |other|.    // We could destroy the old content instead, but this is equally    // valid. vector_.swap(other.vector_); return *this; } private: int* pointer_; char array_[42]; std::vector vector_; }; void ProcessMyType(MyType type) { } MyType MakeMyType(int a) { if (a % 2) { MyType type; return type; } MyType type; return type; } int main(int argc, char **argv) { // MakeMyType returns an rvalue MyType. // Both lines below call our move constructor. MyType type = MakeMyType(2); ProcessMyType(MakeMyType(2)); }