c++ - When does std::unique_ptr<A> need a special deleter if A has a destructor? -


if class a in unique_ptr<a> it's own destructor, necessary declare deleter ensure unique pointer uses destructor? example thinking of a has member mx of type user_matrix (a name made up) needs call function free(...) release memory, 1 define

~a(){ user_matrix::free(mx); /*etc*/} 

since default_deleter<> call delete, understanding that should use ~a(). however, example opening , closing of directories in section 5.2, under "deleters associated resources", of book of josuttis (the c++ standard library: tutorial , reference) suggests 1 may need declare special deleter this, confused.... because, in given example, class dir doesn't have destructor uses closedir(...)?

the default deleter of std::unique_ptr<t> call delete , default deleter of std::unique_ptr<t[]> call delete[] , call destructors of objects appropriately.

what may happen operation need scheduled right before destruction, either because destructor incomplete (or lacking) or because you more (for example, logging). in case need dedicated deleter achieve this.

suppose, example, given handle, such file* (common in c). handles come close method of sort , no destructor (because there none in c).

struct filecloser {     void operator()(file* f) { if (f) { fclose(f); } } };  uniquefile = std::unique_ptr<file, filecloser>; 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -