Listing 2: Mapping C++ to C

//
//  C++ external library's original source.
//
class CPP_library
    {
public:
    static int function(int a, int b) throw(int)
        {
        if (a >= b)
            throw 1;
        return 0;
        }
    };

//
//  Mapping layer.
//
//  Assumptions:
//
//  * The library function's name is mangled as
//
//    function__11CPP_libraryFii
//
//  * A long in the C source is the same size
//    as an int in the library source.
//
//  * The C compiler's parameter order is
//    reversed from the library's.
//
//  * The library function might throw.
//
extern "C" long C_library_function(int a, int b)
    {
    try
        {
        function__11CPP_libraryFii(long(b), long(a));
        }
    catch (long exception)
        {
        return exception;
        }
    return 0;
    }

/*
//  C source calling the C++ library
//  through the mapping layer
*/
int main(int argc, char **)
    {
    int result = C_library_function(argc, 0);
    return result;
    }
— End of Listing —