Consider the following class:
struct S { ~S() = delete; };
Shortly and for the purpose of the question: I cannot create instances of
Why mark a destructor as delete
?
To prevent the destructor from being invoked, of course ;)
What are the use cases?
I can see at least 3 different uses:
To illustrate the latter point, imagine a C interface:
struct Handle { /**/ };
Handle* xyz_create();
void xyz_dispose(Handle*);
In C++, you would want to wrap it in a unique_ptr
to automate the release, but what if you accidentally write: unique_ptr
? It's a run-time disaster!
So instead, you can tweak the class definition:
struct Handle { /**/ ~Handle() = delete; };
and then the compiler will choke on unique_ptr
forcing you to correctly use unique_ptr
instead.