fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. template <typename T>
  5. std::shared_ptr<T> OwnedPtr(T* object)
  6. {
  7. return {object, [] (T*) {}};
  8. }
  9.  
  10. int main() {
  11. std::shared_ptr<int> ptr;
  12.  
  13. // some condition (dans ton code)
  14. int global_permanent_value = 456;
  15.  
  16. if (true)
  17. ptr = std::make_shared<int>(123);
  18. else
  19. ptr = OwnedPtr(&global_permanent_value);
  20.  
  21. std::cout << *ptr << std::endl;
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
123