-fno-exceptions documentation

Howard Hinnant hhinnant@apple.com
Tue Dec 6 18:46:00 GMT 2005


On Dec 6, 2005, at 5:56 AM, Peter Dimov wrote:

> Howard Hinnant wrote:
>
>> So for  http://gcc.gnu.org/ml/gcc/2004-10/msg00099.html, my knee-jerk
>> reaction is to say "recompile libstdc++" with that setting if you
>> need it to not thrown an exception".  That being said, my response is
>> probably not what the customer wanted either.  He wanted op new to
>> return 0 on failure, not abort.  In general it would be nice to give
>> the customer the ability to customize error handling when exceptions
>> are turned off.  For example instead of calling abort() in an
>> exceptional situation we might instead call a weak-linked
>> __gcc_handle_error(const char* msg).  The customer then might
>> override this function, inspect the message, and do something that
>> makes sense for them.  The default implementation could just call
>> abort(), maybe after printing out the msg.  This still requires a
>> recompile of libstdc++, but at least it gives the customer another
>> option.  Here's how it might look:
>>
>> operator new (std::size_t sz) throw (std::bad_alloc)
>> {
>>  ...
>>       if (! handler)
>> #ifdef __EXCEPTIONS
>> throw bad_alloc();
>> #else
>>         std::__gcc_handle_error("bad_alloc");
>> #endif
>>    ...
>> }
>
> This is what I do in Boost:
>
> void * operator new (std::size_t sz) throw (std::bad_alloc)
> {
>    if( !handler )
>    {
>        boost::throw_exception( std::bad_alloc() );
>        return 0;
>    }
> }
>
> The default boost::throw_exception( x ) throws x, but when  
> exceptions are off, is left undefined for the user to supply an  
> appropriate implementation. In libstdc++, it could have a weak  
> definition that aborts by default.
>
> 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 ;-)).

-Howard



More information about the Libstdc++ mailing list