I know already that some forms of "suicide" are safe (to be considered legal), but, is it specifically safe to perform delete this
in a virtual member function?
Note, by "safe", I mean whether the "code" generated by the compiler is able to deal with the construct.
Note, I'm not interested in the pros and cons of doing it, just whether I can consider is safe.
Side question: Does the language standard explicitly or implicitly demand that implementations support any forms of the delete this
idiom?
I do not consider this a duplicate of Is delete this allowed?. My question is about whether it is safe to do in a virtual member function.
Here is an outline of what I am pursuing to do
class FooBase {
protected:
virtual void on_idle() { /* no-op by default */ }
};
class Foo : public FooBase {
void on_idle() override final
{
delete this;
}
};
Note that while Foo
needs to be heap allocated, other subclasses possibly do not.