unique_ptr
- Use it for exclusive ownership resource management
- When we are done with heap allocation we need to call delete, the destructor of unique_ptr does that for us.
- Rust ownership is transferring
unique_ptr
because if it is copyable, and two pointer reference to the same address, it is unclear who may have the responsibility to clear it
- It is
move
only, makes ownership clearer
- Smart pointer should be treated like regular pointer, pass by value, return by value. 8 byte.
template<class T, class Deleter = std:: default_delete<T>>
class unique_ptr {
T *p_ = nullptr;
Deleter d_;
~unique_ptr() {
if (p_) d_(p_);
}
}
template<class T>
struct default_delete {
void operator()(T* p) const {
delete p
}
}
// Need to override default close function
struct FileCloser {
void operator()(FILE *fp) const {
if (fp != nullptr) {
fclose(fp);
}
}
}
FILE *fp = fopen("INPUT.txt", "r");
std :: unique_ptr<FILE, FileCloser> uptr(fp);