问题
If the class A
in unique_ptr<A>
it's own destructor, is it necessary to declare a deleter to ensure that the unique pointer uses that destructor? The example I am thinking of is that A
has a member mx
of type user_matrix
(a name I just made up) which needs to call a function free(...)
to release its memory, one would define
~A(){ user_matrix::free(mx); /*etc*/}
Since default_deleter<>
will call delete
, it is my understanding that that should use ~A()
. However, the example with the opening and closing of directories in Section 5.2, under "Deleters for Associated resources", of the book of Josuttis (The C++ Standard Library: A Tutorial and Reference) suggests one may need to declare a special deleter to do this, so I am confused.... Is this because, in the given example, the class DIR
doesn't have a destructor that uses closedir(...)
?
回答1:
The default deleter of std::unique_ptr<T>
will call delete
and the default deleter of std::unique_ptr<T[]>
will call delete[]
and those will call the destructors of the objects appropriately.
What may happen is that an operation need be scheduled right before the destruction, either because the destructor is incomplete (or lacking) or because you would like to do something more (for example, some logging). In this case you need a dedicated deleter to achieve this.
Suppose, for example, that you are given a handle, such as FILE*
(common in C). Those handles often come with a close
method of some sort and no destructor (because there is none in C).
struct FileCloser {
void operator()(FILE* f) { if (f) { fclose(f); } }
};
UniqueFile = std::unique_ptr<FILE, FileCloser>;
来源:https://stackoverflow.com/questions/17827608/when-does-stdunique-ptra-need-a-special-deleter-if-a-has-a-destructor