-fno-exceptions documentation
Peter Dimov
pdimov@mmltd.net
Tue Dec 6 19:16:00 GMT 2005
Howard Hinnant wrote:
>> http://www.boost.org/libs/utility/throw_exception.html
>
> Thanks Peter. That's a cleaner solution as it moves the #ifdef
> __EXCEPTIONS out of op new and into throw_exception(), although we
> might also want to pass a const char* to make it a little easier in
> the non-throwing case, maybe not for bad_alloc... :-)
>
> Anyway, if you come up with a way to pull this off without having to
> recompile op new (or another part of the lib) when flipping
> exceptions on/off, that would really be cool! (your challenge for the
> week ;-)).
If libstdc++ only throws standard exception types (bad_alloc, out_of_range,
length_error, runtime_error, bad_cast, overflow_error) it could be possible
to either:
1. Declare
template<class E> void __throw_exception( E const & e );
and provide specializations for the above types, or
2. Declare separate functions for every exception:
void __throw_bad_alloc();
void __throw_out_of_range();
// ...
libstdc++ wouldn't need a recompile, but one tiny .o would still need to
(somehow) be chosen conditionally depending on whether __EXCEPTIONS is on.
A variation of option (1) may even inject the implementation in user code,
where __EXCEPTIONS can be queried:
// before other libstdc++ things
template<class E> void __throw_exception( E const & e );
template<> void __throw_exception<bad_alloc>( bad_alloc const & e );
// libstdc++ code
// suffix header, only included in user code, not when building libstdc++
#ifdef __EXCEPTIONS
template<> void __throw_exception<bad_alloc>( bad_alloc const & e )
{
throw e;
}
#else
template<> void __throw_exception<bad_alloc>( bad_alloc const & e )
{
std::cerr << e.what() << std::endl;
abort();
}
#endif
But I haven't tried this; it certainly seems fragile and I'm not entirely
sure that it's standard-conforming. It probably isn't. :-)
More information about the Libstdc++
mailing list