0

Let say I have the following code:

template<typename T> Matrix{
  private:
    int nr;
    std::vector<T> data;
  public:
    T& operator()(const int& j, const int& k){
    return this->mat.at(j+k*nr);}

    const T& operator()(const int& j, const int& k) const {
    return this->mat.at(j+k*nr);}
};

At some point I need to create a function pointer associated to const T& operator()(const int& j) const to use it as an argument in another function. Using functional, I tried:

//#include<functional>
function<const double&(const Matrix<double>&, const int&, const int&)> coef=&Matrix<double>::operator();

and I have the following error:

 no viable conversion from '<overloaded function type>' to 'function<const double &(const
  Matrix<double> &, const int &, const int &)>

I cannot get it to work and it is pretty hard to find informations on function, since the name is quite used...

EDIT

As pointed out in the comment section, it is not a duplicate and I don't think I should you use bind. I am just trying to apply the last example in http://www.cplusplus.com/reference/functional/function/function/ where they add a reference argument in the function pointer for a member function. The difference here is that my function member is an operator and I have not succeeded in doing the same as in the example. So, is there a syntax problem in what I am doing ? or may there is a subtlety with operators ?

9
  • This isn't a duplicate of the linked question, it is about overload resolution and address-of-member Commented May 31, 2017 at 15:18
  • @Rakete1111 I have seen it. But I don't understand why I should bind. I added const Matrix<double>& to the template argument used in the instantiation of coef. It actually work with usual methods (like in the last example of cplusplus.com/reference/functional/function/function) Commented May 31, 2017 at 15:19
  • const double&(Matrix<double::*member)(const int &, const int&) = &Matrix<double>::operator(); std::function<...> coef = member; will work. Similar is going on in stackoverflow.com/questions/16794695/… Commented May 31, 2017 at 15:20
  • @PierreMarchand Because it is a member function, you have to add an instance to call it one or make the function take an instance of any class. You don't have to use std::bind, a lambda works fine and is better anyways. Commented May 31, 2017 at 15:25
  • @Rakete1111 he's binding to the right function type, the problem is the non-const overload Commented May 31, 2017 at 15:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.