【发布时间】:2016-06-07 07:33:42
【问题描述】:
我有以下 C++14 代码
boost::asio::io_service service_;
我想将工作片段提交到io_service,使用以下代码,它接受任何函数,它是输入参数并向我返回一个std::future 作为返回值。
template <typename F, typename... Args>
auto enqueue(F &&f, Args &&... args) -> std::future<typename std::result_of<F(Args...)>::type> {
typedef typename std::result_of<F(Args...)>::type rType;
auto task = std::make_shared<std::packaged_task<rType()>>(std::bind(std::forward<F>(f),
std::forward<Args>(args)...));
std::future<rType> res = task->get_future();
service_.post(task);
return res;
}
然后使用
调用它enqueue([] (int i) {
return i+1;
}, 100);
这似乎不起作用。我收到一条错误消息,提示 service_.post() 不期待此输入。
xxxxxxxxxxxxxx:49:3: required from ‘std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> enqueue(F&&, Args&& ...) [with F = main()::<lambda()>::<lambda()>; Args = {int}; typename std::result_of<_Functor(_ArgTypes ...)>::type = int]’
xxxxxxxxxxxxxx:44:6: required from here
/usr/include/boost/asio/impl/io_service.hpp:102:3: error: static assertion failed: CompletionHandler type requirements not met
BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
^
/usr/include/boost/asio/impl/io_service.hpp:85:3: error: no match for call to ‘(std::shared_ptr<std::packaged_task<void()> >) ()’
BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
^
据我了解boost::asio 文档,这是可以做到的。有什么想法吗?
【问题讨论】:
标签: c++ c++11 boost boost-asio variadic-templates