创建一个boost :: asio定时器的std :: vector使编译失败

时间:2014-03-12 11:45:46

标签: c++11 boost boost-asio

Coliru上的GCC 4.7.3和4.8.0都无法编译以下代码。看起来asio计时器不可移动:

#include <vector>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/system_timer.hpp>

int main() {
    boost::asio::io_service io;    
    boost::asio::system_timer t{io}; // works
    std::vector<boost::asio::system_timer> timers;
    timers.emplace_back(io); // timer cannot be constrcuted in place
    timers.push_back(std::move(t)); // cannot be moved as well
}

你知道创建定时器矢量的原因和方法吗?在我的例子中,一种解决方案是使用唯一指针的向量,但我更愿意避免这种情况。

编译器的错误是:

  

包含在/ usr / include / c ++ / 4.8 / vector:62:0,

中的文件
             from main.cpp:1:
     

/ usr / include / c ++ / 4.8 / bits / stl_construct.h:在'void的实例化中   std :: _ Construct(_T1 *,_ Args&amp;&amp; ...)[with _T1 =   提高:: ASIO :: basic_waitable_timer   _Args = {boost :: asio :: basic_waitable_timer,   boost :: asio :: waitable_timer_service&gt; &GT;}]':

     

/ usr / include / c ++ / 4.8 / bits / stl_uninitialized.h:75:53:需要来自   'static ForwardIterator   std :: _uninitialized_copy&lt; TrivialValueTypes&gt; :: _uninit_copy(_InputIterator,_InputIterator,_ForwardIterator)[with _InputIterator = std :: move_iterator *&gt;   _ForwardIterator = boost :: asio :: basic_waitable_timer *;   bool _TrivialValueTypes = false]'

     

/ usr / include / c ++ / 4.8 / bits / stl_uninitialized.h:117:41:从   '_ForwardIterator std :: uninitialized_copy(_InputIterator,   _InputIterator,_ForwardIterator)[with _InputIterator = std :: move_iterator *&gt;   _ForwardIterator = boost :: asio :: basic_waitable_timer *]'

     

/ usr / include / c ++ / 4.8 / bits / stl_uninitialized.h:258:63:从   ' ForwardIterator std :: _uninitialized_copy_a(_InputIterator,   _InputIterator,_ForwardIterator,std :: allocator&lt; _Tp&gt;&amp;)[with _InputIterator = std :: move_iterator *&gt;   _ForwardIterator = boost :: asio :: basic_waitable_timer *;   _Tp = boost :: asio :: basic_waitable_timer]'

     

/ usr / include / c ++ / 4.8 / bits / stl_uninitialized.h:281:69:从   “ ForwardIterator   的std :: _uninitialized_move_if_noexcept_a(_InputIterator,   _InputIterator,_ForwardIterator,_Allocator&amp;)[with _InputIterator = boost :: asio :: basic_waitable_timer *;   _ForwardIterator = boost :: asio :: basic_waitable_timer *;   _Allocator = std :: allocator

     
    

]'

  
     

/ usr / include / c ++ / 4.8 / bits / vector.tcc:415:43:从'void   std :: vector&lt; _Tp,_Alloc&gt; :: _ M_emplace_back_aux(_Args&amp;&amp; ...)[与_Args   = {boost :: asio :: io_service&amp;}; _Tp = boost :: asio :: basic_waitable_timer   _Alloc = std :: allocator

     
    

]'

  
     

/ usr / include / c ++ / 4.8 / bits / vector.tcc:101:54:从'void   std :: vector&lt; _Tp,_Alloc&gt; :: emplace_back(_Args&amp;&amp; ...)[with _Args =   {提高:: ASIO :: io_service对象和放大器;}; _Tp =   提高:: ASIO :: basic_waitable_timer   _Alloc = std :: allocator

     
    

]'

  
     

main.cpp:10:27:从这里要求

     

/ usr / include / c ++ / 4.8 / bits / stl_construct.h:75:7:错误:使用已删除   功能   '提高:: ASIO :: basic_waitable_timer :: basic_waitable_timer(升压:: ASIO :: basic_waitable_timer&安培;&安培)'

 { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }

   ^
     

在/usr/local/include/boost/asio.hpp:33:0中包含的文件中,

             from main.cpp:3:
     

/usr/local/include/boost/asio/basic_waitable_timer.hpp:127:7:注意:   '提高:: ASIO :: basic_waitable_timer :: basic_waitable_timer(升压:: ASIO :: basic_waitable_timer&安培;&安培)'   被隐式删除,因为默认定义是   非法的构造:

     

class basic_waitable_timer

   ^
     

包含来自的文件   /usr/local/include/boost/asio/basic_socket.hpp:20:0,

             from /usr/local/include/boost/asio/basic_datagram_socket.hpp:20,

             from /usr/local/include/boost/asio.hpp:21,

             from main.cpp:3:
     

/usr/local/include/boost/asio/basic_io_object.hpp:163:3:错误:   “提高:: ASIO :: basic_io_object :: basic_io_object(常量   boost :: asio :: basic_io_object&amp;)[with   IoObjectService =   boost :: asio :: waitable_timer_service&gt;布尔   Movable = false]'是私人的

     

basic_io_object(const basic_io_object&amp;);

     

^

     

在/usr/local/include/boost/asio.hpp:33:0中包含的文件中,

             from main.cpp:3:
     

/usr/local/include/boost/asio/basic_waitable_timer.hpp:127:7:错误:   在此背景下

     

class basic_waitable_timer

   ^

1 个答案:

答案 0 :(得分:4)

ASIO尚未使用移动语义(或至少部分内容)进行更新,因此计时器不可移动。所以你的选择是

  • 使用unique_ptr
  • 使用不需要元素的容器可以移动所有操作,例如dequelist

看起来像这样:

#include <deque>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/system_timer.hpp>

int main() {
    boost::asio::io_service io;    
    boost::asio::system_timer t{io}; // works
    std::deque<boost::asio::system_timer> timers;
    timers.emplace_back(io); // should work
    timers.push_back(std::move(t)); // won't work
}

但请勿尝试将inserterase用于集合,因为这些需要可移动性。