#include <iostream>
#include <memory>
using namespace std;
template <class T>
inline void out(const T& t) { cout << t << endl; }
inline void out(const char* str) { cout << str << endl; }
template <class T>
inline void out(const char* str, const T& t)
{ cout << str << ' ' << t << endl; }
class Obj
{
private:
static int count;
int num;
public:
Obj() : num(count++) { out("new", num); }
~Obj() { out("delete", num); }
};
int Obj::count = 0;
unique_ptr<Obj> foo() {
out("in foo");
unique_ptr<Obj> tmp(new Obj);
out("out foo");
return tmp;
}
int main() {
out("start");
unique_ptr<Obj> o(new Obj);
unique_ptr<Obj> e, p;
out("call foo");
o = foo();
e = move(o);
out("back to main");
o = foo();
p.swap(o);
out(o.get());
out(e.get());
out(p.get());
out("finish");
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGUgPGNsYXNzIFQ+CmlubGluZSB2b2lkIG91dChjb25zdCBUJiB0KSB7IGNvdXQgPDwgdCA8PCBlbmRsOyB9CmlubGluZSB2b2lkIG91dChjb25zdCBjaGFyKiBzdHIpIHsgY291dCA8PCBzdHIgPDwgZW5kbDsgfQp0ZW1wbGF0ZSA8Y2xhc3MgVD4KaW5saW5lIHZvaWQgb3V0KGNvbnN0IGNoYXIqIHN0ciwgY29uc3QgVCYgdCkKCXsgY291dCA8PCBzdHIgPDwgJyAnIDw8ICB0IDw8IGVuZGw7IH0KCmNsYXNzIE9iagp7CnByaXZhdGU6CglzdGF0aWMgaW50IGNvdW50OwoJaW50IG51bTsKcHVibGljOgoJT2JqKCkgOiBudW0oY291bnQrKykgeyBvdXQoIm5ldyIsIG51bSk7IH0KCX5PYmooKSB7IG91dCgiZGVsZXRlIiwgbnVtKTsgfQp9OwppbnQgT2JqOjpjb3VudCA9IDA7Cgp1bmlxdWVfcHRyPE9iaj4gZm9vKCkgewoJb3V0KCJpbiBmb28iKTsKCXVuaXF1ZV9wdHI8T2JqPiB0bXAobmV3IE9iaik7CglvdX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