-fno-exceptions documentation

Peter Dimov pdimov@mmltd.net
Thu Dec 8 12:34:00 GMT 2005


Howard Hinnant wrote:

> 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 ;-)).

The following appears to work for me.

functexcept.h:

#include <iostream>
#include <cstdlib>

template<class T> struct X
{
#ifndef _EXCEPTION_NEUTRAL

#ifdef __EXCEPTIONS

    static void throw_bad_alloc()
    {
        throw std::bad_alloc();
    }

#else

    static void throw_bad_alloc()
    {
        std::cerr << "Out of memory!\n";
        std::abort();
    }

#endif

#else

    static void throw_bad_alloc();

#endif
};

#ifndef _EXCEPTION_NEUTRAL

template struct X<void>;

#endif

The actual throwing is done by using X<void>::throw_bad_alloc. The helper 
template is only necessary to give the correct "linkage" to throw_bad_alloc 
so that multiple definitions do not result in an error.

A compiled library that wishes to be "exception neutral" (such as libstdc++ 
itself) should #define _EXCEPTION_NEUTRAL and would receive no definition of 
throw_bad_alloc, leaving the calls unresolved.

User code just #includes the above header (indirectly via any libstdc++ 
include) and receives an appropriate (for the current __EXCEPTIONS setting) 
definition for X<void>::throw_bad_alloc because of the explicit 
instantiation.



More information about the Libstdc++ mailing list