fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. class Object {
  6. public:
  7. };
  8.  
  9. struct Exception {};
  10.  
  11. struct Wrapper {
  12. Object *ptr = nullptr;
  13. Object& operator *() {
  14. if(ptr == nullptr)
  15. throw Exception();
  16. else
  17. return *ptr;
  18. }
  19. };
  20.  
  21. int main() {
  22. unique_ptr<Wrapper> up = make_unique<Wrapper>();
  23. unique_ptr<Wrapper> up2 = make_unique<Wrapper>();
  24. up2->ptr = new Object();
  25. try {
  26. Object& refToValid = **up2;
  27. cout << "Dereferenced" << endl;
  28. Object& ref = **up;
  29. } catch(Exception& e) {
  30. cout << "Exception" << endl;
  31. delete up2->ptr;
  32. }
  33. return 0;
  34. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Dereferenced
Exception