This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Why does debugging "pure virtual method called" have to be so hard?
- To: Joe Buck <jbuck at synopsys dot com>
- Subject: Re: Why does debugging "pure virtual method called" have to be so hard?
- From: Alexandre Oliva <oliva at dcc dot unicamp dot br>
- Date: 26 Oct 1998 21:47:42 -200
- Cc: jason at cygnus dot com, mrs at wrs dot com, egcs at cygnus dot com
- References: <199810262017.MAA17869@atrus.synopsys.com>
I wrote:
>> ... calling a pure virtual function has undefined behavior ...
Joe Buck <jbuck@Synopsys.COM> writes:
> which means we can do what we want.
> I vote for
> throw __pure_virtual_exception;
> where that class is derived from class std::exception.
The more I think of it, the more I dislike this too... Given the
following code snippet:
class foo {
virtual void bar() = 0;
foo() {
try {
bar();
} catch (...) {
// do something
}
}
};
It would seem strange to have an exception caught in this case... Of
course, the behavior is undefined, so we can do whatever we want, but
it just sounds too unexpected to me... Also, unlike calling abort(),
this wouldn't help debugging very much. OTOH, by throwing an
exception, it would become possible to redefine the behavior of
calling a pure virtual function, and try to work around failures,
which is something one might want to do.
It seems to me that a better solution would be to implement
__pure_virtual in a way similar to terminate and unexpected, i.e., by
providing a default implementation that abort()s, but allowing the
user to modify this behavior by running __set_pure_virtual() or
something like that. The default pure_virtual handler could be
abort(), and we could provide an alternate handler, say
__pure_virtual_throw, that would throw
__pure_virtual_exception (: public std::exception).
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:oliva@gnu.org mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil