With C++11 and later, user-defined completion handlers are only required to be move constructible, and are not required to be copy constructible.
When move support is enabled, asynchronous that are documented as follows:
template <typename Handler> void async_XYZ(..., Handler handler);
are actually declared as:
template <typename Handler> void async_XYZ(..., Handler&& handler);
The handler argument is perfectly forwarded and the move construction occurs
within the body of async_XYZ()
. This ensures that all other
function arguments are evaluated prior to the move. This is critical when
the other arguments to async_XYZ()
are members of the handler.
For example:
struct my_operation { unique_ptr<tcp::socket> socket; unique_ptr<vector<char>> buffer; ... void operator(error_code ec, size_t length) { ... socket->async_read_some(asio::buffer(*buffer), std::move(*this)); ... } };
Move support is automatically enabled for g++
4.5 and
later, when the -std=c++0x
or -std=gnu++0x
compiler options are used. It may be disabled by defining ASIO_DISABLE_MOVE
,
or explicitly enabled for other compilers by defining ASIO_HAS_MOVE
.
Note that these macros also affect the availability of movable
I/O objects.