Daniel Grooms
Daniel Grooms

Reputation: 31

C++ Callback Function to Member Function

I have never worked with callbacks, but the following code should work according to my professor's notes. It doesn't like the template and has errors about "gauss cannot appear in a constant-expression." Note: GaussElim is a function object (gauss(mx, vector) works in previous code).

The DirichletSolver templated 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);
}

The Gauss operator() overload signature:

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 have to do to get this to work?

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

Upvotes: 3

Views: 118

Answers (1)

David Brown
David Brown

Reputation: 13536

The second template parameter for solve is expecting a function, not a functor. Specifically a function with the signature Vector<T> ()(const AbstractMatrix<T>&, const Vector<T>) for the given template parameter T.

gauss.operator() doesn't make sense, maybe you meant GaussElim::operator() however that won't work either because it is a member function. If you can write whatever the implementation of GaussElim::operator() is as a free function you can use that as the template parameter:

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

And then call it with

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

Upvotes: 1

Related Questions