I would like to periodically http GET some value from a website. with beast, the code is like:
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, version};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::response<http::string_body> res;
while (true) {
// Send the HTTP request to the remote host
http::write(stream, req);
res = {};
// Receive the HTTP response
http::read(stream, buffer, res);
// Write the message to standard out
std::cout << res << std::endl;
}
But in the loop,
res = {}
brought one temporary object creation, one move/copy assignment and the destroy of the temporary object. I am wondering if there is a better way to avoid these unnecessary costs。

Just remove the offending line and move the declaration of
resinside thewhileloop.reswill then be created and destroyed each time round the loop: