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 ?

Pierre Marchand
  • 619
  • 1
  • 7
  • 10
  • This isn't a duplicate of the linked question, it is about overload resolution and address-of-member – Caleth May 31 '17 at 15:18
  • @Rakete1111 I have seen it. But I don't understand why I should `bind`. I added `const Matrix&` to the template argument used in the instantiation of `coef`. It actually work with usual methods (like in the last example of http://www.cplusplus.com/reference/functional/function/function/) – Pierre Marchand May 31 '17 at 15:19
  • `const double&(Matrix::operator(); std::function<...> coef = member;` will work. Similar is going on in https://stackoverflow.com/questions/16794695/connecting-overloaded-signals-and-slots-in-qt-5 – Caleth May 31 '17 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. – Rakete1111 May 31 '17 at 15:25
  • @Rakete1111 he's binding to the right function type, the problem is the non-const overload – Caleth May 31 '17 at 15:46
  • @Caleth You sure? I thought that the first parameter has to be a pointer to the class, not a reference. – Rakete1111 May 31 '17 at 15:47
  • It's bullet 1 of pointer-to-member-function in http://en.cppreference.com/w/cpp/concept/Callable, not bullet 3. That is, a `std::function` is more general than `std::function`, in that it can be called with either pointers or references – Caleth May 31 '17 at 15:49
  • As I said, in http://www.cplusplus.com/reference/functional/function/function/ there is an example where they use a reference for a member function. So, my question can be marked as not a duplicate ? – Pierre Marchand May 31 '17 at 20:40
  • My question is still marked as duplicate and I have already edited it, what can I do ? – Pierre Marchand Jun 08 '17 at 08:03

0 Answers0