Search code examples
c++11std-functionstdbind

callbacks fails when std::bind and std::function used


There are two class server and client. client calls method datas() on server. The server responds to the caller with the datalist using datacallback().

I see compile time error in function clientfun2 when serverobj.datas() is called. Please help to fix it.

#include <iostream>
#include <functional>
#include <memory>
#include <string>
#include <vector>

enum Status { SUCCESS, FAILED, UNKOWN };

typedef std::vector<std::string> datalist;

class server {
 public:
  typedef std::function<void(int, Status, const datalist&)> Callback;
  void datas(int request_id, Callback datacallback) {
    datalist data; //here data is inserted and set to caller
    std::cout << "Invoked datas method\n";
    datacallback(123, SUCCESS, data); // sending back to caller
  }
};

class client {
 public:
  void clientfun1(int req_id, Status status, datalist& datas) {
    std::cout << "Invoked clientfun1\n";
  }

  void clientfun2(server serverobj) {
    serverobj.datas(123,
                    std::bind(&client::clientfun1, this, std::placeholders::_1,
                              std::placeholders::_2, std::placeholders::_3));  /*Here the error comming pls help to fix */
  }
};

int main() {
  server serverobj;
  client clientobj;
  clientobj.clientfun2(serverobj);
}

****Error in text format****

bind.cc: In member function ‘void client::clientfun2(server)’:
bind.cc:30:76: error: no matching function for call to ‘server::datas(int, std::_Bind_helper<false, void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&), client*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&>::type)’
                               std::placeholders::_2, std::placeholders::_3));
                                                                            ^
bind.cc:14:8: note: candidate: void server::datas(int, server::Callback)
   void datas(int request_id, Callback datacallback) {
        ^
bind.cc:14:8: note:   no known conversion for argument 2 from ‘std::_Bind_helper<false, void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&), client*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&>::type {aka std::_Bind<std::_Mem_fn<void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&)>(client*, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>)>}’ to ‘server::Callback {aka std::function<void(int, Status, const std::vector<std::__cxx11::basic_string<char> >&)>}’

error screen shot


Solution

  • If we take the std::function and the actual function right next to each other it's easy to see a difference (reformatted to show it better):

    //                                                    vvvvv
    typedef std::function<void(int       , Status       , const datalist&)> Callback;
    void clientfun1(           int req_id, Status status,       datalist& datas) {
    //                                                    ^^^^^
    

    Now we can see that for the std::function object the last argument is a reference to a constant datalist, while in the clientfun1 function it's not constant.

    That makes the two function types different, leading to your error.