[cfe-dev] is delete on abstract with non-virtal ever safe?
clang at lucena.com
Mon Jul 23 13:48:17 PDT 2012
On Jul 23, 2012, at 12:40 PM, David Blaikie wrote:
>> Which reminds me: I don’t follow the standard proceedings at all, but why shouldn’t the implicitly declared destructor in a class with a virtual member function not be made virtual by default? The only other option that makes sense is to make it protected and nonvirtual, though that is probably less often what the class designer wants. I thought this might change in C++11, and find it makes even more sense given the new rules about special member functions.
> The doBaseStuff calls virtual functions from a base reference (perhaps
> the author doesn't want the code bloat of a template, or it's
> statically compiled in another library, etc) but the object itself is
> never polymorphically owned so it's not polymorphically destroyed and
> thus doesn't need a virtual destructor.
Hence why I mentioned protected non-virtual destructors. If your destructor is public, no force on Earth will keep some ninny from calling it directly at some point, documentation and good sense be damned.
> derived's dtor doesn't need to be virtual as shared_ptr's type erasure
> machinery guarantees that it'll be destroyed the same way make_shared
> created it, with a derived*, not a base*.
Once again, a protected nonvirtual destructor guards against this. But, once again, since it’s the less frequently desired behavior, it would make sense for the default to be a virtual public destructor for classes that have a vtable anyway, and let implementors explicitly declare it otherwise if that’s what they desire.
More information about the cfe-dev