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.
new
to create an instance on the heap but if you neverdelete
it it will compile fine. – Barbershop