3

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.

asmmo
  • 6,922
  • 1
  • 11
  • 25
Thompson Liu
  • 135
  • 1
  • 6
  • 1
    See [What can I do with a moved from object?](https://stackoverflow.com/q/7027523). In general, assignment doesn't have preconditions, it can be done with any valid state. – kmdreko Oct 17 '20 at 06:14

2 Answers2

3

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().

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

Of course, a will contain {1,3,4}. Simply moving from a vector means taking its content, the dynamically allocated array, and giving that content to the left-hand side beside somethings. Now the assigning will give new content to the old vector. This is perfectly valid.

asmmo
  • 6,922
  • 1
  • 11
  • 25