Search code examples
c++vectormove

c++ reassign after moved


From other posts that I understand after moved from, the object is in a valid, but unspecified state. Can I reassign things to the vector, illustrated by the example below:

std::vector a = {1,2,3};
std::vector b = std::move(a);
a = {1,3,4}

Does it guarantee that a will contain 1,3,4 after all.


Solution

  • This is guaranteed. After the move, the vector is guaranteed to be empty, i.e. has no elements. It's fine to be assigned by other contents.

    After the move, other is guaranteed to be empty().