#include class base { int b_; public: base():b_(){} base(const base& b):b_(b.b_) { std::cout << "bcc" << std::endl; } base(base&& b):b_(b.b_) { std::cout << "bmc" << std::endl; } base& operator=(const base& b) { b_=b.b_; std::cout << "bca" << std::endl; } base& operator=(base&& b) { b_=b.b_; std::cout << "bma" << std::endl; } virtual void sideeffect() const { std::cout << "b_: " << b_ << std::endl; } virtual void sideeffect() { std::cout << "++b_: " << ++b_ << std::endl; } }; base createBase() { base b; b.sideeffect(); return b; } class derived : public base { int d_; public: derived():base(),d_(){} derived(const derived& d):base(d),d_(d.d_) { std::cout << "dcc" << std::endl; } derived(derived&& d):base(static_cast(d)),d_(d.d_) { std::cout << "dmc" << std::endl; } derived& operator=(const derived& d) { base::operator=(d); d_=d.d_; std::cout << "dca" << std::endl; } derived& operator=(derived&& d) { base::operator=(static_cast(d)); d_=d.d_; std::cout << "dma" << std::endl; } virtual void sideeffect() const { std::cout << "d_: " << d_ << std::endl; } virtual void sideeffect() { std::cout << "++d_: " << ++d_ << std::endl; } }; derived createDerived() { derived d; d.sideeffect(); return d; } void nonsense(const base& rval) { std::cout << "const lref: "; rval.sideeffect(); } void nonsense(base& rval) { std::cout << "lref: "; rval.sideeffect(); } void nonsense(base&& rval) { std::cout << "rref: "; rval.sideeffect(); } #if defined(TRY_TEMPLATE_RV) template void nonsense(T&& rval) { std::cout << "template rref: "; rval.sideeffect(); } #elif defined(TRY_DERIVED_RV) void nonsense(derived&& rval) { std::cout << "derived rref: "; rval.sideeffect(); } #endif int main() { std::cout << std::endl << "base constructors: " << std::endl; base b = createDerived(); const base cb = createBase(); std::cout << std::endl << "base(), b, cb: " << std::endl; nonsense(base()); nonsense(b); nonsense(cb); std::cout << std::endl << "base assignments: " << std::endl; base bb(b); b = cb; b = static_cast(bb); bb = createBase(); std::cout << std::endl << "derived constructors: " << std::endl; derived d = createDerived(); const derived cd = createDerived(); std::cout << std::endl << "derived(), d, cd: " << std::endl; nonsense(derived()); nonsense(d); nonsense(cd); std::cout << std::endl << "derived assignments: " << std::endl; derived dd(d); d = cd; d = static_cast(dd); dd = createDerived(); std::cout << std::endl << "slicing assignments: " << std::endl; bb = cd; b = static_cast(d); b = createDerived(); return 0; }