asio C++ library

PrevUpHomeNext

package (2 of 2 overloads)

Wrap a function object in a packaged task.

template<
    typename Function,
    typename Allocator>
packaged_token< typename decay< Function >::type, Allocator > package(
    Function && function,
    const Allocator & a);

The package function is used to adapt a function object as a packaged task. When this adapter is passed as a completion token to an asynchronous operation, the result of the function object is retuned via a std::future.

Example
std::future<std::size_t> fut =
  my_socket.async_read_some(buffer,
    package([](asio::error_code ec, std::size_t n)
      {
        return ec ? 0 : n;
      }));
...
std::size_t n = fut.get();

PrevUpHomeNext