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?

  1. yes, std::vector<t> movable if t not movable. left side merely takes ownership vector on right, no elements touched. (with 1 exception, listed in #2)

  2. the move assignment of vector call move constructor or move assignment of t if , allocators compare equal , left side's allocator's propagate_on_container_move_assignment false. (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_assignment true (it is), vector movable, , without touching individual elements. if it's false, 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.

  3. 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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -