c++11 - std::move a non-copyable object into a vector -


consider this:

struct a{}; struct b {     // make object non-copyable     b ( const b & )                 = delete;     b & operator= ( const b & )     = delete;      b(){};     std::unique_ptr<a> pa; };  int main() {     b b;     std::vector<b> vb;      vb.push_back(std::move(b));      return 0; } 

error:

../src/sandbox.cpp:24:27:   required here /usr/include/c++/4.7/bits/stl_construct.h:77:7: error: use of deleted function ‘b::b(const b&)’ 

who calling deleted copy constructor? trying move object not copy since has unique_ptr member.


as suggested commentators:

i tried implement move constructor:

b ( const b && rhs_ ) {     pa = std::move(rhs_.pa); // error below } 

and replaced push_back(std::move(b)) emplace_back(std::move(b))

i got error

../src/sandbox.cpp:17:25: error: use of deleted function ‘std::unique_ptr<_tp, _dp>& std::unique_ptr<_tp, _dp>::operator=(const std::unique_ptr<_tp, _dp>&) [with _tp = a; _dp = std::default_delete<a>; std::unique_ptr<_tp, _dp> = std::unique_ptr<a>]’ 

emplace_back work that. calls suitable constructor instead of copy constructor.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -