#include <iostream>
#include <memory>
using namespace std;
class Object {
public:
};
struct Exception {};
struct Wrapper {
Object *ptr = nullptr;
Object& operator *() {
if(ptr == nullptr)
throw Exception();
else
return *ptr;
}
};
int main() {
unique_ptr<Wrapper> up = make_unique<Wrapper>();
unique_ptr<Wrapper> up2 = make_unique<Wrapper>();
up2->ptr = new Object();
try {
Object& refToValid = **up2;
cout << "Dereferenced" << endl;
Object& ref = **up;
} catch(Exception& e) {
cout << "Exception" << endl;
delete up2->ptr;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgT2JqZWN0IHsKICAgIHB1YmxpYzoKfTsKCnN0cnVjdCBFeGNlcHRpb24ge307CgpzdHJ1Y3QgV3JhcHBlciB7CglPYmplY3QgKnB0ciA9IG51bGxwdHI7CglPYmplY3QmIG9wZXJhdG9yICooKSB7CgkJaWYocHRyID09IG51bGxwdHIpCgkJCXRocm93IEV4Y2VwdGlvbigpOwoJCWVsc2UKCQkJcmV0dXJuICpwdHI7Cgl9Cn07CgppbnQgbWFpbigpIHsKCXVuaXF1ZV9wdHI8V3JhcHBlcj4gdXAgPSBtYWtlX3VuaXF1ZTxXcmFwcGVyPigpOwoJdW5pcXVlX3B0cjxXcmFwcGVyPiB1cDIgPSBtYWtlX3VuaXF1ZTxXcmFwcGVyPigpOwoJdXAyLT5wdHIgPSBuZXcgT2JqZWN0KCk7Cgl0cnkgewoJCU9iamVjdCYgcmVmVG9WYWxpZCA9ICoqdXAyOwoJCWNvdXQgPDwgIkRlcmVmZXJlbmNlZCIgPDwgZW5kbDsKCQlPYmplY3QmIHJlZiA9ICoqdXA7Cgl9IGNhdGNoKEV4Y2VwdGlvbiYgZSkgewoJCWNvdXQgPDwgIkV4Y2VwdGlvbiIgPDwgZW5kbDsKCQlkZWxldGUgdXAyLT5wdHI7Cgl9CglyZXR1cm4gMDsKfQ==