#include <iostream>
#include <memory>
using namespace std;
struct MyClass
{
MyClass() { cout << "MyClass ctor runned." << endl; }
~MyClass() { cout << "MyClass dtor runned." << endl; }
};
int main()
{
unique_ptr<MyClass> up1(new MyClass);
up1 = make_unique<MyClass>(); // since C++14
{
cout << "Entered inner block." << endl;
unique_ptr<MyClass> up2(new MyClass);
//up1 = up2; // Syntax error
up1 = move(up2); // Ownership moved from up2 to up1
up2.reset(); // No memory deallocation
cout << "Leaving inner block." << endl;
}
cout << "Left inner block." << endl;
up1.reset(); // Memory deallocation
cout << "Leaving main() function." << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBNeUNsYXNzCnsKICAgIE15Q2xhc3MoKSB7IGNvdXQgPDwgIk15Q2xhc3MgY3RvciBydW5uZWQuIiA8PCBlbmRsOyB9CiAgICB+TXlDbGFzcygpIHsgY291dCA8PCAiTXlDbGFzcyBkdG9yIHJ1bm5lZC4iIDw8IGVuZGw7IH0KfTsKCmludCBtYWluKCkgCnsKICAgIHVuaXF1ZV9wdHI8TXlDbGFzcz4gdXAxKG5ldyBNeUNsYXNzKTsKICAgIHVwMSA9IG1ha2VfdW5pcXVlPE15Q2xhc3M+KCk7ICAvLyBzaW5jZSBDKysxNAoJCiAgICB7CiAgICAgICAgY291dCA8PCAiRW50ZXJlZCBpbm5lciBibG9jay4iIDw8IGVuZGw7CiAgICAgICAgdW5pcXVlX3B0cjxNeUNsYXNzPiB1cDIobmV3IE15Q2xhc3MpOwogCiAgICAgICAgLy91cDEgPSB1cDI7CQkgIC8vIFN5bnRheCBlcnJvcgogICAgICAgIHVwMSA9IG1vdmUodXAyKTsgIC8vIE93bmVyc2hpcCBtb3ZlZCBmcm9tIHVwMiB0byB1cDEKICAgICAgICB1cDIucmVzZXQoKTsJICAgIC8vIE5vIG1lbW9yeSBkZWFsbG9jYXRpb24KICAgICAgICBjb3V0IDw8ICJMZWF2aW5nIGlubmVyIGJsb2NrLiIgPDwgZW5kbDsKICAgIH0KICAgIGNvdXQgPDwgIkxlZnQgaW5uZXIgYmxvY2suIiA8PCBlbmRsOwogCiAgICB1cDEucmVzZXQoKTsJCSAgICAvLyBNZW1vcnkgZGVhbGxvY2F0aW9uCiAgICBjb3V0IDw8ICJMZWF2aW5nIG1haW4oKSBmdW5jdGlvbi4iIDw8IGVuZGw7CiAgICAKICAgIHJldHVybiAwOwp9IA==