Does anyone know why the STL containers don't have virtual destructors?
As far as I can tell, the only benefits are:
The downside is that it's unsafe to subclass the containers in the usual way.
EDIT: Perhaps my question could be rephrased "Why weren't STL containers designed to allow for inheritance?"
Because they don't support inheritance, one is stuck with the following choices when one wants to have a new container that needs the STL functionality plus a small number of additional features (say a specialized constructor or new accessors with default values for a map, or whatever):
As a side question: is there a standards-safe way of subclassing with non-virtual destructors (let's assume that I don't want to override any methods, just that I want to add new ones)? My impression is that there is no generic and safe way of doing this if one does not have the power to change the code defining the non-virtual class.
EDIT 2:
As @doc points out, C++ 11's fancier using
declarations lower the cost of composition somewhat.
A virtual destructor is only useful for inheritance scenarios. STL containers are not designed to be inherited from (nor is it a supported scenario). Hence they don't have virtual destructors.