为什么我不能将boost计时器的std :: unique_ptr移动到捕获子句中?

时间:2017-09-26 01:23:43

标签: c++ boost c++14 boost-asio

当我尝试在boost :: basic_waitable_timer上调用async_wait时,在capture子句中有std :: unique_ptr,我得到错误。请看以下示例:

namespace asio = boost::asio;
using steady_timer = asio::basic_waitable_timer<std::chrono::steady_clock>;
asio::io_service ios;
std::thread([&ios] {
    asio::io_service::work w(ios);
    ios.run();
}).detach();


std::unique_ptr<steady_timer> timer = std::make_unique<steady_timer>(ios);
timer->async_wait([t = std::move(timer)](const system::error_code& ec) {
    std::cout << "timer hit " << "err:" << ec.message() << std::endl;
});

此代码给出了以下编译器错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2338   WaitHandler type requirements not met   cpp_rest_test   e:\development\boost_1_65_0\boost\asio\basic_waitable_timer.hpp 509 
Error   C2280   'timer_test::::(const timer_test:: &)': attempting to reference a deleted function   cpp_rest_test   e:\development\boost_1_65_0\boost\asio\basic_waitable_timer.hpp 509 
Error   C2280   'timer_test::::(const timer_test:: &)': attempting to reference a deleted function   cpp_rest_test   e:\development\boost_1_65_0\boost\asio\basic_waitable_timer.hpp 509 

提升错误让我想到了这个:

// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a WaitHandler.
BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;

我正在使用msvc140编译器。

0 个答案:

没有答案