2

I found this interesting link boost::asio::spawn yield as callback

and since that might be what I need I wanted to try out the following part:

template <class CompletionToken>
auto async_foo(uint64_t item_id, CompletionToken&& token)
{

    typename boost::asio::handler_type< CompletionToken, void(error_code, size_t) >::type handler(std::forward<CompletionToken>(token));
    //handler_type_t<CompletionToken, void(error_code, size_t)>                        handler(std::forward<CompletionToken>(token));

    async_result<decltype(handler)> result(handler);

    //async_request_data(item_id, handler);

    return result.get();
}  

But obviously neither handler_type_t nor boost::asio::handler_type is existing anymore in a newer boost version.

How can I adapt the example?

EDIT:

Is that he right way? Instead of

boost::asio::handler_type< CompletionToken, void(error_code, size_t) >::type

I used

typename boost::asio::async_result< CompletionToken, void(error_code, size_t) >::completion_handler_type
MichaelW
  • 1,328
  • 1
  • 15
  • 32

1 Answers1

2

Its almost right. from the docs of boost.asio

An initiating function determines the type CompletionHandler of its completion handler function object by performing typename async_result<decay_t<CompletionToken>, Signature>::completion_handler_type

and

An initiating function produces its return type as follows:

— constructing an object result of type async_result<decay_t<CompletionToken>, Signature>, initialized as result(completion_handler); and

— using result.get() as the operand of the return statement.

So correct way to adapt example would be

template <class CompletionToken>
auto async_foo(uint64_t item_id, CompletionToken&& token)
{

    typename boost::asio::async_result<std::decay_t<CompletionToken>, void(std::error_code, std::size_t)>::completion_handler_type
                    handler(std::forward<CompletionToken>(token));
    boost::asio::async_result<std::decay_t<CompletionToken>, void(std::error_code, std::size_t)> result(handler);

    async_request_data(item_id, handler);

    return result.get();
}
Community
  • 1
  • 1
Gaurav Dhiman
  • 1,163
  • 6
  • 8
  • This does not work with the `use_awaitable` completion token. One must use the new `boost::asio::async_initiate`. See my answer here: https://stackoverflow.com/a/73346297/245265 – Emile Cormier Aug 13 '22 at 17:23