// // (C) Copyright Thomas Klimpel 2010. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // #include class simple_impl {}; class simple_cm {}; class simple_class { typedef simple_class self_t; simple_impl* p; simple_cm m; public: ~simple_class() { if(p) delete p; } simple_class() : p(0), m() {} simple_class(const self_t& cr) : p(0), m(cr.m) { if(cr.p) p = new simple_impl(*cr.p); } simple_class(self_t&& rr) : p(rr.p), m(std::move(rr.m)) { rr.p = 0; } void swap(self_t&& rr) { if (this != &rr) { if (p) delete p; p = rr.p; rr.p = 0; m = std::move(rr.m); } } self_t& operator=(self_t&& rr) { swap(std::move(rr)); return *this; } //The following copy assignment operator doesn't work, //because it makes the move assignment operator ambiguous. //self_t& operator=(self_t s) { swap(std::move(s)); return *this; } //The following copy assignment operator should work, //and should have the same performance as the above implementation, //because RVO cases get handled by the move assignment operator. self_t& operator=(const self_t& cr) { swap(self_t(cr)); return *this; } //The following copy assignment operator is not exception safe //self_t& operator=(const self_t& cr) { // if (this != &cr) { // if (p) delete p; // p = cr.p ? new simple_impl(*cr.p) : 0; // m = cr.m; // } //} }; int main() { simple_class sc; sc = simple_class(); return 0; }