In place std::move in pubsub

Viewed 105

In pubsub google official documentation https://cloud.google.com/pubsub/docs/pull we can find such example.

namespace pubsub = ::google::cloud::pubsub;
auto sample = [](pubsub::Subscriber subscriber) {
  return subscriber.Subscribe(
      [&](pubsub::Message const& m, pubsub::AckHandler h) {
        std::cout << "Received message " << m << "\n";
        std::move(h).ack();
        PleaseIgnoreThisSimplifiesTestingTheSamples();
      });
};

I am curious why std::move is used here. As I understand the only thing that std::move does is to cast an object to rvalue. I do not see any difference if it is used here or not. I would appreciate if someone could explain this.

1 Answers

I can't tell you why this is the way it is, but the reason for the call to std::move is that pubsub::AckHandler::ack is declared as void ack() && and the trailing && means that the function can only be called on rvalues. Since h is an lvalue, they used std::move to cast it back to an rvalue expression so that ack could be called.

Related