Self Move Assignment in Standard Library Containers
The C 11 standard guarantees that self move assignment (assignment of an object to itself after it has been moved from) is not possible for standard library containers like std::vector.
The specific provision in the standard is found in [res.on.arguments], which states that function arguments bound to rvalue references are considered unique references to their bound argument. This means that the implementation can assume that the argument is not referring to the same object as the target of the assignment.
In the case of std::vector, the move assignment operator (operator=) is defined to take an rvalue reference. Therefore, when selfAssign() is called with a std::vector argument, the standard allows the implementation to assume that the argument is a unique reference to a temporary object. As a result, self move assignment is not possible and the std::vector will be left in a resource-less state (0 capacity).
However, it is important to note that this behavior is only guaranteed for standard library containers. For user-defined types, the C 11 standard does not provide any guarantees regarding self move assignment.
The above is the detailed content of Why is Self Move Assignment Not Allowed for Standard Library Containers in C 11?. For more information, please follow other related articles on the PHP Chinese website!