问题
我们可以使用移动语义来移动堆栈上的堆分配对象吗?
例子
#include <boost/asio.hpp>
#include <memory>
class connection
{
public:
connection(boost::asio::ip::tcp::socket&& socket);
void start();
private:
boost::asio::ip::tcp::socket m_socket;
};
class server
{
public:
// Not relevent here
private:
void accept();
boost::asio::io_service m_io_service;
boost::asio::ip::tcp::acceptor m_acceptor;
};
void server::accept()
{
auto socket = new boost::asio::ip::tcp::socket(m_io_service);
m_acceptor.async_accept(*m_socket,
[&, socket](const boost::system::error_code& error)
{
if (!error)
{
auto connection = std::make_shared<connection>(std::move(*socket));
connection->start();
}
accept();
});
}