On Fri, 2011-10-28 at 13:12 -0700, Andrew W. Steiner wrote: > Yeah I wouldn't suggest requiring GCC 4.5. However, I was thinking > it might be useful to have some lambda function boilerplate around > somewhere, just in case there was a C++ user who wanted a quick > way to use a GSL function without having to resort to more drastic > measures.
I don’t think lambda functions will work because you’ll end up taking the address of a temporary function and I don’t think there’s any guarantee the function will exist at the point you want to use it. But I’d love to be proved wrong. The best solution I’ve managed to come up with is to use template functions like: template<typename T,double (T::*F)(double) = &T::operator()> double GSLFunction( double x, void* params ){ return (reinterpret_cast<T*>(params)->*F)( x ); } template<typename T,double (T::*F)(double),double (T::*DF)(double)> void GSLFunction( double x, void* params, double* f, double* df ){ *f = (reinterpret_cast<T*>(params)->*F)( x ); *df = (reinterpret_cast<T*>(params)->*DF)( x ); } Then, for example with a class — struct Quadratic { Quadratic( double a, double b, double c ); double operator()( double x ); double df( double x ); }; — I can write code like Quadratic q( 1, 2, 1 ); gsl_function_fdf fdf; fdf.f = GSLFunction<Quadratic>; fdf.df = GSLFunction<Quadratic,&Quadratic::df>; fdf.fdf = GSLFunction<Quadratic,&Quadratic::operator(),&Quadratic::df>; fdf.params = reinterpret_cast<void*>( &q ); and gsl can handle it. This still has all sorts of problems. You need more template classes for const member functions and it assumes that the class members have a particular form. My own preference (http://sourceforge.net/projects/ccgsl/) is to wrap the gsl functions using headers so that you can pass C++-style arguments. But I haven’t yet tried this with root-finding and the like. -- John D Lamb _______________________________________________ Help-gsl mailing list Help-gsl@gnu.org https://lists.gnu.org/mailman/listinfo/help-gsl