Boost C++ Libraries: Ticket #9023: asio::io_service::wrap() docs need an example https://svn.boost.org/trac10/ticket/9023 <p> The documentation for <strong>io_service::wrap</strong> reads: </p> <p> <strong>Return Value</strong> A function object that, when invoked, passes the wrapped handler to the <strong>io_service</strong> object's dispatch function. Given a function object with the signature: </p> <p> To say that this is cryptic would be an understatement. An example should be provided. One of the most common cases for calling <strong>io_service::wrap</strong> is when you want to post your own completion parameters for a given handler. For example, in your composed operation when an error occurs, you simply want to call the handler with the <strong>error_code</strong>. </p> <p> Here's an example from a piece of my own code. Note how the <a class="missing wiki">ReadHandler</a> is posted to the <strong>io_service</strong> with a default-constructed <strong>error_code</strong> and <strong>bytes_transferred</strong> from the result of <strong>asio::buffer_copy</strong>: </p> <pre class="wiki"> template &lt;typename MutableBufferSequence, typename ReadHandler&gt; BEAST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void (error_code, std::size_t)) async_read_some (MutableBufferSequence const&amp; buffers, BOOST_ASIO_MOVE_ARG(ReadHandler) handler) { using namespace boost; if (m_buffer.size () &gt; 0) { std::size_t const bytes_transferred = asio::buffer_copy ( buffers, m_buffer.data ()); return get_io_service ().wrap ( BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)) ( error_code (), bytes_transferred); } return m_next_layer.async_read_some (buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); } </pre><p> The addition of an example of a call to <strong>io_service::wrap</strong> would help illuminate this useful case. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9023 Trac 1.4.3 chris_kohlhoff Mon, 05 May 2014 08:48:14 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9023#comment:1 https://svn.boost.org/trac10/ticket/9023#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This is not intended as a valid use case for wrap(). The wrap() function is documented to use dispatch(), but the <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/asynchronous_operations.html">requirements on asynchronous operations</a> state that an operation must behave as if implemented using post(). </p> <p> Regarding your specific example, a better approach is to <strong>still</strong> call the m_next_layer.async_read_some() function inside your if-block, but with a zero-length buffer. The <a class="missing wiki">AsyncReadStream</a> type requirements say that this shall complete immediately, so you can use it as a replacement for post(), but with the added benefit of reducing the number of template instantiations. </p> Ticket