如何替换此示例代码片段中已弃用的 handler_type_t 或 boost::asio::handler_type?

How can I replace deprecated handler_type_t or boost::asio::handler_type in this example code snippet?

我发现这很有趣 link boost::asio::spawn yield as callback

因为这可能是我需要的,所以我想尝试以下部分:

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();
}  

但显然 handler_type_tboost::asio::handler_type 都不再存在于较新的提升版本中。

我该如何改编这个例子?

编辑:

他这样做对吗?而不是

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

我用过

typename boost::asio::async_result< CompletionToken, void(error_code, size_t) >::completion_handler_type

差不多吧。来自 boost.asio

docs

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

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.

因此改编示例的正确方法是

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();
}