mercredi 26 octobre 2016

Why is my overloaded << not returning any data?

I have a class that loads a vector<unsigned char> using Asio's asynchronous methods. I then overloaded the << operator to return that data.

The problem is that even though the buffer is full and valid, the operator does not return any data.

My buffer: vector<unsigned char>read_buffer;

Operator declaration:

friend std::vector<unsigned char> operator<<(const vector<unsigned char>output, const shared_ptr<Socket>socket) noexcept;

The implementation:

std::vector<unsigned char> operator<<(const vector<unsigned char>output,
                                           const shared_ptr<Socket>socket) noexcept {
      std::cerr << output.size() << std::endl;
      std::cerr << socket->read_buffer.size() << std::endl;
      return socket->read_buffer;
    }

where std::cerr << socket->read_buffer.size() << std::endl; has the right size and step by step debugging show its content is also valid.

But when I get data back:

vector<unsigned char> response;
response << socket;

response is empty. I've tried initialising it to the length of the expected response but I just end up with a buffer with X null characters.

Stumped by this. Shouldn't a return statement copy or move the value out?

Aucun commentaire:

Enregistrer un commentaire