V
Victor Bazarov
SuperKoko said:[..]
IMHO, this problem would be dramatically reduced if C++
implementations had better diagnostic messages.
For instance, compilers should do a warning when:
1) A pointer to an incomplete type (or the void type) is the static
type of the argument of a delete-expression.
Actually, the behavior is undefined if such statement is executed.
2) A pointer to an abstract class having no virtual destructor is the
static type of the argument of a delete-expression.
Since one can't have instances of an abstract class whose dynamic type
is equal to the static type, behavior is always undefined if such
statement is executed.
These two warning messages would resolve a bit number of UB.
It would also be possible to add a warning message when defining a
class having virtual functions but a public non-virtual destructor
(protected non-virtual destructor and public virtual destructor would
be accepted).
Actually, these two or three warning messages would solve all problems
related to virtual destructors except if one derive from a concrete
class, which is often a bad idea.
However, there are classes which are conceptually abstract (such as
std::binary_function). In that case, such classes should have a
protected destructor (unfortunately, it has a public destructor).
And, it would be possible, for a compiler, to emit a warning message
when deriving from a class which is neither abstract nor have a
protected destructor.
I know, that this message should be sometimes ignored... But, at
least, you know where you do dangerous things.
Well, to me it seems that you're talking about a QoI issue. Why put
the requirement about warning messages in the Standard? Choose the
compiler that gives the warnings you need, talk to the vendors of C++
implementations about what warnings you'd like to see, and you shall
receive. There is also room for PC-lint and the like. There is *no*
need to *require* those things to exist at the language level. C++
specification says "UB". That should be enough.
Besides, it's not really a solution for the "need for virtual d-tors"
problem. It's a work-around. The solution would be if the compiler
did automatic recognition of the fact that the object being destroyed
was originally created as part of a larger object. To do that you
need a virtual d-tor *or* some other mechanism. I see only two
solutions: either d-tors are always virtual or there is some other
way in *run-time* for the program to tell a complete object (so to
speak) from a part, when deleting.
V