How does =delete on destructor prevent stack allocation?
I have two answers:
From "The C++ Programming Language":
You can’t have a local variable that can’t be destroyed (§17.2.2) ...
In my own words:
Think about it for a minute - a variable created on the stack is meant to be deleted at the end of scope in which it is declared.
By disallowing the object's implicit destructor, you are basically telling the compiler "You are not permitted to delete this object at the end of scope." The compiler doesn't want to take a risk of creating something that can't be destroyed (and it's right - what if it's large and will leak megabytes of memory?), so it refuses to create it. That leaves you only one option - to create it on the free store and manually manage it.
Now the responsibility is yours. Note, however, that this is bad code - the objects will never be able to be delete
d, even manually. Remember - you delete
d the destructor! As it was correctly pointed out in other answers, this is a forced memory leak and should be avoided.
The destructor of a variable with automatic storage duration (i.e. a local variable) would need to run when the variable's lifetime ends. If there is no accessible destructor the compiler refuses to compile the code that allocates such a variable.
Basically the distinction between "stack allocation" (an inaccurate choice of term by the way) and free store allocation is that with local variables constructor/destructor calls always come in pairs, whereas with free store allocation you can construct an object without ever destructing it. Therefore by preventing access to the destructor your code makes it impossible to allocate a local variable of the type (if the constructor runs the destructor must also run, but there is no destructor so the program is rejected).
Marking a destructor as deleted will make it impossible to destroy the object. It doesn't matter if it's on the stack or on the heap. All destruction of an object (whether automatic by it going out of scope, or by doing delete
on it) calls the destructor. And as it is the compiler that handles the calling of the destructor, it will notice if it's been marked as deleted and issue an error.
It doesn't however disallow the creation of objects, but as non-pointer instances are destructed on the end of the scope where they were declared, the compiler will issue an error, in effect disallowing creation of the instance. It's still possible to dynamically allocate instances using new
, with the caveat that they can't be deleted, possibly leading to a memory leak.
I understand, that is not possible to delete this instance, either explicitly or implicitly.
More than that, it's not possible to destroy any instance; whether by deleting it or otherwise.
Declaring an automatic variable (or "stack allocation", if you like) doesn't just cause the instance to be created when the program reaches the point of declaration; it also causes it to be destroyed when the program leaves that block. With a deleted destructor, that can't be done, so the declaration is not allowed.
This also prevents you from declaring a static or thread-local variable, since that also generates code to destroy the variable when the program or thread ends.
So the only way to create one of these is with new
, and once you've done that you can never destroy it. However, this doesn't altogether prevent stack allocation:
char memory[sizeof(FS_Only)] alignas(FS_Only);
FS_Only * not_fs = new (memory) FS_Only;
Also why would one need this?
In my view, you wouldn't. A mandatory memory leak is a horrible way to ensure that an object is never destroyed at the wrong time. Instead, use techniques such as RAII to manage any objects that need a dynamic lifetime, ensuring that they always have a well defined owner (or shared owners) responsible for deleting them after use. The smart pointers in the C++11 standard library are a good starting point.