Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 815
Daniel Grooms Avatar asked Jul 03 '26 00:07

Daniel Grooms


1 Answers

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);
like image 75
David Brown Avatar answered Jul 05 '26 12:07

David Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!