c++ - Is a std::vector<T> movable if T is not movable? -
i getting crash when trying move std::vector<t> t not movable (no move constructor/assignment operator defined, , contains internal pointers)
but why move functions of vector want call move functions of t? should not necessary.
so question title: std::vector<t> movable if t not movable?
yes,
std::vector<t>movable iftnot movable. left side merely takes ownership vector on right, no elements touched. (with 1 exception, listed in #2)the move assignment of
vectorcall move constructor or move assignment oftif , allocators compare equal , left side's allocator'spropagate_on_container_move_assignmentfalse. (note if move constructor might throw or doesn't exist, copy constructor used instead) however, unlikely encountering either of these.reworded: if
propagate_on_container_move_assignmenttrue(it is),vectormovable, , without touching individual elements. if it'sfalse, allocators compare equal, vector moved without touching individual elements. if it's false , allocators compare inequal, individual elements transferred. if nothrow move assignment exists, that's used. otherwise, if copy constructor exists, that's used. otherwise throwing move assignment used. if it's neither movable nor copiable, that's undefined behavior.- having no move assignment operator defined, , class containing internal pointers, not mean class not movable. in fact, sounds compiler thinks is movable. if want disable moves, use
t(t&&) = delete;,t& operator=(t&&) =delete, don't recommend that. instead, add correctly working move constructor , move assignemnt. tend easy, , quite useful.
Comments
Post a Comment