dimanche 18 novembre 2018

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

When const Serviceclass used in std::function leads to compilation error. If remove const the complication is successful.

Code Flow : Client::GetValue() is called which calls on Server::datalist()

QUESTION : Why can't i use const on Serviceclass ?

SERVER SIDE

  //                                      vvvvv (if const remove then compliation success.)
  typedef std::function<void(int, Status, const Serviceclass&)>
    Callback;

    void Server::datalist(int Id, sessionId,Callback callback) {
    std::unique_ptr<Serviceclass> ServiceObj = Serviceclass::create();
    callback(request_id, RESULT,*ServiceObj.get());
    }

CLIENT SIDE

void Client::Boo(int Id, 
                Status,
                const Serviceclass& Serviceclass){
  //            ^^^^^ (if const remove then compliation success.)
}


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

Aucun commentaire:

Enregistrer un commentaire