Kurt Vanmechelen and Jan Broeckhove
University of Antwerp
BE-2020 Antwerp, Belgium
emails: {kurt.vanmechelen,jan.broeckhove}@ua.ac.be
Object oriented programs in C++ can use a number of callable entities: 'plain' function or pointer to function, functors, and member functions. Interfacing functors or member functions with numerical or scientific libraries developed in C, proves to be difficult as library routines can only accept a function or pointer-to-function as a callback argument. The type of that argument is fixed by some function prototype declared in the header file of the library, e.g. double derivative(double X, double (*funcToDerive)(double));. This syntactic structure prohibits the use of functors and member functions as callbacks, even if they have the required argument list and return type. This is actually as it should be; a functor^Òs call operator or member function in general, needs to be bound to a specific object instance on which the call will be invoked, in order for the call to make sense. Usually this limitation is addressed by writing an ad hoc wrapper function that conforms to the C prototype. This approach has several drawbacks which are outlined in this contribution.
We propose and develop a more flexible and generic solution to the problem of mapping functor or object-member function pairs to plain C functions using template programming techniques. The two key elements of our solution are the use of static member functions as wrappers and the compile time generation of a number of such wrapper functions through recursive template instantiation. We present an extensive performance analysis of our solution on a number of processor/compiler configurations, in order to evaluate its usefulness in a high performance computing context.