Home > Backend Development > C++ > Why is Self Move Assignment Not Allowed for Standard Library Containers in C 11?

Why is Self Move Assignment Not Allowed for Standard Library Containers in C 11?

Patricia Arquette
Release: 2024-11-16 02:49:03
Original
417 people have browsed it

Why is Self Move Assignment Not Allowed for Standard Library Containers in C  11?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template