I have a quadrature class for which I would like to be able to pass any integrand into, regardless of whether the function defining the integrand is a free function or a member function of another class. For the latter case, the best progress I've made is like the following.
Class ChiSquared contains a pointer to the GaussQuadrature class and passes a member function to the GaussQuadrature named Quad like so:
double ChiSquared::LowerIncompleteGammaIntegrand(double x) {
return pow(x, (5/2.0)-1)*exp(-x);
}
double ChiSquared::LowerIncompleteGamma(double x) {
Quad->Quadrature(&ChiSquared::LowerIncompleteGammaIntegrand, other args...);
return Quad->getQuad();
}
In the GaussQuadrature template class, the Quadrature function that accepts this call is
template<typename T>
void GaussQuadrature<T>::
Quadrature(double (ChiSquared::*Integrand)(double), other args...)
{
T val = Integrand(argument);
...
// other math stuff
}
Firstly, This generates the error
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘Integrand (...)’, e.g. ‘(... ->* Integrand) (...)’
which makes it seem like I need an instance of ChiSquared in the GaussQuadrature class, not the other way around, and I want to avoid this because:
Secondly, I want to decouple the GaussQuadature class from any other classes, as mentioned before. Ideally, ANY function would be able to be passed in as an "integrand" and so I don't want to have to declare a separate Quadrature function for each possible type of class the "integrand" could come from. Could I use some sort of function template here?
ChiSquared::LowerIncompleteGammaIntegrandstatic it will be free (not have a this).lambdas, then you can wrap it in an anonymous function, that can bindthis.other args...the pointer to theChiSquaredobject on which to do the firing. To do your second requirement would need an overload using a non member function pointer.