C ++ callback function for member function

I have never worked with callbacks, but the following code should work as per my professor's comments. He doesn't like the pattern and has bugs about "gauss cannot appear in a constant expression". Note. GaussElim is a functional object (gauss (mx, vector) works in the previous code).

DirichletSolver callback function:

template <class T, Vector<T> matrixAlgo(const AbstractMatrix<T>&, const Vector<T>)>
Vector<T> DirichletSolver::solve(const AbstractMatrix<T>& mx, const Vector<T> vect)
{
  return matrixAlgo(mx, vect);
}

      

Gaussian overload signal ():

template <class T>
Vector<T> operator()(const AbstractMatrix<T>& mx, const Vector<T> vect);

      

And the driver code:

GaussElim gauss;
DirichletSolver dir;
SymMatrix<double> mx;
Vector<double> vect;
...
dir.solve<gauss.operator()>(mx, vect);

      

What do I need to do to get this to work?

Will this work for my functor? (I have two more to implement)

+3


source to share


1 answer


The second template parameter for solve

expects a function, not a functor. In particular, a function with a signature Vector<T> ()(const AbstractMatrix<T>&, const Vector<T>)

for a given template parameter T

.

gauss.operator()

doesn't make sense, maybe you meant GaussElim::operator()

, however it won't work because it's a member function. If you can write any implementation GaussElim::operator()

as a free function, you can use it as a template parameter:

template <class T>
Vector<T> myFunc(const AbstractMatrix<T>& mx, const Vector<T> vect)
{
    // contents of GaussElim::operator()
}

      



And then call it

dir.solve<double, myFunc>(mx, vect);

      

+1


source







All Articles