Is a Moved-From Vector Always Empty?
Question:
It is known that objects of standard library types can be moved from, and while move operations may leave moved-from objects in an unspecified state, there are no explicit requirements excluding vector from this. However, how is it possible for moved-from vectors to be in a non-empty state?
Answer:
Generally, a moved-from vector will be empty, but it is not always the case.
Specific Cases:
The behavior of a moved-from vector depends on the operation used (move constructor or move assignment) and the allocator associated with the vector.
Move Constructor:
vector(vector&& v)
In this case, the moved-from vector will always be empty. The move operation requires constant complexity, necessitating the transfer of ownership from one vector to another, leaving the former empty.
Move Assignment:
vector& operator=(vector&& v)
There are three scenarios to consider:
It should be noted that some implementations may choose to explicitly clear the moved-from vector in this scenario, but it is not a requirement.
The above is the detailed content of When Is a Moved-From Vector Not Empty?. For more information, please follow other related articles on the PHP Chinese website!