c++ - Why is my overloaded << not returning any data? -


i have class loads vector<unsigned char> using asio's asynchronous methods. overloaded << operator return data.

the problem though buffer full , valid, operator not return 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 right size , step step debugging show content valid.

but when data back:

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

response empty. i've tried initialising length of expected response end buffer x null characters.

stumped this. shouldn't return statement copy or move value out?

response << socket; 

means

operator<<(response, socket); 

and in form it's clear you're discarding return value.

that code compiles prototype reads const vector<unsigned char>output hint you're never modifying output vector.

if want work stream insertion operators - appending left-hand side , being chainable, so:

 response << socket << another_socket; 

the first parameter should non-const reference, , should return non-const reference same object:

vector<unsigned char>& operator<<(vector<unsigned char>& output,                                   const shared_ptr<socket>socket) noexcept  {       output.insert(output.end(), socket->read_buffer.begin(), socket->read_buffer.end());       return output; } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -