Run-time support routines for throwing bad_cast and bad_typeid
Nathan Sidwell
nathan at codesourcery.com
Thu Jun 29 10:47:25 UTC 2000
Mark Mitchell wrote:
>
> Both EDG and G++ call run-time library routines to throw the bad_cast
> and bad_typeid exceptions, rather than trying to expand the throws
> inline. This is much more convenient since those exceptions can be
> thrown without the headers declaring bad_cast being included. I think
> we should follow this exisitng practice and provide appropriate entry
> points. How about:
>
> extern "C" void __cxa_bad_cast ();
> extern "C" void __cxa_bad_typeid ();
FYI, the G++ declarations are
extern "C" void *__throw_bad_cast ();
extern "C" std::type_info const &__throw_bad_typeid ();
of course these never actually return, but it causes least
confusion at the calling point by keeping the type system consistent.
These are called with something like the following pseudo C++
for dynamic_cast <T &> (lvalue)
(void *tmp = __dynamic_cast (...),
*(T*)(tmp ? tmp : __throw_bad_cast ()))
for typeid (*ptr)
(ptr ? *(type_info const *)ptr->vtable[-1] : __throw_bad_typeid ())
One side of a conditional expr can be void, but only if it is a throw
expression, wrapping up the throws in function calls hides that, and
in g++'s case caused problems. The easiest solution was the above
declarations.
I suggest the following
extern "C" void *__cxa_bad_cast ();
extern "C" const void *__cxa_bad_typeid ();
That typeid signature will mean a little reworking of the typeid operator
implementation for G++, but not too much. For implementations where Mark's
suggestion is valid, these will be too, but not vice-versa.
nathan
--
Dr Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
'But that's a lie.' - 'Yes it is. What's your point?'
nathan at codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan at acm.org
More information about the cxx-abi-dev
mailing list