This is to demonstrates how shared_ptr can be smart enough to use as RAII in different scenarios other than just memory management.
In this demo, we use it for closing file descriptor in normal and exception conditions.
- RAII for file, file will be closed in normal flow and exception.
- RAII enables us to avoid exception handling just to close file.
- shared_ptr will never call it's own deleter but will be replaced by local lambda function provided.
- So, File object is never deleted by shared_ptr but closed only.
- File object will be deleted only when it goes out of scope but not by smart_ptr - shared_ptr.
- It also shows how try-catch can be used in constructor to inform client that object creation has failed