Why can I even call delete this given that the function is const
Because delete
doesn't necessarily modify the deleted pointer.
Is the behaviour on returning 0 defined? Doesn't the function "sort-of-die" after the delete?
Why would it? The logic isn't removed from the program. The functionality stays in memory for next objects to use it. It's only this
pointer that gets invalidated.
There's an SO answer that points to The ISO C++ FAQ which has an entry on that, excerpts of which:
- You must be absolutely 100% positively sure that the rest of your member function (after the delete this line) doesn’t touch any piece of this object (including calling any other member functions or touching any data members). This includes code that will run in destructors for any objects allocated on the stack that are still alive.
- You must be absolutely 100% positively sure that no one even touches the this pointer itself after the delete this line. In other words, you must not examine it, compare it with another pointer, compare it with nullptr, print it, cast it, do anything with it.
Since your code abides those rules, again, it's fine.