将 const class 对象传递给 std::function 和 std::bind 导致编译错误

Passing const class object to std::function and std::bind lead compilation error

When const Serviceclass used in std::function 导致编译错误。 如果删除 const 复杂化成功。

代码流 : Client::GetValue() 调用 Server::datalist( )

问题: 为什么我不能在 Serviceclass 上使用 const

服务器端

     //                                      vvvvv (if const remove then compliation success.)
      typedef std::function<void(int, Status, const Serviceclass&)>
        Callback;
        //SECOND INVOKED
        void Server::datalist(int Id, sessionId,Callback callback) {
        std::unique_ptr<Serviceclass> ServiceObj = Serviceclass::create();
        callback(request_id, RESULT,*ServiceObj.get());
        }

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

       typedef std::function<void(int, Status, const dataList&)>callback_data;
       //FOURTH INVOKED
       void Server::getdata(int Id,callback_data){
        // callback called with datalist
       }

客户端

  //FIFTH INVOKE
 void Client::Foo(int Id,Status,const dataList& data){
    //impl
   }


 //THIRD INVOKED
void Client::Boo(int Id, 
                Status,
                const Serviceclass& Serviceclass){
  //            ^^^^^ (if const remove then compliation success.)
             Serviceclass.getdata(123,
                std::bind(&Client::Foo, this, std::placeholders::_1,
                          std::placeholders::_2, std::placeholders::_3));
}

//FIRST INVOKED
void Client::GetValue() {
        int Id = 123; 
        serverobj->datalist(
                        Id, sessionId,
                        std::bind(&Client::Boo, this, std::placeholders::_1,
                                std::placeholders::_2, std::placeholders::_3));
}

错误

In member function 'void Client::Boo(int, Status, const Serviceclass&)':
mycode.cc:68:76: error: passing 'const Serviceclass&' as 'this' argument discards qualifiers [-fpermissive]
                               std::placeholders::_2, std::placeholders::_3));

将成员函数设为const或将Serviceclass的非const对象设为const。

//                                        ^^^^^ add const
void Server::getdata(int Id,callback_data)const {
        // callback called with datalist
       }