#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); }
void o() { out("mynum", num); }
};
int Obj::count = 0;
void foo(auto_ptr<Obj> f) {
out("in foo(auto_ptr):", f.get());
}
void foo(shared_ptr<Obj> f) {
out("in foo(shared_ptr):", f.get());
}
void foo(unique_ptr<Obj> f) {
out("in foo(unique_ptr):", f.get());
}
void bar(auto_ptr<Obj>& b) {
out("in bar(auto_ptr):", b.get());
}
void bar(shared_ptr<Obj>& b) {
out("in bar(shared_ptr):", b.get());
}
void bar(unique_ptr<Obj>& b) {
out("in bar(unique_ptr):", b.get());
}
int main() {
out("start");
auto_ptr<Obj> a(new Obj);
shared_ptr<Obj> s(new Obj);
unique_ptr<Obj> u(new Obj);
out("a:", a.get()); out("s:", s.get()); out("u:", u.get());
foo(a);
foo(s);
foo(move(u)); // foo(u); はエラー
out("a:", a.get()); out("s:", s.get()); out("u:", u.get());
out("reset");
a.reset(new Obj);
s.reset(new Obj);
u.reset(new Obj);
out("a:", a.get()); out("s:", s.get()); out("u:", u.get());
bar(a);
bar(s);
bar(u);
out("a:", a.get()); out("s:", s.get()); out("u:", u.get());
out("finish");
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGUgPGNsYXNzIFQ+CmlubGluZSB2b2lkIG91dChjb25zdCBUJiB0KSB7IGNvdXQgPDwgdCA8PCBlbmRsOyB9CmlubGluZSB2b2lkIG91dChjb25zdCBjaGFyKiBzdHIpIHsgY291dCA8PCBzdHIgPDwgZW5kbDsgfQp0ZW1wbGF0ZSA8Y2xhc3MgVD4KaW5saW5lIHZvaWQgb3V0KGNvbnN0IGNo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