"Automatically" creating non-member wrappers for C++ member functions -
i have class bunch of member functions mathematical operation , return result. example:
class foo { public: double f(double); double g(double); double h(double); // ... } double foo::f(double foo1) { // ... } // , on
at several points in program i'm working on, need numerically integrate of these functions. numerical integration routine i'd use has signature
extern "c" double qgauss(double (*func)(double, void*), void* data, double a, double b);
and i've been reading seems best way pass member functions integration routine create wrapper functions:
double f_wrapper(double x, void* data) { return ((foo*)data)->f(x); }
but dozen member functions f
, g
, h
, etc. wrap, , perhaps more added later, gets pretty repetitive. can (or perhaps should) use template or macro or condense amount of code have write in order create these wrapper functions?
as aside, solution i'm using reimplement integration routine c++ function template accepts object parameter directly:
template <class c> double qgauss(c* obj, double (c::*func)(double), double a, double b) { // ... }
but involves large-scale duplication of code, don't like. if has better solution either creating wrapper functions or implementing c++ version of integrator, interested hear , can ask separate question if more appropriate.
you try templates:
template <class c, c::*func> double wrapper(double x, void* data) { return ((c*)data)->*func(x); } qgauss(&wrapper<c, &c::f>, data, a, b);
now, haven't tried this, , there may issue because want address of function template--and worse yet think technically need extern "c"
function. if above indeed dead end, think should use macros, after c programming you're doing @ point, , macros natural that.
Comments
Post a Comment